【发布时间】:2011-02-18 07:08:35
【问题描述】:
我想为具有图像层的 UIImageView 添加阴影。
我试过 self.layer.shadowOffset/shadowOpacity 路由,但是太慢了..
当我想添加阴影时,我调用 addShadowLayerWithOffset 方法,我希望在该方法下面调用 drawRect 并添加阴影..
但是 drawRect 没有被调用。
我在这里错过了什么?
- (void)drawRect:(CGRect)rect
{
SYSLOG(LOG_DEBUG, "in drawRect, isShadowed: %d", isShadowed);
if (isShadowed == true)
{
CGContextRef currentContext = UIGraphicsGetCurrentContext();
CGContextSaveGState(currentContext);
CGContextSetShadow(currentContext, CGSizeMake(100, 100), 3);
[super drawRect: rect];
CGContextRestoreGState(currentContext);
}
else
[super drawRect: rect];
}
- (void) addShadowLayerWithOffset: (int)offset
{
// self.layer.shadowOffset = CGSizeMake(offset,offset);
// self.layer.shadowOpacity = 0.7f;
// self.layer.shadowRadius = 5.0;
isShadowed = true;
[self setNeedsDisplay];
}
- 编辑
好的,我调用了 drawLayer。 我需要 [self.layer setNeedsDisplay] 而不是 [self.layer setNeedsPlay] 其中 self 是 UIImageView 子类。
但没有绘制阴影,实际上图像(原始图层)本身也没有显示。
我应该改变什么?
- (void) drawLayer: (CALayer*) layer inContext: (CGContextRef)context
{
SYSLOG(LOG_DEBUG, "in drawLayer, isShadowed: %d", isShadowed);
if(isShadowed == true)
{
CGContextSaveGState(context);
CGContextSetShadow(context, CGSizeMake(10, 10), 3);
[super drawLayer: layer inContext: context];
CGContextRestoreGState(context);
}
else
[super drawLayer: layer inContext: context];
}
【问题讨论】: