为有牺牲多壮志,敢教日月换新天。

[Xcode 实际操作]六、媒体与动画-(6)使用UIBlurEffect给图片添加模糊效果

热烈欢迎,请直接点击!!!

进入博主App Store主页,下载使用各个作品!!!

注:博主将坚持每月上线一个新app!!!

目录:[Swift]Xcode实际操作

本文将演示如何给图像添加模糊效果。

在项目导航区,打开视图控制器的代码文件【ViewController.swift】

 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 }

 

posted @ 2018-11-28 20:16  为敢技术  阅读(200)  评论(0编辑  收藏  举报