關(guān)于如何設(shè)置陰影的同時裁剪圖片

具體實現(xiàn)的套路有兩種 ?

1、在圖層下方在添加一個背景圖層, 分別設(shè)置。

self.layerView1.layer.cornerRadius =20.0f;self.layerView2.layer.cornerRadius =20.0f;//add a border to our layersself.layerView1.layer.borderWidth =5.0f;self.layerView2.layer.borderWidth =5.0f;//add a shadow to layerView1self.layerView1.layer.shadowOpacity =0.5f;self.layerView1.layer.shadowOffset =CGSizeMake(0.0f,5.0f);self.layerView1.layer.shadowRadius =5.0f;//add same shadow to shadowView (not layerView2)self.shadowView.layer.shadowOpacity =0.5f;self.shadowView.layer.shadowOffset =CGSizeMake(0.0f,5.0f);self.shadowView.layer.shadowRadius =5.0f;//enable clipping on the second layerself.layerView2.layer.masksToBounds =YES;

2、通過layer的shadowPath設(shè)置

@interfaceViewController()@property(nonatomic,weak)IBOutletUIView*layerView1;@property(nonatomic,weak)IBOutletUIView*layerView2;@end@implementationViewController- (void)viewDidLoad{? [superviewDidLoad];//enable layer shadowsself.layerView1.layer.shadowOpacity =0.5f;self.layerView2.layer.shadowOpacity =0.5f;//create a square shadowCGMutablePathRefsquarePath =CGPathCreateMutable();CGPathAddRect(squarePath,NULL,self.layerView1.bounds);self.layerView1.layer.shadowPath = squarePath;CGPathRelease(squarePath);? //create a circular shadowCGMutablePathRefcirclePath =CGPathCreateMutable();CGPathAddEllipseInRect(circlePath,NULL,self.layerView2.bounds);self.layerView2.layer.shadowPath = circlePath;CGPathRelease(circlePath);}@end

如果是一個矩形或者是圓,用CGPath會相當簡單明了。但是如果是更加復(fù)雜一點的圖形,UIBezierPath這個類會更加合適。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容