I want to repeat an animation forever in my view. Below is my code:
[UIView animateWithDuration:0.5f
delay:0.49f
options:UIViewAnimationOptionCurveEaseInOut
animations:^{
//for IMMERSE
[_pic2 setAlpha:1.0f];
_view1_immerse=_pic2.frame;
_view1_immerse.origin.x = ([UIScreen mainScreen].bounds.size.width-_view1_immerse.size.width)/2;
self.pic2.frame=_view1_immerse;
}
completion:^(BOOL finished){
[UIView animateWithDuration:0.5f delay:0.49f options:UIViewAnimationOptionCurveEaseOut animations:^{
_view1_immerse=_pic2.frame;
_view1_immerse.origin.x = [UIScreen mainScreen].bounds.size.width;
self.pic2.frame=_view1_immerse;
[_pic2 setAlpha:0.0];
} completion:^(BOOL finished){
[UIView animateWithDuration:0.0f animations:^{
_view1_immerse=_pic2.frame;
_view1_immerse.origin.x = -_view1_immerse.size.width;
self.pic2.frame=_view1_immerse;
}];
}];
}];
I tried adding option UIViewAnimationOptionRepeat
, but it seems that it only repeats the "animation" part of the most outside layer. I want to repeat the whole set of animation.
And I do not want to use NSTimer
.
Could anyone give me some advice about my problem?? Thanks in advance!
To make our animation repeat, we use . repeatForever() . The default behavior of repeat forever is autoreverse, that's mean our animation will scale from 1 to 0.5 then 0.5 back to 1, creating a seamless loop of animation.
withAnimation() takes a parameter specifying the kind of animation you want, so you could create a three-second linear animation like this: withAnimation(.linear(duration: 3)) Explicit animations are often helpful because they cause every affected view to animate, not just those that have implicit animations attached.
SwiftUI has built-in support for animations with its animation() modifier. To use this modifier, place it after any other modifiers for your views, tell it what kind of animation you want, and also make sure you attach it to a particular value so the animation triggers only when that specific value changes.
You can check below code for repeat animation.
[UIView animateWithDuration:1.0f
delay:0.0f
options: UIViewAnimationOptionRepeat | UIViewAnimationOptionAutoreverse | UIViewAnimationOptionBeginFromCurrentState
animations: ^(void){self.view.center = CGPointMake(0, 200);}
completion:NULL];
Thanks
Swift 4
UIView.animate(withDuration: 100,
delay: 0,
options: [.repeat, .autoreverse, .beginFromCurrentState],
animations: {
}, completion: nil)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With