# 模糊效果 ### 需求說明 iOS 14 beta版測試時,發現Blur的 private key無法使用... ![](https://i.imgur.com/RBFKDY4.gif =200x) ### ![BulrKit](https://i.imgur.com/lXmLIRG.png =100x) [github](https://github.com/touyu/BlurKit) ```swift label.bk.addBlur(blurRadius: 3, colorTint: .white, colorTintAlpha: 0.4) imageView.bk.addBlur(blurRadius: 4, colorTint: .white, colorTintAlpha: 0.2) view.bk.removeBlur() view.bk.isBlurHidden = true // or false ``` ![](https://i.imgur.com/qiSdDVd.png =500x) ## Private API - iPhone中的API 1. Published API(Documented API) 2. Private API:PrivateFrameworks框架中的API 3. UnPublished API(Undocumented API):放在Frameworks框架中,但是卻沒有在官方文件中有使用說明、程式碼介紹等記錄的API ## VisualEffect + Animator ```swift // 動畫 private let animator = UIViewPropertyAnimator(duration: 0.1, curve: .linear, animations: nil) // 遮罩 private let visualEffectView = UIVisualEffectView(effect: nil) /// 模糊程度 private let blurPercentage: CGFloat = 0.15 /// 設定blurEffectView的範圍與模糊程度 private func setupVisualBlurEffectView() { contentView.addSubview(visualEffectView) visualEffectView.isUserInteractionEnabled = false visualEffectView.translatesAutoresizingMaskIntoConstraints = false NSLayoutConstraint.activate([ visualEffectView.topAnchor.constraint(equalTo: contentView.topAnchor), visualEffectView.leadingAnchor.constraint(equalTo: contentView.leadingAnchor), visualEffectView.trailingAnchor.constraint(equalTo: contentView.trailingAnchor), visualEffectView.bottomAnchor.constraint(equalTo: contentView.bottomAnchor) ]) animator.fractionComplete = blurPercentage } /// 移除blurEffectView func removeVisualBlurEffectViewIfNeed() { for subview in contentView.subviews where subview.isKind(of: UIVisualEffectView.self) { subview.removeFromSuperview() return } } override func awakeFromNib() { super.awakeFromNib() animator.addAnimations { // 完成狀態 self.visualEffectView.effect = UIBlurEffect(style: .regular) } } /// 準備reuse override func prepareForReuse() { removeVisualBlurEffectViewIfNeed() animator.stopAnimation(true) if animator.state == .stopped { animator.finishAnimation(at: .current) } } deinit { animator.stopAnimation(true) if animator.state == .inactive { animator.finishAnimation(at: .current) } } /// Animator若在完成之間就離開,會有NSInternalInconsistencyException的狀況 /// reason: 'It is an error to release a paused or stopped property animator. Property animators must either finish animating or be explicitly stopped and finished before they can be released.' /// reason: 'finishAnimationAtPosition: should only be called on a stopped animator!' /// 以上錯誤說明 /// 1. 釋放掉動畫前,必需明確的'停止'或是'完成'才能釋放 /// 2. finishAnimation只能在stopAnimation之後呼叫 private func finishAnimator() { animator.stopAnimation(true) if animator.state == .stopped { animator.finishAnimation(at: .current) } } ``` ## 待解決問題 - 加入Notification後發現,在didEnterBackgroundNotification時,畫面會開始變模糊 - 若在didEnterBackgroundNotification下斷點,就會正常 ![](https://i.imgur.com/6wSnHs2.gif)
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up