參考(cocoachina):iOS-Core-Animation-Advanced-Techniques
簡(jiǎn)述
由于對(duì)view添加圓角需要maskToBounds = YES;而陰影正好是bounds之外渲染的,所以對(duì)同一個(gè)layer層添加顯示圓角就不能顯示陰影,參考以上文章,發(fā)現(xiàn)簡(jiǎn)單的方法就是對(duì)view添加自定義的layer從而實(shí)現(xiàn)圓角和需要圓角顯示的圖形, 然后再添加一個(gè)layer層用來顯示陰影
實(shí)現(xiàn)代碼如下:
//對(duì)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];