1

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

这是我到目前为止所拥有的('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];

}

谢谢你的帮助

4

3 回答 3

3

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

在这里查看我的答案:

无限循环动画

于 2010-10-13T17:34:26.263 回答
1

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.

于 2010-10-13T18:00:16.423 回答
0

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

于 2010-10-13T17:48:25.493 回答