CALayer属性(例如:阴影)不会出现在所有UITableViewCell上

时间:2012-11-11 15:34:51

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

所以我在原型UIView中有一个UITableViewCell。在该视图的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中,但它也没有在那里一起工作。有没有人知道可能导致这种情况的原因?

1 个答案:

答案 0 :(得分:3)

我终于意识到问题在于阴影没有丢失,它们只是被其他细胞重叠。

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

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

相关问题