UIView阴影和模糊效果的实现

shadowOpacity shadowColor shadowOffset

在iOS中UIView的layer层提供了shadowOpacity Color Offset Radius等方法来实现View的阴影效果,可设计阴影透明度 颜色 偏移量等.
下面是代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
//图片添加阴影
let imgView1 = UIImageView(frame: myFrame)
imgView1.layer.shadowOpacity = 0.8
imgView1.layer.shadowColor = UIColor.blackColor().CGColor
imgView1.layer.shadowOffset = CGSize(width: 1, height: 1)
         
//图片添加阴影(透明背景)
let imgView2 = UIImageView(frame: myFrame)
imgView2.layer.shadowOpacity = 0.8
imgView2.layer.shadowColor = UIColor.blackColor().CGColor
imgView2.layer.shadowOffset = CGSize(width: 1, height: 1)
imgView2.layer.shadowRadius = 1
         
//按钮添加阴影
let btn = UIButton(frame: myFrame)
btn.layer.shadowOpacity = 0.8
btn.layer.shadowColor = UIColor.blackColor().CGColor
btn.layer.shadowOffset = CGSize(width: 1, height: 1)

添加模糊效果则可以使用UIBlurEffect来达到.

1
2
let blureffect = UIBlurEffect(style: UIBlurEffectStyle.Light)
let blurView = UIVisualEffectView(effect: blureffect)

UIView的Layer层