无法在UICollectionViewCell上设置阴影并具有圆angular。 一次只能做一件事

我有一个UICollectionViewCell子类,我需要圆angular,并添加一个阴影。 细胞看起来像一张方形的卡片,细胞之间有很大的空间。

所以在每个细胞的“下面”,我想添加一些阴影。 我可以成功做到这一点,但我的细胞只有底部的圆angular。 顶部只有正常的angular落。 我需要所有四个angular落的圆angular。

我已经在这里find了UIViews解决scheme,build议添加一个单独的UIView作为subview ,但我宁愿避免出于性能的原因。

我find了一个解决方法,就是使用这个方法,你可以在我的代码中find:

[UIBezierPath bezierPathWithRoundedRect: cornerRadius:]

但这对我也不起作用。 有没有可能是因为我试图只在底部添加阴影“底部”/在底部的细胞不工作对我来说? 似乎大多数这些答案都是为开发人员想要在整个单元格上添加阴影的问题提供的。

我想我会愿意添加一个特殊的subview到我的UICollectionViewCell子类,但我想用它作为最后的手段。

我的目标是iOS 7+和使用Xcode 6.1.1.

这里是我在我的UICollectionViewCell子类中使用的代码尝试实现阴影和圆angular:

 - (void)load:(CustomUserObject *)customObject { self.customObject = customObject; // Round cell corners self.layer.cornerRadius = 12; // Add shadow self.layer.masksToBounds = NO; self.layer.shadowOpacity = 0.75f; self.layer.shadowRadius = 10.0f; self.layer.shouldRasterize = NO; self.layer.shadowPath = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(self.frame.size.width/2 - (self.frame.size.width - 50)/2, self.frame.size.height, self.frame.size.width - 50, 10) cornerRadius:self.layer.cornerRadius].CGPath; } 

编辑:如果我将self.layer.masksToBounds设置为NO ,阴影的作品,但顶angular不圆。 如果我将self.layer.masksToBounds设置为YES ,阴影不起作用,但现在四舍五入了四个angular。 我只是不知道如何四舍五入,并得到阴影的工作。

看过蒂莫西•穆斯(Timothy Moose)在评论中分享的样本项目之后,我意识到我的所作所为几乎和他一样。

出于挫折,我重新访问了我的单元格的nib文件,最终打到了我。 我已经添加了一个UIView的单元格的顶部。 这个视图是作为一个彩色的横幅,也作为另一个UIImageViewUILabel的容器。

UICollectionViewCell的顶部成功地将UICollectionViewCell四舍五入,但是您永远不会知道,因为彩色的UIView位于单元格的顶部,并且与单元格一样宽。

愚蠢的错误,多次它的小事情。

这里是我用来实现四个圆angular和UICollectionViewCell下的阴影的最终代码。 self.banner是隐藏单元格顶部的额外UIView

 - (void)load:(CustomUserObject *)customObject { self.customObject = customObject; // Round the banner's corners UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.banner.bounds byRoundingCorners:(UIRectCornerTopLeft | UIRectCornerTopRight) cornerRadii:CGSizeMake(10, 10)]; CAShapeLayer *maskLayer = [CAShapeLayer layer]; maskLayer.frame = self.banner.bounds; maskLayer.path = maskPath.CGPath; self.banner.layer.mask = maskLayer; // Round cell corners self.layer.cornerRadius = 10; // Add shadow self.layer.masksToBounds = NO; self.layer.shadowOpacity = 0.75f; self.layer.shadowRadius = 10.0f; self.layer.shouldRasterize = NO; self.layer.shadowPath = [UIBezierPath bezierPathWithRect:CGRectMake(self.frame.size.width/2 - (self.frame.size.width - 50)/2, self.frame.size.height, self.frame.size.width - 50, 10)].CGPath; } 
 func dropShadow() { self.layer.masksToBounds = false self.layer.shadowColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.25).cgColor self.layer.shadowOpacity = 0.5 self.layer.shadowOffset = CGSize(width: 0.0, height: 2.0) self.layer.shadowRadius = 4.0 self.layer.cornerRadius = 5.0 } 

//放下阴影使用

cell.dropShadow()