目錄:[Swift]Xcode實際操做html
本文將演示如何給圖像添加模糊效果。swift
在項目導航區,打開視圖控制器的代碼文件【ViewController.swift】ide
1 import UIKit 2 3 class ViewController: UIViewController { 4 5 override func viewDidLoad() { 6 super.viewDidLoad() 7 // Do any additional setup after loading the view, typically from a nib. 8 9 //從項目資源文件中加載一張圖片 10 let image = UIImage(named: "Picture") 11 //建立一個圖像視圖對象, 12 //並給圖像視圖指定須要顯示的圖片 13 let imageView = UIImageView(image: image) 14 //將圖像視圖,添加到當時視圖控制器的根視圖 15 self.view.addSubview(imageView) 16 17 //從iOS8.0版本開始,系統提供了模糊效果的功能。 18 //這裏判斷若是系統版本號大於等於8.0,則使用模糊效果 19 if #available(iOS 8.0, *) 20 { 21 //初始化一個模糊效果對象。 22 //模糊效果對象能夠幫助快速製做雷系與導航欄、通知中心或控制中心的毛玻璃效果 23 let blur = UIBlurEffect(style: .light) 24 //初始化一個基於模糊效果的視覺效果視圖 25 let blurView = UIVisualEffectView(effect: blur) 26 //設置設置模糊效果的位置爲(55,75),尺寸爲(200,200) 27 blurView.frame = CGRect(x: 55, y: 75, width: 200, height: 200) 28 //設置模糊視圖的圓角半徑爲30 29 blurView.layer.cornerRadius = 30 30 //設置模糊視圖的遮罩覆蓋屬性,進行邊界裁切 31 blurView.layer.masksToBounds = true 32 //將模糊視圖添加到圖像視圖,做爲圖像視圖的子視圖 33 imageView.addSubview(blurView) 34 } 35 else 36 { 37 print("UIBlurEffect is only available on iOS8.0 or later.") 38 } 39 } 40 41 override func didReceiveMemoryWarning() { 42 super.didReceiveMemoryWarning() 43 // Dispose of any resources that can be recreated. 44 } 45 }