【问题标题】:Screen Transition effect using UIView.animate使用 UIView.animate 的屏幕过渡效果
【发布时间】:2020-01-23 17:06:37
【问题描述】:

我有一个带有各种屏幕的应用程序。我在每个屏幕上都有一个下一个按钮和一个上一个按钮。单击下一个按钮时,我将显示下一个屏幕。现在我正在通过使用 UIView.animate() 来显示我的下一个屏幕,它只是用下一个屏幕替换我的当前屏幕。它在屏幕转换时不会产生/产生任何动画效果。单击下一个按钮时,我希望下一个屏幕看起来像从右侧出现/呈现。怎样才能达到这样的过渡效果?

【问题讨论】:

    标签: ios swift user-interface uiview uiviewanimation


    【解决方案1】:

    您可以使用导航视图控制器并将其设置为该系列视图的初始视图控制器。

    let newView  = sampleStoryBoard.instantiateViewController(withIdentifier: "NewViewController") as! NewViewController 
    
    self.present(newView, animated: true, completion: nil)
    

    【讨论】:

    • 我目前的设计中没有使用导航视图控制器。我的设计就像一开始的通用视图控制器,它将有一个容器视图。所有接下来的屏幕都被替换为容器视图。我现在不想改变我的设计。我希望使用 UIView.animate() 来完成它
    • 编辑了我的答案以编程方式完成。
    • @ShaneShelton 请将代码放入代码块中。
    【解决方案2】:

    您可以实现简单的协议,并将其用于您的视图:

    protocol PresentedAsPush {
        func presentLikeAPush()
    }
    
    extension UIView: PresentedAsPush {
    
        func presentLikeAPush() {
            self.frame = CGRect(x: bounds.width, y: 0, width: bounds.width, height: bounds.height)
            UIView.animate(withDuration: 5) { [weak self] in // configure slide animation duration, and handler, if you need
                guard let wrappedSelf = self else { return }
                wrappedSelf.frame = CGRect(x: 0, y: 0, width: wrappedSelf.bounds.width, height: wrappedSelf.bounds.height)
            }
        }
    
    }   
    

    推送代码:

    func pushView() {
         let blueView = UIView(frame: view.bounds)
         blueView.backgroundColor = UIColor.blue
         view.addSubview(blueView)
         blueView.presentLikeAPush()   
    }   
    

    在您的情况下,您只需使用:

    toController.view.presentLikeAPush()   
    

    您也可以为 presentLikeAPushMethod 实现处理程序,并在其中返回动画完成块,并在动画之后执行您需要的操作(删除旧控制器等)

    您还可以使用约束代替框架。
    添加完整代码:

    class RootViewController: UIViewController {
    
        override func viewDidLoad() {
            super.viewDidLoad()
            view.backgroundColor = UIColor.green
            let redVc = RedViewController()
            addChild(redVc)
            view.addSubview(redVc.view)
        }
    
    }
    
    class RedViewController: UIViewController {
    
        override func viewDidLoad() {
            super.viewDidLoad()
            view.backgroundColor = UIColor.red
            let pushButton = UIButton()
            view.addSubview(pushButton)
            pushButton.setTitle("push", for: .normal)
            pushButton.frame = CGRect(x: 20, y: 100, width: 100, height: 50)
            pushButton.addTarget(self, action: #selector(pushButtonPressed), for: .touchUpInside)
        }
    
        @objc private func pushButtonPressed() {
            guard let parent = parent else { return }
    
            let blueVc = BlueViewController()
            parent.addChild(blueVc)
            parent.view.addSubview(blueVc.view)
    
            blueVc.view.presentLikeAPush(fromController: self)
        }
    
    }
    
    class BlueViewController: UIViewController {
    
        override func viewDidLoad() {
            super.viewDidLoad()
            view.backgroundColor = UIColor.blue
        }
    
    }
    
    protocol PresentedAsPush {
        func presentLikeAPush(fromController: UIViewController)
    }
    
    extension UIView: PresentedAsPush {
    
        func presentLikeAPush(fromController: UIViewController) {
            self.frame = CGRect(x: bounds.width, y: 0, width: bounds.width, height: bounds.height)
            UIView.animate(withDuration: 2, animations: { [weak self] in
                guard let wrappedSelf = self else { return }
                wrappedSelf.frame = CGRect(x: 0, y: 0, width: wrappedSelf.bounds.width, height: wrappedSelf.bounds.height)
            }) { (completed) in
                fromController.view.removeFromSuperview()
                fromController.removeFromParent()
                fromController.willMove(toParent: nil)
            }
        }
    }
    

    【讨论】:

    • 你写的2个方法能不能解释清楚,我没完全明白。您编写了一个函数 pushView(),它不会从任何地方被调用。
    • @KunduriRaj,pushView()是一个解释如何使用presentLikeAPush()方法的例子,你不需要
    • @idev.agg.mf 你能看一下我更新的代码吗。我尝试在已完成的块中删除旧控制器,但旧控制器仍然被新控制器覆盖。
    猜你喜欢
    • 1970-01-01
    • 2012-05-15
    • 1970-01-01
    • 2020-09-17
    • 1970-01-01
    • 2018-08-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多