2016-12-26 101 views
0

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

回答

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

剛剛更新的答案嘗試,讓我知道,如果不能修復。 –

+0

好吧它修復了舊的問題,但現在動畫的第二部分似乎並沒有運行。黑色方塊變爲1.0 alpha,然後似乎消失,而目標VC出現。 –

+0

在此塊中寫入所有UI更新任務DispatchQueue.main.async {}類似於更新alpha等 –