how to use array in uiview animation - ios

I created a UIView Animation, my array contains 10 images. I want to move one place to another place when button is pressed. Below code is working but problem is all 10 images is moving.
-(IBAction)moveImageAtIndex:(int)index {
UIButton *buttonCelebrate = [redAppleArray objectAtIndex:j];
UIButton *buttonRefer = [reffButtonArray objectAtIndex:j];
currentView = buttonCelebrate;
CGRect frame = currentView.frame;
CGRect frame1 = buttonRefer.frame;
frame.origin.x = frame1.origin.x;
frame.origin.y = frame1.origin.y;
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration: 2.0];
[UIView setAnimationDelegate:self];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
currentView.frame = frame;
[UIView animateWithDuration:2.0 animations:^
{
[currentView setTransform:CGAffineTransformIdentity];
}completion:^(BOOL finished) {
if (finished) {
if(index != [redAppleArray count] - 1) {
[self moveImageAtIndex:index+1];
}
}
}];
[UIView commitAnimations];
}
Expected Output is: I want to move one by one instead of all.
screenshot

You have to wait until the current animation has finished, then you can start the next animation. A simple solution is to use recursion:
-(void)moveImageAtIndex:(int)index {
[UIView animateWithDuration:2.0 animations:^{
//Do animation
}completion:^(BOOL finished) {
if (finished) {
if(index != [redAppleArray count] - 1) {
[self moveImageAtIndex:index + 1];
}
}
}];
}

