【问题标题】:(iphone) adding a shadow to layer(iphone) 为图层添加阴影
【发布时间】: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];
}

【问题讨论】:

    标签: iphone shadow drawrect


    【解决方案1】:

    尝试调用这个方法:

    由 UIViewController 调用:

    [self addShadowedLayer:self.view inRect:( CGRectMake(30, 30, 128, 192))];
    
    
    - (void)addShadowedLayer:(UIView *)aUIView inRect:(CGRect)aRect {
        CALayer *sublayer = [CALayer layer];
        sublayer.backgroundColor = [UIColor colorWithWhite:1.0 alpha:1.0].CGColor;
        sublayer.shadowOffset = CGSizeMake(0, 3);
        sublayer.shadowRadius = 5.0;
        sublayer.shadowColor = [UIColor blackColor].CGColor;
        sublayer.shadowOpacity = 0.8;
        sublayer.frame = aRect;
        sublayer.cornerRadius = 26.0;
        [aUIView.layer addSublayer:sublayer];
    }
    

    它只是添加了一个白色的圆角矩形,但你需要的只是它的影子......

    【讨论】:

      【解决方案2】:

      你在打电话:

      [super drawRect: rect];
      

      但是苹果说:

      你永远不应该自己直接调用这个方法。要使视图的一部分无效,从而导致该部分被重绘,请调用 setNeedsDisplay 或 setNeedsDisplayInRect: 方法。

      尝试过drawLayer,而不是drawRect?

      -(void)drawLayer:(CALayer *)layer inContext:(CGContextRef)context
      

      【讨论】:

      • 我猜 [super drawRect: rect] 很好,因为 [super dealloc] 很好。无论如何,drawLayer 也没有被调用。我已经设置了我添加到视图中的层的委托。
      • 感谢我调用了 drawLayer,但没有绘制图像,请查看我的编辑
      • @Eugene: ops,对不起,你是对的,我没有看到它是在 drawRect 方法中......我现在看看你的编辑......
      【解决方案3】:

      我认为你找错树了。你说直接在图像层上指定阴影太慢了。这是正确的,因为操作系统使用 alpha 通道来确定在哪里绘制阴影。

      在 iOS 3.2 及更高版本上,您可以通过指定阴影路径来提高渲染性能。您首先创建一个 CGPathRef 并将其设置为图层的阴影路径。

      这应该会显着改善渲染,而无需您仅为阴影引入另一层。

      【讨论】:

        猜你喜欢
        • 2011-04-02
        • 2017-10-17
        • 1970-01-01
        • 1970-01-01
        • 2019-08-12
        • 2015-01-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多