Run an animation with delay in swift
Depending on how your animation is implemented, you could use:
UIView.animate(withDuration: 0.3, delay: 5.0, options: [], animations: {
//Animations
}) { (finished) in
//Perform segue
}
This will apply a delay before the animations run, then run a completion block (where you execute your segue).
EDIT: You updated your question to use CAAnimations, so I don't think this solution will work.
Make use of UIView.animate(withDuration:)'s completion handler. Basic sample below:
func performMyAnimation() {
UIView.animate(withDuration: {$duration},
animations: { /* Animation Here */ },
completion: { _ in self.performSegue(withIdentifier: "segueIdentifierHere", sender: nil) }
}
Your animation runs in the animations
block. Upon completion, you'll then perform the segue to the next screen.
Edit: As the question now includes the animation code, using CABasicAnimation
, then perhaps this answer for a CAAnimation callback may work for you.
Basically, you need to wrap your CABasicAnimation
call with a CATransaction
.
CATransaction.begin()
CATransaction.setCompletionBlock({
self.performSegue(withIdentifier: "segueIdentifierHere", sender: nil)
})
// Your animation here
CATransaction.commit()
If you're looking for a delay which is standard in Swift 3, then it's probably best to use the DispatchQueue API in conjunction with UIView.animate(withDuration:_:)
.
If for example you wanted to delay a 1.0 second animation by 3 seconds:
let yourDelay = 3
let yourDuration = 1.0
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + .seconds(yourDelay), execute: { () -> Void in
UIView.animate(withDuration: yourDuration, animations: { () -> Void in
// your animation logic here
})
})
Let me know if this makes sense to you