Fdo*_*Fdo 10 iphone ios swift uiblureffect xcode8
我正在以模糊背景效果模式呈现视图控制器.iOS 10/XCode 8引入了我的动画问题.这是演示代码:
let modalVC = ModalViewController(nibName: "ModalViewController", bundle: nil)
modalVC.modalTransitionStyle = .CrossDissolve
modalVC.modalPresentationStyle = .OverFullScreen
presentViewController(modalVC, animated: true, completion: nil)
Run Code Online (Sandbox Code Playgroud)
viewDidLoad()
在ModalViewController中添加函数模糊:
let blurEffect = UIBlurEffect(style: .Light)
let blurEffectView = UIVisualEffectView(effect: blurEffect)
blurEffectView.frame = view.bounds
blurEffectView.autoresizingMask = [.FlexibleWidth, .FlexibleHeight]
view.addSubview(blurEffectView)
view.sendSubviewToBack(blurEffectView)
Run Code Online (Sandbox Code Playgroud)
该ModalViewController
有一个明确的背景下,我加入了BlurEffectView
一个黑暗的模糊效果.以编程方式使用上一个代码段和Interface Builder进行尝试.
在iOS 8和9上,.CrossDissolve
转换处理了"淡入淡出",但在iOS 10(设备和模拟器)上进行测试后,视图显示为黑色半透明背景颜色而不是模糊.
在之后.CrossDissolve
动画完成,背景颜色变为实际的模糊效果的背景.任何想法为什么会这样?
还尝试layoutIfNeeded()
在viewDidLoad()
模态视图控制器的开头和结尾添加没有任何运气.我正在使用swift 2.3
Pie*_*rin 10
嗨,
你需要创建一个新的UIViewControllerAnimatedTransitioning
.
然后animateTransition(using transitionContext: UIViewControllerContextTransitioning)
你需要编写动画代码.
现在在iOS的10,您可以使用UIViewPropertyAnimator
,以动画BlurRadius
的UIVisualBlurEffect
.
这里有一个用法示例:https://github.com/PierrePerrin/PPBlurModalPresentation
您需要创建模糊过渡
class BlurModalPresentation: NSObject,UIViewControllerAnimatedTransitioning {
func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval{
return 0.5
}
//This is the blur view used for transition
var blurView = UIVisualEffectView(effect: UIBlurEffect(style: UIBlurEffectStyle.light))
var destinationView : UIView!
var animator: UIViewPropertyAnimator?
// This method can only be a nop if the transition is interactive and not a percentDriven interactive transition.
func animateTransition(using transitionContext: UIViewControllerContextTransitioning){
let containerView = transitionContext.containerView
_ = transitionContext.viewController(forKey: UITransitionContextViewControllerKey.from)
let toVc = transitionContext.viewController(forKey: UITransitionContextViewControllerKey.to)
destinationView = toVc!.view
destinationView.alpha = 0.0
//Here we add the blur view and set it effect to nil
blurView.effect = nil
blurView.frame = containerView.bounds
self.blurTransition(transitionContext) {
self.unBlur(transitionContext, completion: {
self.blurView.removeFromSuperview()
transitionContext.completeTransition(true)
})
}
containerView.addSubview(toVc!.view)
containerView.addSubview(blurView)
}
//This methods add the blur to our view and our destinationView
func blurTransition(_ context : UIViewControllerContextTransitioning,completion: @escaping () -> Void){
UIViewPropertyAnimator.runningPropertyAnimator(withDuration: self.transitionDuration(using: context)/2, delay: 0, options: UIViewAnimationOptions.curveLinear, animations: {
self.destinationView.alpha = 0.5
self.blurView.effect = UIBlurEffect(style: UIBlurEffectStyle.light)
}, completion: { (position) in
completion()
})
}
//This Method remove the blur view with an animation
func unBlur(_ context : UIViewControllerContextTransitioning,completion: @escaping () -> Void){
UIViewPropertyAnimator.runningPropertyAnimator(withDuration: self.transitionDuration(using: context) / 2, delay:0, options: UIViewAnimationOptions.curveLinear, animations: {
self.destinationView.alpha = 1.0
self.blurView.effect = nil
}, completion: { (position) in
completion()
})
}
}
Run Code Online (Sandbox Code Playgroud)
您需要在以下位置设置转换委派ViewController
:
import UIKit
class ViewController: UIViewController,UIViewControllerTransitioningDelegate {
let blurModalPresentation = BlurModalPresentation()
override func viewDidLoad() {
super.viewDidLoad()
}
func showVC(){
let str = self.storyboard!
let vc = str.instantiateViewController(withIdentifier: "YourViewControllerIdentifier")
vc.transitioningDelegate = self
self.present(vc, animated: true, completion: nil)
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
}
func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning?{
return blurModalPresentation
}
func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning?{
return blurModalPresentation
}
}
Run Code Online (Sandbox Code Playgroud)
首先,我将此解决方案视为临时解决方法,因为我必须假设这个新引入的行为是一个错误,并将在未来的更新中修复。这使得它稍微不那么明显,因为模糊效果是在动画期间而不是之后突然出现的。仍然不如 iOS 9 及其后版本那么好,但稍微好一些。
显示没有动画的视图控制器:
presentViewController(modalVC, animated: false, completion: nil)
Run Code Online (Sandbox Code Playgroud)从一开始就隐藏你的视图:
override func viewDidLoad() {
super.viewDidLoad()
view.alpha = 0
}
Run Code Online (Sandbox Code Playgroud)手动应用动画:
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
UIView.animateWithDuration(0.25) {
self.view.alpha = 1
}
}
override func viewWillDisappear(animated: Bool) {
super.viewWillDisappear(animated)
UIView.animateWithDuration(0.25) {
self.view.alpha = 0
}
}
Run Code Online (Sandbox Code Playgroud) 归档时间: |
|
查看次数: |
6812 次 |
最近记录: |