如何在单个子视图上一个接一个地应用 2 个动画



我有一个代表收藏夹按钮的按钮。我想以 1.0 秒的延迟向上制作动画,然后在有人点击它时以 0.2 秒的延迟向下动画。我做了两种不同的方法来做向上和向下的动画。但是当我实际运行代码时,两个动画同时提交,这显示了相反的顺序。我想先对向上动画进行动画处理,然后再对向下动画进行动画处理。这是代码。

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

我调用上述方法的按钮操作方法。

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

这样的事情吗?

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

相关内容

最新更新