有时为了突出图片,需要给图片添加阴影效果。通过UIImageView的layer阴影属性设置,可以很方便的实现这个功能。 不仅是UIImageView,其他的UI控件也是可以设置阴影的。
import UIKit class ViewController: UIViewController { @IBOutlet weak var imageView1: UIImageView! @IBOutlet weak var imageView2: UIImageView! @IBOutlet weak var button1: UIButton! override func viewDidLoad() { super.viewDidLoad() //图片添加阴影 self.imageView1.layer.shadowOpacity = 0.8 self.imageView1.layer.shadowColor = UIColor.blackColor().CGColor self.imageView1.layer.shadowOffset = CGSize(width: 1,height: 1) //图片添加阴影(透明背景) self.imageView2.layer.shadowOpacity = 0.8 self.imageView2.layer.shadowColor = UIColor.blackColor().CGColor self.imageView2.layer.shadowOffset = CGSize(width: 1,height: 1) self.imageView2.layer.shadowRadius = 1 //按钮添加阴影 self.button1.layer.shadowOpacity = 0.8 self.button1.layer.shadowColor = UIColor.blackColor().CGColor self.button1.layer.shadowOffset = CGSize(width: 1,height: 1) } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() } }