【问题标题】:How to add drop down shadows in tableview cells in iOS如何在 iOS 的 tableview 单元格中添加下拉阴影
【发布时间】:2014-09-24 13:31:45
【问题描述】:

正如question 中所见,可以使用这种类型的代码添加下拉阴影:

self.layer.shadowOffset = CGSizeMake(0, 1);
self.layer.shadowRadius = 1;
self.layer.shadowOpacity = 0.3;

但是这不能硬件加速,所以渲染需要很长时间,并且在tableview中使用时,需要重新绘制每个单元格,它会减慢很多!

解决了这个问题,有没有人知道如何顺利​​地做到这一点?

【问题讨论】:

    标签: ios objective-c uitableview calayer


    【解决方案1】:

    我找到了解决我的问题的方法,它更像是一种解决方法,但对我来说效果很好,我创建了自定义类 ShadowVIew,它为任何 UIView 进行了正确设置,并在其下方添加了一个深色透明视图以查看像影子:

    代码如下:

    ShadowView.m

    #import "ShadowView.h"
    
    @interface ShadowView()
    
    @property (nonatomic) UIView * shadow;
    
    @end
    
    @implementation ShadowView
    
    -(id)initWithCoder:(NSCoder *)aDecoder{
    
        if ((self = [super initWithCoder:aDecoder])) {
            self.layer.masksToBounds = NO;
            self.layer.cornerRadius = 8; // if you like rounded corners
        }
        return self;
    
    }
    
    -(void)layoutSubviews{
        [super layoutSubviews];
    
        if (self.shadow.superview == nil) {
            UIView * parent = self.superview;
            self.shadow = [[UIView alloc] initWithFrame:self.frame];
            self.shadow.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.3];
            CGRect frame = self.shadow.frame;
            frame.origin.y+=1;
            frame.origin.x-=1;
            frame.size.width+=2;
            self.shadow.frame = frame;
            self.shadow.layer.masksToBounds = NO;
            self.shadow.layer.cornerRadius = 8;
    
            [parent insertSubview:self.shadow atIndex:0];
            }
    
    
    @end
    

    请记住,这不是完美的解决方案,但可以很好地快速实施。如果您的视图不经常加载,您可以简单地添加石英绘制的阴影,这样不会影响应用程序的性能。

    self.layer.shadowOffset = CGSizeMake(0, 1);
    self.layer.shadowRadius = 1;
    self.layer.shadowOpacity = 0.3;
    

    希望它可以帮助某人:)

    【讨论】:

      猜你喜欢
      • 2016-11-20
      • 1970-01-01
      • 2013-01-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-15
      • 2016-09-27
      相关资源
      最近更新 更多