【问题标题】:Custom Segue Class UIView Animation Issue自定义 Segue 类 UIView 动画问题
【发布时间】:2016-12-26 06:13:47
【问题描述】:

我在使用 swift 3 和 iOS 8 创建自定义 segue 时遇到问题。我试图通过从一个 VC 淡入到黑屏然后从黑色淡入到我的第二个 VC 来在视图控制器之间转换。我尝试通过使用下面的代码创建自定义 segue 来实现 segue,但它没有像我希望的那样工作。我的目标是在黑色方块从 0.5 alpha 变为 1.0 alpha 时执行动画,然后呈现我的第二个视图控制器,然后将黑色方块从 1.0 alpha 设置回 0.5 alpha 并删除它。现在,它正确地执行了第一部分,但是在动画完成后,您可以在第二个 VC 弹出之前短暂看到第一个 VC。我应该如何更改我的代码以使过渡更平滑并获得所需的结果?

override func perform() {
    let sourceVC = self.source
    let destinationVC = self.destination

    let square = UIView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height))
    square.alpha = 0.5
    square.backgroundColor = UIColor.black
    sourceVC.view.addSubview(square)

    UIView.animate(withDuration: 0.2, animations: {
        square.alpha = 1.0
    })

    UIView.animate(withDuration: 0.2, delay: 0.2, animations: { 
        square.alpha = 0.5
    }) { (finished) in
        sourceVC.present(destinationVC, animated: false, completion: nil)
    }
}

【问题讨论】:

    标签: ios swift animation uiviewcontroller segue


    【解决方案1】:

    如下所示更新您的代码并尝试:

       UIView.animate(withDuration: 0.2, animations: {
            square.alpha = 1.0
    
    UIView.animate(withDuration: 0.2, delay: 0.2, animations: { 
                        square.alpha = 0.5
                    }) { (finished) in
    
        DispatchQueue.main.async {
          sourceVC.present(destinationVC, animated: false, completion: nil)
        }
        })
        }
    

    【讨论】:

    • 好吧,它解决了旧问题,但现在动画的第二部分似乎没有运行。黑色方块变为 1.0 alpha 然后似乎消失了,而是出现了 destinationVC。
    • 在此块 DispatchQueue.main.async { } 中写入所有 UI 更新任务,如更新 alpha 等。
    • 当我将 square.alpha = 0.5 添加到 DispatchQueue.main.async 块中时,它会导致 SourceVC 立即更改为 DestinationVC 而根本不使用方形动画。就好像只有一个普通的节目转场。
    • 所以尽量增加持续时间
    • 我将两个动画的持续时间都增加到了 2 秒,但它仍然不显示
    猜你喜欢
    • 2017-02-15
    • 2012-06-02
    • 2013-10-15
    • 2015-01-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多