参考(cocoachina):iOS-Core-Animation-Advanced-Techniques
简述
由于对view添加圆角需要maskToBounds = YES;而阴影正好是bounds之外渲染的,所以对同一个layer层添加显示圆角就不能显示阴影,参考以上文章,发现简单的方法就是对view添加自定义的layer从而实现圆角和需要圆角显示的图形, 然后再添加一个layer层用来显示阴影
实现代码如下:
//对self.loginBtn添加圆角,背景图片bkgImage, 以及阴影
CALayer *sublayer =[CALayer layer];
sublayer.backgroundColor =[UIColor whiteColor].CGColor;
sublayer.shadowOffset = CGSizeMake(3, 10);
sublayer.shadowRadius =22;
sublayer.shadowColor =[UIColor blackColor].CGColor;
sublayer.shadowOpacity =0.5;
sublayer.frame = self.loginBtn.bounds;
sublayer.cornerRadius =22;
[self.loginBtn.layer addSublayer:sublayer];
CALayer *imageLayer =[CALayer layer];
imageLayer.frame = sublayer.bounds;
imageLayer.cornerRadius =self.loginBtn.bounds.size.height / 2;
imageLayer.contents =(id)backImage.CGImage;
imageLayer.masksToBounds =YES;
[sublayer addSublayer:imageLayer];
网友评论