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()
    }
}

相关文章

软件简介:蓝湖辅助工具,减少移动端开发中控件属性的复制和粘...
现实生活中,我们听到的声音都是时间连续的,我们称为这种信...
前言最近在B站上看到一个漂亮的仙女姐姐跳舞视频,循环看了亿...
【Android App】实战项目之仿抖音的短视频分享App(附源码和...
前言这一篇博客应该是我花时间最多的一次了,从2022年1月底至...
因为我既对接过session、cookie,也对接过JWT,今年因为工作...