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

发布于 2025-01-05 01:30:24 字数 1056 浏览 0 评论 0原文

我正在尝试对图像进行动画处理,使其在选择时左右旋转,基本上是为了让用户触摸他们正在触摸的对象。

我找到了一些动画代码:

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

我可以做什么来创建动画,使其向左旋转,然后向右旋转?

I'm trying to animate an image to rotate left and right when selected, basically to let the user which the object they're touching.

I found some code for animating:

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

However, when I try to run two animations, only the last one ever works. Even with a proper delay, only the second animation will show:

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

What can I do to create the animation so it rotates left, then rotates right?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

涫野音 2025-01-12 01:30:24
[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/

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

    } completion:^(BOOL finished){

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

    }];

found it here

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

-小熊_ 2025-01-12 01:30:24
[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){}];
}];
[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){}];
}];
野稚 2025-01-12 01:30:24

动画块绝对是最佳选择。这应该复制您正在尝试做的事情,包括宽松。这假设您从视图控制器进行调用,但如果您将此代码放入 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){}];

Animation blocks are definitely the way to go. This should replicate what you're trying to do, including the easing. This assumes your calling from a view controller, but if your putting this code into a UIView or UIImageView, then just replace self.view with 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){}];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文