为按钮设置延迟动画 - iPhone

发布于 2024-09-27 00:00:39 字数 948 浏览 1 评论 0原文

我试图让按钮移动到一个坐标,暂停,移动到另一个坐标,暂停,然后再次移动。然后该过程应该无限重复。我现在所拥有的只是迈出了最后一步。

这就是我到目前为止所拥有的(“mover”是 UIButton 名称):

- (void) firstAnimation {

        [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationDuration:0.5];
        [UIView setAnimationDelay:5];
        [UIView setAnimationRepeatCount:-1];
        [UIView setAnimationRepeatAutoreverses:NO];

        CGPoint pos = mover.center;
        pos.y = 200.f;
        pos.x = 169.f;
        mover.center = pos;

        [NSTimer timerWithTimeInterval:1.0 target:self selector:@selector(firstAnimation:) userInfo:nil repeats:NO];
        pos.y = 100.f;
        pos.x = 179.f;
        mover.center = pos;

        [NSTimer timerWithTimeInterval:1.0 target:self selector:@selector(firstAnimation:) userInfo:nil repeats:NO];
        pos.y = 160.f;
        pos.x = 129.f;
        mover.center = pos;

        [UIView commitAnimations];

}

感谢您的帮助

I'm trying to have a button move to a co-ordinates, pause, move to another co-orinates, pause, then move again. the process should then repeat infinitely. what I have right now just moves the last step.

this is what I have so far ('mover' is the UIButton name):

- (void) firstAnimation {

        [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationDuration:0.5];
        [UIView setAnimationDelay:5];
        [UIView setAnimationRepeatCount:-1];
        [UIView setAnimationRepeatAutoreverses:NO];

        CGPoint pos = mover.center;
        pos.y = 200.f;
        pos.x = 169.f;
        mover.center = pos;

        [NSTimer timerWithTimeInterval:1.0 target:self selector:@selector(firstAnimation:) userInfo:nil repeats:NO];
        pos.y = 100.f;
        pos.x = 179.f;
        mover.center = pos;

        [NSTimer timerWithTimeInterval:1.0 target:self selector:@selector(firstAnimation:) userInfo:nil repeats:NO];
        pos.y = 160.f;
        pos.x = 129.f;
        mover.center = pos;

        [UIView commitAnimations];

}

thanks for any help

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

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

发布评论

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

评论(3

暖阳 2024-10-04 00:00:39

您不需要使用计时器,只需将动画委托设置为 self 并实现animationfinished 方法即可。

在这里查看我的答案:

无限循环的动画

You don't need to use timers, just set the animation delegate to self and implement the animationfinished method.

See my answer here:

animations on infinite loop

轻拂→两袖风尘 2024-10-04 00:00:39

如果您专门针对 iOS4 进行发布,我强烈建议您使用块和以下方法:

[UIView animateWithDuration:kAnimationDuration
                      delay:kAnimationDelay
                    options:UIViewAnimationCurveEaseInOut
                 animations:^ {
                     // your animations here.
                 }
                 completion:^(BOOL finished) {
                     // what you want to do upon animation completion here.
                 }];

请注意,在完成块中您也可以提示另一个动画。事实上,您可以将三个动画块存储为块变量,然后将它们简单地传递给上述方法,依次执行,直到第三个动画块完成。然后重新启动该过程即可!

积木是你的朋友。

If you're shipping for iOS4 exclusively, I thoroughly recommend using blocks and the following method:

[UIView animateWithDuration:kAnimationDuration
                      delay:kAnimationDelay
                    options:UIViewAnimationCurveEaseInOut
                 animations:^ {
                     // your animations here.
                 }
                 completion:^(BOOL finished) {
                     // what you want to do upon animation completion here.
                 }];

Note that in the completion block you can just as well cue up another animation. In fact, you could store the three animation blocks as block variables and then simply pass them in to the above method to be executed, one after another, until the third completes. Then just restart the process!

Blocks are your friends.

冷了相思 2024-10-04 00:00:39

您更适合使用 CoreAnimation 关键帧 并将 repeatCount = HUGE_VALF 设置为永远循环

You would be better suited to use CoreAnimation keyframes and setting repeatCount = HUGE_VALF to loop forever

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文