【问题标题】:CALayer properties (ex: drop shadows) not appearing on all UITableViewCellsCALayer 属性(例如:阴影)未出现在所有 UITableViewCells 上
【发布时间】:2012-10-31 05:19:55
【问题描述】:

所以我在原型UITableViewCell 中有一个UIView。在该视图的awakeFromNib 方法中,我有以下代码来制作阴影

CALayer *layer = self.layer;

layer.cornerRadius = 5.0f;

// Makes shadow for each cell in all and nearby table views.

CGSize size = self.bounds.size;
CGFloat curlFactor = 15.0f;
CGFloat shadowDepth = 5.0f;

UIBezierPath *path = [UIBezierPath bezierPath];
[path moveToPoint:CGPointMake(0.0f, 0.0f)];
[path addLineToPoint:CGPointMake(size.width, 0.0f)];
[path addLineToPoint:CGPointMake(size.width, size.height + shadowDepth)];
[path addCurveToPoint:CGPointMake(0.0f, size.height + shadowDepth)
        controlPoint1:CGPointMake(size.width - curlFactor, size.height + shadowDepth - curlFactor)
        controlPoint2:CGPointMake(curlFactor, size.height + shadowDepth - curlFactor)];

self.layer.shadowColor = [UIColor blackColor].CGColor;
self.layer.shadowOpacity = 0.3f;
self.layer.shadowOffset = CGSizeMake(2.0f, 7.0f);
self.layer.shadowRadius = 2.0f;
self.layer.masksToBounds = NO;
self.layer.shadowPath =  path.CGPath; //Sets a path for the shadow. Greatly enhances performance.

问题是阴影只出现在某些单元格上,而不是全部,而且有时似乎是随机的。我尝试将代码放在viewWillAppearviewDidAppear 中,但它也不能在那里一起工作。有没有人知道可能是什么原因造成的?

【问题讨论】:

    标签: ios objective-c uitableview core-animation calayer


    【解决方案1】:

    我终于意识到问题是阴影并没有丢失,它们只是被其他单元格重叠了。

    所以this answer 可以解决问题(在这种情况下你需要sendSubviewToBack:)。

    仅供参考,iOS 6 中的新 UICollectionView 将让您更好地控制单元 z 索引,对于 iOS 6 之前的支持,PSTCollectionView 是一个不错的选择。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-17
      • 2013-01-28
      • 2011-02-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多