Syed Qamar Abbas
Syed Qamar Abbas

Reputation: 3677

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];
}

Upvotes: 0

Views: 49

Answers (1)

David Yang Liu
David Yang Liu

Reputation: 1170

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 
     }];
   }];
}

Upvotes: 3

Related Questions