具體實(shí)現(xiàn)的套路有兩種 ?
1、在圖層下方在添加一個(gè)背景圖層, 分別設(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
如果是一個(gè)矩形或者是圓,用CGPath會相當(dāng)簡單明了靴姿。但是如果是更加復(fù)雜一點(diǎn)的圖形沃但,UIBezierPath這個(gè)類會更加合適。