swift UIView 动画完成处理程序首先调用

swift UIView Animation completion handler calls first

我找到了一个不错的 curl 视图动画,我想在完成后添加 segue,但是 segue 正在调用(即使返回 viewcontroller 我也能看到动画结束)。请帮我找出错误或实现目标的方法

UIView.animate(withDuration: 1, animations: {
            let animation = CATransition()
            animation.duration = 1
            animation.startProgress = 0.0
            animation.endProgress = 1
            animation.timingFunction = CAMediaTimingFunction(name: CAMediaTimingFunctionName.easeInEaseOut)
            animation.type = CATransitionType(rawValue: "pageCurl")
            animation.subtype = CATransitionSubtype(rawValue: "fromRight")

            animation.isRemovedOnCompletion = false
            animation.isRemovedOnCompletion = false
            self.selectedCell!.view1.layer.add(animation, forKey: "pageFlipAnimation")

        }, completion: { _ in

            let secondViewController = self.storyboard?.instantiateViewController(withIdentifier: "PageVC") as? PageVC
             secondViewController!.modalPresentationStyle = .fullScreen
             self.navigationController?.present(secondViewController!, animated: false, completion: nil)
        })

您正在做的是尝试将动画添加到视图中。 动画块需要 1 秒才能完成。基本上它试图在一秒钟内为添加的动画制作动画。完成后,它开始导航。 除了这样做,您还可以使用 CATransaction 来创建所需的功能。

CATransaction.begin()
CATransaction.setCompletionBlock {
    if let secondViewController = self.storyboard?.instantiateViewController(withIdentifier: "PageVC") as? PageVC {
        secondViewController.modalPresentationStyle = .fullScreen
        self.navigationController?.present(secondViewController, animated: false, completion: nil)
    }
}

let animation = CATransition()
animation.duration = 1
animation.startProgress = 0.0
animation.endProgress = 1
animation.timingFunction = CAMediaTimingFunction(name: CAMediaTimingFunctionName.easeInEaseOut)
animation.type = CATransitionType(rawValue: "pageCurl")
animation.subtype = CATransitionSubtype(rawValue: "fromRight")

animation.isRemovedOnCompletion = false
animation.isRemovedOnCompletion = false
self.selectedCell!.view1.layer.add(animation, forKey: "pageFlipAnimation")

CATransaction.commit()