Remove the code from loop and keep in the method which is called by button then by pressing the button your images in array will be moved one by one according to user clicks.
declare i as int in the .h file and make i value as 0 in viewDidLoad and Don't forget to increment the i value in the end
use below code
-(void)viewDidLoad {
i=0;
[super viewDidLoad];
}
-(IBAction)methodName:(id)sender {
UIButton *buttonCelebrate = [redAppleArray objectAtIndex:index];
currentView = buttonCelebrate;
CGRect frame = currentView.frame;
CGRect frame1 = reffButton.frame;
frame.origin.x = frame1.origin.x;
frame.origin.y = frame1.origin.y;
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration: 2.0];
[UIView setAnimationDelegate:self];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
currentView.frame = frame;
[UIView animateWithDuration:2.0 animations:^
{
[currentView setTransform:CGAffineTransformIdentity];
}completion:^(BOOL finished) {
if (finished) {
}
}];
[UIView commitAnimations];
i++;
}
as per the Eric use below code
You have to wait until the current animation has finished, then you can start the next animation. A simple solution is to use recursion:
-(void)moveImageAtIndex:(int)index {
[UIView animateWithDuration:2.0 animations:^{
UIButton *buttonCelebrate = [redAppleArray objectAtIndex:i];
currentView = buttonCelebrate;
CGRect frame = currentView.frame;
CGRect frame1 = reffButton.frame;
frame.origin.x = frame1.origin.x;
frame.origin.y = frame1.origin.y;
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration: 2.0];
[UIView setAnimationDelegate:self];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
currentView.frame = frame;
[UIView animateWithDuration:2.0 animations:^
{
[currentView setTransform:CGAffineTransformIdentity];
}completion:^(BOOL finished) {
if (finished) {
if(index != [redAppleArray count] - 1) {
[self moveImageAtIndex:index + 1];
}
}
}];
[UIView commitAnimations];
}

The new solution is:
-(void)moveImageStartAtIndex:(int)index withHowMany:(int)howMany{
[UIView animateWithDuration:2.0 animations:^{
//Do animation
}completion:^(BOOL finished) {
if (finished) {
if(howMany > 1) {
[self moveImageStartAtIndex:index + 1 withHowMany:howMany - 1];
}
}
}];
}

Related

How to animate constraints one by one in IOS?

I have 6 views. I want to animate the constraints one by one, ie, i need the animation on second view only after first, then after second the third and so on. I have added the code in the completion handler of the animations, but it is not working. initial value of all the constraint is set to 300 in storyboard. I want to change it to 0 one by one. Now, only the first animation is working. This is what i have done.
layoutTopFirst.constant = 0.0;
[UIView animateWithDuration:1.0 animations:^{
[self.view layoutIfNeeded];
} completion:^(BOOL finished) {
self->layoutTopSecond.constant = 0.0;
[UIView animateWithDuration:1.0 animations:^{
[self.view layoutIfNeeded];
} completion:^(BOOL finished) {
self->layoutTopThird.constant = 0.0;
[UIView animateWithDuration:1.0 animations:^{
[self.view layoutIfNeeded];
} completion:^(BOOL finished) {
self->layoutTopFourth.constant = 0.0;
[UIView animateWithDuration:1.0 animations:^{
[self.view layoutIfNeeded];
} completion:^(BOOL finished) {
self->layoutTopFifth.constant = 0.0;
[UIView animateWithDuration:1.0 animations:^{
[self.view layoutIfNeeded];
} completion:^(BOOL finished) {
self->layoutTopSixth.constant = 0.0;
[UIView animateWithDuration:1.0 animations:^{
[self.view layoutIfNeeded];
} completion:^(BOOL finished) {
}];
}];
}];
}];
}];
}];
How to do animations one after another?
If only the first one is animating, make sure that
your other five constraint references all point to different vertical constraints ... if they were nil for example, you won't see any changes;
that they're all isActive;
that there aren't any other constraints that are preventing the updated constants from yielding changes.
For what it's worth, you might consider eliminating your tower of completion handlers with keyframe animation, e.g.
CGFloat relativeDuration = 1.0 / 6.0;
[UIView animateKeyframesWithDuration:6 delay:0 options:kNilOptions animations:^{
[UIView addKeyframeWithRelativeStartTime:0 relativeDuration:relativeDuration animations:^{
self.topConstraint1.constant = 0;
[self.view layoutIfNeeded];
}];
[UIView addKeyframeWithRelativeStartTime:1.0 * relativeDuration relativeDuration:relativeDuration animations:^{
self.topConstraint2.constant = 0;
[self.view layoutIfNeeded];
}];
[UIView addKeyframeWithRelativeStartTime:2.0 * relativeDuration relativeDuration:relativeDuration animations:^{
self.topConstraint3.constant = 0;
[self.view layoutIfNeeded];
}];
[UIView addKeyframeWithRelativeStartTime:3.0 * relativeDuration relativeDuration:relativeDuration animations:^{
self.topConstraint4.constant = 0;
[self.view layoutIfNeeded];
}];
[UIView addKeyframeWithRelativeStartTime:4.0 * relativeDuration relativeDuration:relativeDuration animations:^{
self.topConstraint5.constant = 0;
[self.view layoutIfNeeded];
}];
[UIView addKeyframeWithRelativeStartTime:5.0 * relativeDuration relativeDuration:relativeDuration animations:^{
self.topConstraint6.constant = 0;
[self.view layoutIfNeeded];
}];
} completion:nil];
Or, even simpler:
NSArray <NSLayoutConstraint *> *constraints = #[self.topConstraint1, self.topConstraint2, self.topConstraint3, self.topConstraint4, self.topConstraint5, self.topConstraint6];
CGFloat relativeDuration = 1.0 / (CGFloat) constraints.count;
[UIView animateKeyframesWithDuration:totalDuration delay:0 options:kNilOptions animations:^{
for (NSInteger i = 0; i < constraints.count; i++) {
[UIView addKeyframeWithRelativeStartTime: ((CGFloat) i) * relativeDuration relativeDuration:relativeDuration animations:^{
constraints[i].constant = 0;
[self.view layoutIfNeeded];
}];
}
} completion:nil];
That yields:
It would help if you showed all your code (how you setup the constraints, etc).
But, here is a quick example.
It creates 6 labels, with top constraints at 100. Tap the "Do Anim" button to animated them to Top: 0.0 ... tap again to animate back to 100:
#import "SequentialAnimViewController.h"
#interface SequentialAnimViewController () {
NSMutableArray *views;
NSMutableArray *topConstraints;
}
#end
#implementation SequentialAnimViewController
- (void)viewDidLoad {
[super viewDidLoad];
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
[button addTarget:self action:#selector(animViews) forControlEvents:UIControlEventTouchUpInside];
[button setTitle:#"Do Anim" forState:UIControlStateNormal];
button.backgroundColor = [UIColor colorWithWhite:0.9 alpha:1.0];
button.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:button];
[button.centerXAnchor constraintEqualToAnchor:[self.view centerXAnchor]].active = YES;
[button.centerYAnchor constraintEqualToAnchor:[self.view centerYAnchor]].active = YES;
[button.widthAnchor constraintEqualToConstant:200.0].active = YES;
views = [NSMutableArray new];
topConstraints = [NSMutableArray new];
CGFloat x = 40.0;
CGFloat w = 40.0;
UILayoutGuide *g = [self.view safeAreaLayoutGuide];
for (int i = 0; i < 6; i++) {
UILabel *v = [UILabel new];
v.backgroundColor = [UIColor blueColor];
v.textColor = [UIColor yellowColor];
v.textAlignment = NSTextAlignmentCenter;
v.text = [NSString stringWithFormat:#"%i", i];
v.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:v];
[v.widthAnchor constraintEqualToConstant:w].active = YES;
[v.heightAnchor constraintEqualToConstant:w].active = YES;
[v.leadingAnchor constraintEqualToAnchor:g.leadingAnchor constant:x].active = YES;
[topConstraints addObject:[v.topAnchor constraintEqualToAnchor:g.topAnchor constant:100.0]];
[views addObject:v];
x += w + 8;
}
[NSLayoutConstraint activateConstraints:topConstraints];
}
-(void)animViews {
__block int i = 0;
__block CGFloat newConstant = ((NSLayoutConstraint *)self->topConstraints[0]).constant == 0.0 ? 100.0 : 0.0;
((NSLayoutConstraint *)self->topConstraints[i++]).constant = newConstant;
[UIView animateWithDuration:1.0 animations:^{
[self.view layoutIfNeeded];
} completion:^(BOOL finished) {
((NSLayoutConstraint *)self->topConstraints[i++]).constant = newConstant;
[UIView animateWithDuration:1.0 animations:^{
[self.view layoutIfNeeded];
} completion:^(BOOL finished) {
((NSLayoutConstraint *)self->topConstraints[i++]).constant = newConstant;
[UIView animateWithDuration:1.0 animations:^{
[self.view layoutIfNeeded];
} completion:^(BOOL finished) {
((NSLayoutConstraint *)self->topConstraints[i++]).constant = newConstant;
[UIView animateWithDuration:1.0 animations:^{
[self.view layoutIfNeeded];
} completion:^(BOOL finished) {
((NSLayoutConstraint *)self->topConstraints[i++]).constant = newConstant;
[UIView animateWithDuration:1.0 animations:^{
[self.view layoutIfNeeded];
} completion:^(BOOL finished) {
((NSLayoutConstraint *)self->topConstraints[i++]).constant = newConstant;
[UIView animateWithDuration:1.0 animations:^{
[self.view layoutIfNeeded];
} completion:^(BOOL finished) {
NSLog(#"All Done!");
}];
}];
}];
}];
}];
}];
}
#end

UIPickerView Animate on and off screen behavior

This is a continuation of this question
CGRectMake : How To Calculate X and Y For UIPickerView Animation?
answered by https://stackoverflow.com/users/2315974/danypata
I have a picker view that I want to animate on and off screen on a button press and using the code in the previous question/answer I have got it to animate on screen the first time the button is pressed.
the second tome the button is pressed it just disappears and then the third time it is pressed it animates to the wrong place...please help
the code
if (self.pickerSort.hidden == NO) {
[UIView animateWithDuration:0.5 delay:0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
self.pickerSort.frame = CGRectMake(0,self.view.frame.size.height
+ self.pickerSort.frame.size.height,-self.pickerSort.frame.size.width,-self.pickerSort.frame.size.height);
}
completion:^(BOOL finished) {
}];
self.pickerSort.hidden = YES;
// [self performSelector:#selector(hidePicker) withObject:nil afterDelay:1];
} else if (self.pickerSort.hidden == YES) {
self.pickerSort.hidden = NO;
[UIView animateWithDuration:0.5 delay:0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
self.pickerSort.frame = CGRectMake(0 ,
self.view.frame.size.height
- self.pickerSort.frame.size.height,
self.pickerSort.frame.size.width,
self.pickerSort.frame.size.height);
}
completion:^(BOOL finished) {
}];
[self.view bringSubviewToFront:self.pickerSort];
Behavior in images - button press animates onto screen beautifully
Second Press it disappears with no animation
Third Press it animates to here
Any help would be great...functionality I am looking for is how to put the picker view back on an animation so the 3rd press is the same as the first
Please try to use the below code.
self.pickerSort.hidden = NO;
NSTimeInterval duration = 0.5;
[UIView animateWithDuration:duration
delay:0
options:UIViewAnimationOptionCurveLinear
animations:^{
CGRect pickerFrame = self.pickerSort.frame;
// currently picker is off the screen, show it.
if (pickerFrame.origin.y == self.view.frame.size.height) {
// set frame to show picker
pickerFrame.origin.y = self.view.frame.size.height - self.pickerSort.frame.size.height;
} else {
// set frame to hide picker
pickerFrame.origin.y = self.view.frame.size.height;
}
self.pickerSort.frame = pickerFrame;
}
completion:^(BOOL finished) {
self.pickerSort.hidden = YES;
}];
After playing around with this and learning the ins and outs of frames and CGRects, I achieved this by using the following code
if (pickerSort.hidden == YES) {
[UIView animateWithDuration:0.5 delay:0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
pickerSort.hidden = NO;
visualEffectView = [[UIVisualEffectView alloc] initWithEffect:blurEffect];
[self.tableStations addSubview:visualEffectView];
pickerSort.frame = CGRectMake(0,0,originalPickerFrame.size.width,originalPickerFrame.size.height);
visualEffectView.frame = CGRectMake(0,0,originalPickerFrame.size.width,originalPickerFrame.size.height - 64);
}
completion:^(BOOL finished) {
self.navigationController.navigationItem.leftBarButtonItem.enabled = NO;
}];
}
else
{
visualEffectView.hidden = YES;
[UIView animateWithDuration:0.5 delay:0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
pickerSort.frame = CGRectMake(0,-originalPickerFrame.size.height,0,0);
}
completion:^(BOOL finished) {
self.navigationController.navigationItem.leftBarButtonItem.enabled = YES;
pickerSort.hidden = YES;
}];
}
I set the original frame size in viewDidLoad like so
pickerSort = [[UIPickerView alloc]initWithFrame:CGRectMake(0.0, 0.0, self.view.frame.size.width, self.view.frame.size.height)];
originalPickerFrame = pickerSort.frame;
pickerSort.frame = CGRectMake(0,-originalPickerFrame.size.height,0, 0);
Hope this can help somebody in the future

