UIViewPropertyAnimator repeat
Your code is working fine. The trouble is that your animation does nothing after the first time. You say:
view.transform = CGAffineTransform(rotationAngle: .pi)
The first time, we change the rotation from 0 to pi. That is a change, so there is animation. But after that we just keep saying “stay at pi” over and over. We are at pi and you say to stay there, so there is no change to animate.
What you want each animation to do is add pi, not be pi.
UIView Animation Options Repeat count
You can use the following to achieve your goal :
UIView.animateKeyframes(withDuration: 2, delay: 0, options: [], animations: {
UIView.addKeyframe(withRelativeStartTime: 0, relativeDuration: 0.5) {
self.ball.alpha = 0.0
}
UIView.addKeyframe(withRelativeStartTime: 0.5, relativeDuration: 0.5) {
self.ball.alpha = 1
}
}) { (isFinished) in
}
UIViewPropertyAnimator stop repeating
You are not able to stop animation because you are holding the first object of UIViewPropertyAnimator which is created and returned by your function, but this line of your function creating every time new object of UIViewPropertyAnimator
self.spinSpindle(spindle: spindle)
and you are not holding/returning this object to anywhere.
You are always calling stopAnimation function to first object which you have returned from function. That's why you are able to stop animation before you click the spindle before it completes its first revolution and not after the completion block called.
How to repeat UIView animation specific times
int animationLoopCount = 0;
- (void)doAnimation
{
for (animationLoopCount; animationLoopCount < 3; animationLoopCount++) {
[UIView animateWithDuration:1.0f
delay:0.1f
options:UIViewAnimationOptionCurveLinear
animations:^{
imageOne.center = CGPointMake(100, 150);
} completion:^(BOOL finished) {
imageOne.center = CGPointMake(100,200);
[self doAnimation];
if (animationLoopCount == 3) animationLoopCount = 0;
}];
}
}
Repeat/Autoreverse animations in iOS 13.x
You can do something like this:
UIView.animate(withDuration: 0.25, delay: 0, options: [.autoreverse, .curveEaseIn, .repeat], animations: {
let transform = CATransform3DIdentity
let rotate = CATransform3DRotate(transform, 45, 1, 1, 0)
self.ex.layer.transform = rotate
}, completion: nil)
For all the possible calls, you can check this link
In addition, if you really needs the UIViewPropertyAnimator
, it has a similar init:
UIViewPropertyAnimator.runningPropertyAnimator(withDuration: 0.25, delay: 0, options: [.autoreverse, .curveEaseIn, .repeat], animations: {
let transform = CATransform3DIdentity
let rotate = CATransform3DRotate(transform, 45, 1, 1, 0)
self.ex.layer.transform = rotate
})
How to create animation which animate repeatedly with UIViewPropertyAnimator?
UIViewPropertyAnimator
doesn't provide a way to do repeated animations. You need to use the old UIView
animate class methods.
Related Topics
Swift - How to Set Cookie in Nsmutableurlrequest
Common Equatable Class on Swift
Generic Swift Dictionary Extension for Nil Filtering
Bounce Rays with Enumeratebodies Alongraystart
How to Represent Magnitude for Mass in Swift
How Use and Run Swift 2.3 on Command Line
App Crashes When Trying to Append Data to a Child Value
Convert Bar Chart to a Grouped Bar Chart with Danielgindi/Ios-Charts and Swift
Remove Multiple Indices from Array
How to Resume Audio After Interruption in Swift
How to Compile Latest Swift Version on Older Xcode
Peak Detection for Growing Time Series Using Swift
Why Can't I Pass an Implicitly Unwrapped Optional as an Unsafemutablepointer