Repeat animation a variable number of times

I was wondering how I can tweak the animation to repeat. The number of repetitions should be determined by the variable. In the following code, the int newPage variable should determine how often the animation repeats.

I tried this, but the animation (which uses block animation) was done only once:

 for (int temp = 1; temp <= newPage; temp++) { [self animatePage]; } 

If I code the following, it works the way I want, but it is hard-coded (i.e. the animation will be repeated twice), and I see no way to change the number of ways that this animation is often executed in the code and according to my newPage variable:

 [UIView animateWithDuration:0 delay:0.1 options:UIViewAnimationOptionCurveEaseIn animations:^{[self animatePage];} completion:^(BOOL finished){[self animatePage];}]; 

I would be very grateful for suggestions on how to repeat the same animation without requiring hard coding the number of times I want this animation to repeat.




EDIT:

I tried to implement the following code, but in fact only one animation will be executed:

  [UIView animateWithDuration:0 delay:1 options:UIViewAnimationOptionCurveEaseIn animations:^{ [UIView setAnimationRepeatCount:2]; [self animatePage]; } completion:nil]; 
+12
objective-c iphone cocoa-touch animation
Apr 28 '11 at 18:13
source share
4 answers

Had the same problem - you were missing an'UIViewAnimationOptionRepeat '

This should work:

  [UIView animateWithDuration:0 delay:1 options:UIViewAnimationOptionCurveEaseIn | UIViewAnimationOptionRepeat animations:^{ [UIView setAnimationRepeatCount:2]; // **This should appear in the beginning of the block** [self animatePage]; } completion:nil]; 

Did the trick for me.

+31
Mar 16 '13 at 22:34
source share

Have you tried setting repeatCount? + (void)setAnimationRepeatCount:(float)repeatCount

I tried the following block of code and it definitely repeats 2x for me (l was a UITextView that was magnified 2x in X dir and 3X in Y dir):

 [UIView animateWithDuration:2 delay:0.1 options:UIViewAnimationOptionCurveEaseIn animations:^{ [UIView setAnimationRepeatCount:2]; l.transform = CGAffineTransformMakeScale(2,3); } completion:nil]; 
+8
Apr 28 '11 at 18:15
source share

the reason you see, but one animation is that because you are invoking an animation from a loop in the same runloop, renaming in the last call win (one animation)

instead of calling [self animatePage] , try calling

 [self performSelector:@selector(animatePage) withObject:nil afterDelay:.1 *temp]; 

this will create your calls for individual threads.

you may need to play with a delay interval

+2
Apr 28 '11 at 18:20
source share

To avoid glitches between animations, use keyframes. I wrote an extension that works with everything that matches a UIView (a ton of visual elements)

Swift 4:

 import UIKit extension UIView{ func rotate(count: Float, _ complete: @escaping ()->()) { UIView.animateKeyframes(withDuration: 1.0, delay: 0, options: [.repeat], animations: { //to rotate infinitely, comment the next line UIView.setAnimationRepeatCount(count) //rotate the object to 180 degrees UIView.addKeyframe(withRelativeStartTime: 0, relativeDuration: 0.5/1.0, animations: { self.transform = CGAffineTransform(rotationAngle: (CGFloat(Double.pi))) }) UIView.addKeyframe(withRelativeStartTime: 0, relativeDuration: 0.5/1.0, animations: { //rotate the object from 180 to 360 degrees self.transform = CGAffineTransform(rotationAngle: (CGFloat(Double.pi * 2))) }) }, completion:{ _ in complete() }) } } 

To call anywhere in the app:

 view.rotate() { /*do after*/ } 
0
Jul 20 '19 at 13:11
source share



All Articles