iOS Views return to original locations when textView becomesFirstResponder

Hello and thank you in advance...
I am moving views frame location in animation blocks. After I move views, I am tapping a textView to allow for editing/input. When the textView is tapped, the views I moved previously pop back to their original (unmoved) frame locations.
I'm not sure what I am doing incorrectly here.
THIS IS HOW I SETUP THE DIFFERENT FRAME POSITIONS TO ANIMATE TO
//Books active and rest position will effect other elements (paper and paperTextView)
self.bookRestPosition = CGRectMake((self.view.frame.size.width * 0.10), (self.view.frame.size.height * 0.4), self.view.frame.size.width, self.view.frame.size.height);
self.bookActivePosition = CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y +100, self.view.frame.size.width, self.view.frame.size.height);
self.bookOpenPosition = CGRectMake(self.bookActivePosition.origin.x, self.bookActivePosition.origin.y, 20, self.bookActivePosition.size.height);
self.quillRestPosition = self.quill.frame;
self.quillActivePosition = CGRectMake(self.quill.frame.origin.x, self.view.frame.origin.y -200, self.quill.frame.size.width, self.quill.frame.size.height);
self.woodTableRestPosition = self.view.frame;
self.woodTableActivePosition = CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y -200, self.view.frame.size.width, self.view.frame.size.height +200);
self.paperRestPosition = self.bookRestPosition;
self.paperActivePosition = CGRectMake(self.bookOpenPosition.size.width, self.bookActivePosition.origin.y, self.bookActivePosition.size.width, self.bookActivePosition.size.height);
self.paperTextViewRestPosition = self.bookRestPosition;
self.paperTextViewActivePosition = self.paperActivePosition;
self.paperTextView.editable = NO;
self.paper.alpha = 0.0;
self.paperTextView.alpha = 0.0;
self.paper.frame = self.bookRestPosition;
self.paperTextView.frame = self.bookRestPosition;
THIS IS HOW I ANIMATE THEM
- (IBAction)onBookTapped:(UITapGestureRecognizer *)sender {
if (self.logCover.frame.origin.x == self.bookRestPosition.origin.x) {
[UIView animateWithDuration:1.0
delay:0.0
options:UIViewAnimationOptionCurveEaseInOut
animations:^{
[self activateLogAndViewsAssociated];
} completion:^(BOOL finished) {
NSLog(#"Book Slid up!");
}];
}
else if (self.logCover.frame.origin.x == 0){
[UIView animateWithDuration:1.0
delay:0.0
options:UIViewAnimationOptionCurveEaseInOut
animations:^{
[self everythingAtRestPosition];
} completion:^(BOOL finished) {
NSLog(#"Book Slid Down!");
}];
}
}
- (IBAction)swipeBookLeft:(UISwipeGestureRecognizer *)sender {
//if (self.logCoverImage.frame.origin.x == 0) {
self.paper.alpha = 1.0;
[UIView animateWithDuration:1.0
delay:0.0
options:UIViewAnimationOptionCurveEaseInOut
animations:^{
[self.logCover setFrame:self.bookOpenPosition];
self.editButton.alpha = 1.0;
self.editButton.enabled = YES;
} completion:^(BOOL finished) {
NSLog(#"Book Has Opened!");
//This must be here. The paperTextView is what the swipe right gesture is attached to
self.paperTextView.alpha = 1.0;
// self.logCoverImage.alpha = 0.0;
// self.quill.alpha = 0.0;
}];
//}
}
- (IBAction)onPaperSwipeRight:(UISwipeGestureRecognizer *)sender {
[UIView animateWithDuration:1.0
delay:0.0
options:UIViewAnimationOptionCurveEaseInOut
animations:^{
[self.logCover setFrame:self.bookActivePosition];
self.editButton.alpha = 0.0;
self.editButton.enabled = NO;
} completion:^(BOOL finished) {
NSLog(#"Book Has Closed!");
//self.paper.alpha = 0.0;
// [NSUserDefaults *log = [NSUserDefaults standardUserDefaults];
// [log setObject:self.paperTextView forKey:#"textLog"];
}];
}
- (IBAction)onBookSwipeDown:(UISwipeGestureRecognizer *)sender {
if (self.logCover.frame.origin.x == 0) {
[UIView animateWithDuration:1.0
delay:0.0
options:UIViewAnimationOptionCurveEaseInOut
animations:^{
[self everythingAtRestPosition];
} completion:^(BOOL finished) {
NSLog(#"Book Slid Down!");
}];
}
}
- (IBAction)onBookSwipeUp:(UISwipeGestureRecognizer *)sender {
if (self.logCover.frame.origin.x == self.bookRestPosition.origin.x) {
[UIView animateWithDuration:1.0
delay:0.0
options:UIViewAnimationOptionCurveEaseInOut
animations:^{
[self activateLogAndViewsAssociated];
} completion:^(BOOL finished) {
NSLog(#"Book Slid up!");
}];
}
}
- (void)animateAtStartup
{
self.logCover.frame = CGRectMake(self.view.frame.size.width, self.view.frame.size.height, self.bookRestPosition.size.width, self.bookRestPosition.size.height);
[UIView animateWithDuration:1.0
delay:0.0
options:UIViewAnimationOptionCurveEaseInOut
animations:^{
[self.logCover setFrame:self.bookRestPosition];
} completion:^(BOOL finished) {
NSLog(#"Book Slid Down!");
}];
}
AND THIS IS WHAT HAPPENS WHEN THE EDIT BUTTON IS PRESSED
- (IBAction)onEditButtonPressed:(id)sender
{
self.paperTextView.editable = YES;
[self.paperTextView becomeFirstResponder];
}

UiViewAnimation not call setAnimationDidStopSelector

I created this animation and I entered through the stop animation
[UIView setAnimationDidStopSelector:#selector (TermineAnimazioneGoPointAssignedWithDelay)] ;
When the animation part of the SetAnimationDidStop is not called ... Can you tell me why and where am I doing wrong ?
This is all the way animation :
-(void)setViewStatusGoPointassigned {
ViewgoPointAssignMessage = [[UIView alloc] initWithFrame:CGRectMake(115, 150, 100, 100) ];
ViewgoPointAssignMessage.backgroundColor = [UIColor colorWithRed:(77/255.0) green:(108/255.0) blue:(143/255.0) alpha:(1)];
[ViewgoPointAssignMessage.layer setCornerRadius:10.0f];
[ViewgoPointAssignMessage.layer setMasksToBounds:YES];
[UIView animateWithDuration:0.2 animations:^{
ViewgoPointAssignMessage.transform = CGAffineTransformMakeScale(1.05, 1.05);
ViewgoPointAssignMessage.alpha = 0.8; }
completion:^(BOOL finished){
[UIView animateWithDuration:2/15.0 animations:^{
ViewgoPointAssignMessage.transform = CGAffineTransformMakeScale(0.9, 0.9);
ViewgoPointAssignMessage.alpha = 0.9; }
completion:^(BOOL finished) {
[UIView animateWithDuration:1/7.5 animations:^{
ViewgoPointAssignMessage.transform = CGAffineTransformIdentity;
ViewgoPointAssignMessage.alpha = 1.0; } ];
} ];
} ];
[self.view addSubview:ViewgoPointAssignMessage];
[UIView setAnimationDidStopSelector:#selector(TermineAnimazioneGoPointAssignedWithDelay)];
}
-(void)TermineAnimazioneGoPointAssignedWithDelay {
[self performSelector:#selector(EliminazioneViewAfterDelay) withObject:self afterDelay:0.01];
}
-(void)EliminazioneViewAfterDelay {
CGRect endREct;
endREct = CGRectMake(350 , 0 , 330, 90);
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.005];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:#selector(EliminaAnimazione)];
ViewgoPointAssignMessage.frame = endREct;
[UIView commitAnimations];
}
- (void)EliminaAnimazione {
[ViewgoPointAssignMessage removeFromSuperview];
}
First of all, to answer your question.
You may try
[UIView animateWithDuration:(NSTimeInterval)duration animations:^(void)animations completion:^(BOOL finished)completion]
and call [self TermineAnimazioneGoPointAssignedWithDelay] in the completion.
Secondly, [UIView setAnimationDidStopSelector:#selector(TermineAnimazioneGoPointAssignedWithDelay)] doesn't work because according to the reference, setAnimationDidStopSelector must be called between calls to the beginAnimations:context: and commitAnimations methods. Since your code doesn't comply to this rule, the function does not work either.
Hopefully it explains! Good Luck!

UIViewanimation animationDidStop method

I have a 8 imagaviews that I have added to my view with size (0,0). Now what I want to do is to pull op each imageView after each other with a UIViewanimation. I want to do 2 animations. First the size should go to from (0,0) --> (105,85) and after that is finished the imageview size should go from (105,85) --> (93,75)
Now I have a method where I first place all my imageViews on the correct place.All the images have size (0,0) At the end I call the method startAnimating
-(void)startAnimating{
[self animageButton:[arrButtons objectAtIndex:0]];
}
Then the first view animation ( from (0,0) --> (105,85) )
-(void)animageButton:(UIImageView *)imgButton{
loopIndex++;
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:2.0];
[UIView setAnimationDelegate:self];
CGRect btnFrame = imgButton.frame;
btnFrame.size.width = 105;
btnFrame.size.height = 85;
[UIView transitionWithView:self.view
duration:0.5f
options:UIViewAnimationOptionCurveEaseIn
animations:^{
imgButton.frame = btnFrame;
}
completion:nil];
[UIView commitAnimations];
[self animageButton2:imgButton];
}
this calls the second method (from (105,85) --> (93,75))
-(void)animageButton2:(UIImageView *)imgButton{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:2.0];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector: #selector(animationDidStop:finished:context:)];
CGRect btnFrame2 = imgButton.frame;
btnFrame2.size.width = 93;
btnFrame2.size.height = 75;
[UIView transitionWithView:self.view
duration:0.5f
options:UIViewAnimationOptionCurveEaseIn
animations:^{
imgButton.frame = btnFrame2;
}
completion:nil];
[UIView commitAnimations];
}
Now at this point the first imageView should be animated. Now when the animation has finished. I to this.
-(void)animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context {
//do smth
if(loopIndex <= 7){
NSLog(#"called");
UIImageView *imgObj = [arrButtons objectAtIndex:loopIndex];
[self animageButton:imgObj];
}else{
NSLog(#"done");
return;
}
}
What it is doing at the moment it is animating all the images at the same time. But I want that the next imageview starts animating when the previous has done.
Can anyone help me with this ?
-(void)startAnimating
{
NSTimeInterval delay = 0.0;
for(UIImageView *imageView in arrButtons)
{
[self performSelector:#selector(animageButton:) withObject:imageView afterDelay:delay];
delay +=1.0;
}
}
-(void)animageButton:(UIImageView *)imgButton{
CGRect btnFrame = imgButton.frame;
btnFrame.size.width = 105;
btnFrame.size.height = 85;
[UIView animateWithDuration:0.5f animations:^{
imgButton.frame = btnFrame;
} completion:^(BOOL finished) {
[self animageButton2:imgButton];
}];
}
-(void)animageButton2:(UIImageView *)imgButton
{
CGRect btnFrame2 = imgButton.frame;
btnFrame2.size.width = 93;
btnFrame2.size.height = 75;
[UIView animateWithDuration:0.5f animations:^{
imgButton.frame = btnFrame2;
} completion:^(BOOL finished) {
}];
}
I have not tested the code. Let me know if there are any issues. Thanks.

Resources