iOS - 模糊效果 UIBlurEffect

有些場景, 我們可能需要背景模糊的效果.

在 iOS 8 之後, Apple 官方提供了API 讓我們更容易實現此效果.

UIBlurEffect

Step 1: 先在畫面中設置好ImageView

Step 2: 設置模糊效果

1
let blurEffect = UIBlurEffect(style: UIBlurEffectStyle.Light)

UIBlurEffectStyle 共有三種效果

ExtraLight : 極亮
Light : 亮
Dark : 暗

Step 3: 設置模糊視窗並添加至畫面中

1
2
3
let blurView = UIVisualEffectView(effect: blurEffect)
blurView.frame = CGRect(x: 100.0, y: 120.0, width: 300.0, height: 300.0)
self.view.addSubview(blurView);

執行結果

Source Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import UIKit

class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let blurEffect = UIBlurEffect(style: UIBlurEffectStyle.ExtraLight)
let blurView = UIVisualEffectView(effect: blurEffect)
blurView.frame = CGRect(x: 100.0, y: 120.0, width: 300.0, height: 300.0)
self.view.addSubview(blurView);
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
作者

Nick Lin

發表於

2016-10-27

更新於

2023-01-18

許可協議


評論