自定义Segue类UIView动画问题

时间:2016-12-26 06:13:48

标签: ios swift animation uiviewcontroller segue

我在使用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)
    }
}

1 个答案:

答案 0 :(得分:0)

更新您的代码,如下所示,然后尝试:

   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)
    }
    })
    }