简体   繁体   中英

swift UIView Animation completion handler calls first

I have found a nice curl view animation, and I'd like to add segue after finishing, but segue is calling first (and even if go back to viewcontroller I can see animation ends). Please help me find a mistake or the way to achieve my goal

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

What you are doing is trying to animate the addition of animation to the view. The animations block is taking 1 second to finish. Basically it is trying to animate the addition of animation in a second. On completion of that, it starts navigating. Rather than doing that, you can use CATransaction to create the required functionality.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM