想必在日常的開發(fā)中大家都能遇到UI各種圓角加陰影的設(shè)計(jì)
添加圓角效果:
testView.layer.cornerRadius = 5;
testView.layer.masksToBounds = YES;
添加陰影效果:
testView.layer.shadowColor = theColor.CGColor
// 陰影偏移
testView.layer.shadowOffset = CGSizeMake(0,0)
// 陰影透明度
testView.layer.shadowOpacity = 0.5
// 陰影半徑
testView.layer.shadowRadius = 2
需要圓角+陰影效果則不能滿足,view.layer.masksToBounds會(huì)對(duì)圖層進(jìn)行裁剪状植,導(dǎo)致陰影效果失效.
解決方式:
添加一層中間view,在該view上進(jìn)行陰影設(shè)置曲伊,在子view上進(jìn)行圓角設(shè)置
//在AView上設(shè)置圓角
AView.layer.cornerRadius = 5;
AView.layer.masksToBounds = YES;
//在BView上設(shè)置陰影
BView.layer.shadowColor = theColor.CGColor
// 陰影偏移
BView.layer.shadowOffset = CGSizeMake(0,0)
// 陰影透明度
BView.layer.shadowOpacity = 0.5
// 陰影半徑
BView.layer.shadowRadius = 2
//將AView上添加到BView上
[BView addSubView: AView]
[self.view addSubView: BView]
就可以愉快的實(shí)現(xiàn)圓角+陰影的效果了