Swift - 给图片(imageView)添加阴影边框

前端之家收集整理的这篇文章主要介绍了Swift - 给图片(imageView)添加阴影边框前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
有时为了突出图片,需要给图片添加阴影效果。通过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()
    }
}
原文链接:https://www.f2er.com/swift/325044.html

猜你在找的Swift相关文章