Home Ask Login Register

Developers Planet

Your answer is one click away!

Syed Qamar Abbas February 2016

How to apply 2 animations on a single subview one after other iOS

i have a button representing a fav button. i want to animate it upward with 1.0 sec delay and then downward in 0.2 sec delay when some one click on it. i have made two different methods to do upward and downward animation. But when i actually run the code both animations commit at the same time which shows a reverse order. i want to animate the upward animation first and then downward animations. here is the code.

-(void)starButtonUpAnimation{
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:2.0];

    CGRect frame = self.starButton.frame;
    frame.origin.y = frame.origin.y - frame.size.height;
    self.starButton.frame = frame;

    [UIView commitAnimations];

}

-(void)starButtonDownAnimation{

    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.1];

    CGRect frame1 = self.starButton.frame;
    frame1.origin.y = frame1.origin.y + frame1.size.height;
    self.starButton.frame = frame1;
    [UIView commitAnimations];

}

Button Action method where i am invoking above methods.

- (IBAction)clickedStarButton:(id)sender {
    [self starButtonUpAnimation];
    [self changeStarButtonImage];

    [self starButtonDownAnimation];
}

Answers


David Yang Liu February 2016

some thing like this?

-(void)starButtonUpAnimation{

  CGRect frame = self.starButton.frame;
  frame.origin.y = frame.origin.y - frame.size.height;
  self.starButton.frame = frame;
}


-(void)starButtonDownAnimation{


  CGRect frame1 = self.starButton.frame;
  frame1.origin.y = frame1.origin.y + frame1.size.height;
  self.starButton.frame = frame1;

  }

- (IBAction)clickedStarButton:(id)sender {

  [UIView animateWithDuration:2.0 animations:^{
      [self starButtonUpAnimation];
   } completion:^(BOOL finished) {
     [self changeStarButtonImage];
     [UIView animateWithDuration:0.1 animations:^{
        [self starButtonDownAnimation];
     } completion:^(BOOL finished) {
        // clean up 
     }];
   }];
}

Post Status

Asked in February 2016
Viewed 3,665 times
Voted 14
Answered 1 times

Search




Leave an answer


Quote of the day: live life