一个接一个地运行动画失败

时间:2012-02-10 22:45:12

标签: iphone ios xcode ipad

我正在尝试动画图像以便在选中时左右旋转,基本上是让用户触摸他们正在触摸的对象。

我找到了一些动画代码:

- (void)rotateImage:(UIImageView *)image duration:(NSTimeInterval)duration 
              curve:(int)curve degrees:(CGFloat)degrees delay:(CGFloat)delay
{
    // Setup the animation
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDelay:delay];
    [UIView setAnimationDuration:duration];
    [UIView setAnimationCurve:curve];
    [UIView setAnimationBeginsFromCurrentState:YES];

    // The transform matrix
    CGAffineTransform transform = 
    CGAffineTransformMakeRotation(DEGREES_TO_RADIANS(degrees));
    image.transform = transform;

    // Commit the changes
    [UIView commitAnimations];
}

然而,当我尝试运行两个动画时,只有最后一个动画才有效。即使有适当的延迟,也只会显示第二个动画:

[self rotateImage:self duration:.5 
                curve:UIViewAnimationCurveEaseIn degrees:60 delay:0];
    [self rotateImage:self duration:.5 
                curve:UIViewAnimationCurveEaseIn degrees:-60 delay:5];

如何创建动画以使其向左旋转,然后向右旋转?

3 个答案:

答案 0 :(得分:4)

[UIView animateWithDuration:0.2 animations:^{
// animation left
    [UIView setAnimationDelay:10];

    } completion:^(BOOL finished){

[UIView animateWithDuration:0.2 animations:^{
       // animation right
    } completion:^(BOOL finished){
       // done
    }];

    }];

在这里找到了

https://developer.apple.com/library/content/featuredarticles/Short_Practical_Guide_Blocks/

答案 1 :(得分:1)

[UIView animateWithDuration:0.5f delay:0.0f options:UIViewAnimationOptionCurveEaseIn animations:^{
    self.view.transform = CGAffineTransformMakeRotation(DEGREES_TO_RADIANS(60));
} completion:^(BOOL finished){
    [UIView animateWithDuration:0.5f delay:5.0f options:UIViewAnimationOptionCurveEaseIn
      animations:^{
        self.view.transform = CGAffineTransformMakeRotation(DEGREES_TO_RADIANS(-60));
      }
      completion:^(BOOL finished){}];
}];

答案 2 :(得分:0)

动画块肯定是要走的路。这应该复制你要做的事情,包括宽松。假设您从视图控制器调用,但如果您将此代码放入UIView或UIImageView,则只需将self.view替换为self。

[UIView animateWithDuration:0.5f delay:0.0f options:UIViewAnimationOptionCurveEaseIn animations:^{
    self.view.transform = CGAffineTransformMakeRotation(DEGREES_TO_RADIANS(60));
} completion:^(BOOL finished){}];

[UIView animateWithDuration:0.5f delay:5.0f options:UIViewAnimationOptionCurveEaseIn animations:^{
    self.view.transform = CGAffineTransformMakeRotation(DEGREES_TO_RADIANS(-60));
} completion:^(BOOL finished){}];