自定义Segue类UIView动画问题

问题描述:

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

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

更新你的代码像下面和尝试:

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

刚刚更新的答案尝试,让我知道,如果不能修复。 –

+0

好吧它修复了旧的问题,但现在动画的第二部分似乎并没有运行。黑色方块变为1.0 alpha,然后似乎消失,而目标VC出现。 –

+0

在此块中写入所有UI更新任务DispatchQueue.main.async {}类似于更新alpha等 –