首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用模糊效果呈现视图控制器

问使用模糊效果呈现视图控制器
EN

Stack Overflow用户
提问于 2016-09-23 00:54:24
回答 3查看 9.5K关注 0票数 11

我以模态的方式呈现了一个具有模糊背景效果的视图控制器。iOS 10/XCode8引入了我的动画的一个问题。以下是演示文稿代码:

代码语言:javascript
复制
let modalVC = ModalViewController(nibName: "ModalViewController", bundle: nil)
modalVC.modalTransitionStyle = .CrossDissolve
modalVC.modalPresentationStyle = .OverFullScreen
presentViewController(modalVC, animated: true, completion: nil)

在ModalViewController中添加viewDidLoad()模糊函数:

代码语言:javascript
复制
let blurEffect = UIBlurEffect(style: .Light)
let blurEffectView = UIVisualEffectView(effect: blurEffect)
blurEffectView.frame = view.bounds
blurEffectView.autoresizingMask = [.FlexibleWidth, .FlexibleHeight]

view.addSubview(blurEffectView)
view.sendSubviewToBack(blurEffectView)

ModalViewController有一个清晰的背景,我添加了一个具有暗模糊效果的BlurEffectView。使用前面的代码段和在界面生成器中以编程方式进行了尝试。

在iOS 8和9上,.CrossDissolve转换处理了“淡入淡出”,但在iOS 10 (设备和模拟器)上测试后,视图显示为深色半透明背景色,而不是模糊。

.CrossDissolve动画完成后,背景颜色将更改为实际的模糊效果背景。你知道为什么会发生这种情况吗?

我还尝试在viewDidLoad()的开头和结尾为模式视图控制器添加layoutIfNeeded(),但没有成功。我使用的是swift 2.3

EN

回答 3

Stack Overflow用户

发布于 2017-03-21 00:39:12

嗨,

您需要创建新的UIViewControllerAnimatedTransitioning。

然后在animateTransition(using transitionContext: UIViewControllerContextTransitioning)中,你需要编写你的动画。

现在,在iOS 10中,您可以使用UIViewPropertyAnimator来为UIVisualBlurEffect的BlurRadius设置动画。

结果:

​

下面是一个用法示例:https://github.com/PierrePerrin/PPBlurModalPresentation

第一

你需要创建你的模糊过渡

代码语言:javascript
复制
    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()
            })
        }

    }

然后

您需要在ViewController中设置转换委托

代码语言:javascript
复制
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
    }

}
票数 11
EN

Stack Overflow用户

发布于 2016-09-26 16:46:19

首先,我认为这个解决方案是一个临时的解决方案,因为我必须假设这个新引入的行为是一个错误,并将在未来的更新中修复。这使得它稍微不那么明显,因为模糊效果在动画期间而不是在动画之后弹出。仍然不如iOS 9和以后的版本好,但略好一些。

不带动画的

  1. Present视图控制器:

presentViewController(modalVC,animated: false,completion: nil)

  • 从头开始隐藏视图:

override func viewDidLoad() { super.viewDidLoad() view.alpha =0}

  • 手动应用动画:

覆盖函数viewWillAppear(动画: Bool) {super.viewWillAppear(动画) UIView.animateWithDuration(0.25) { self.view.alpha =1}}覆盖函数viewWillDisappear(动画: Bool) {super.viewWillDisappear(动画) UIView.animateWithDuration(0.25) { self.view.alpha =0} }

票数 1
EN

Stack Overflow用户

发布于 2016-10-12 20:41:41

唯一正确的方法是创建自定义模式转换和动画效果属性。请参阅https://stackoverflow.com/a/39709740

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39644775

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档