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

Posted

技术标签:

【中文标题】swift UIView 动画完成处理程序首先调用【英文标题】:swift UIView Animation completion handler calls first 【发布时间】:2020-06-10 17:43:33 【问题描述】:

我找到了一个不错的 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】:

您正在做的是尝试将动画添加到视图中。 动画块需要 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()

【讨论】:

以上是关于swift UIView 动画完成处理程序首先调用的主要内容,如果未能解决你的问题,请参考以下文章

在动画完成之前调用 UIView 动画完成

如何在swift中打开另一个动画完成视图?

UIView animate withDuration 不会在 iOS 9.3.5 上调用完成处理程序

动画中断时未调用 UIView 动画完成块

从 Timer 选择器函数 Swift 调用函数的完成处理程序

为啥UIView动画会改变View frame?