0

我在 while 循环中有一个动作。

while (y < 26)
{
    [NSTimer scheduledTimerWithTimeInterval:.06 target:self selector:@selector(self) userInfo:nil repeats:NO];

    self.ball.center = CGPointMake(_ball.center.x + 0, _ball.center.y + 10);

    y = y + 1;
}

单击按钮时,我有一个球的图像,我希望球开始向下移动,但我需要它等待 0.5 秒然后再次移动,否则它会立即向下移动。我试过sleep(.5)and [NSTimer scheduledTimerWithTimeInterval:.05 target:self selector:@selector(self) userInfo:nil repeats:NO];,但他们没有工作。

4

2 回答 2

0

在 iOS 中,您不会手动制作动画(在这种情况下,您会尝试使用 while 循环来制作动画)。相反,您要求操作系统为您制作动画。以下是实现您想要的球运动的超级简单选项:

[UIView animateWithDuration:1.0 animations:^{

    self.ball.center = CGPointMake(self.ball.center.x + 0, self.ball.center.y + 10);
}];

请用我在这里给你的代码替换你的代码(包括while循环)。

希望这可以帮助!

于 2013-10-30T18:03:26.583 回答
0

您可以为球的移动设置动画,如下所示:

/// declare block for animating
@property (nonatomic, strong) void(^animationCompletion)(BOOL);

CGFloat static kAnimationDuration = 0.3;
...

/// you declare here next position
CGPoint myPoint = CGPointMake(x, y)

/// create weak of self to not make retain cycle
ClassName __weak weakSelf = self;

/// define completion block declared above.
/// this block is called when one step is done. look below this block.
self.animationCompletion = ^(BOOL finished) {
    myPoint = CGPointMake... //next point to move to
    if (myPoint == lastPoint/*or other check when to finish moving*/) {
        weakSelf.animationCompletion = nil; /// nil your block to not call this again
    }
    /// call next animation with next step
    UIView animateWithDuration:kAnimationDuration animations:^{
        // animate to next point
    } completion:weakSelf.animationCompletion; /// and call block itself to make some sort of loop. 
}

/// here is first call of above block. you make first step here.
UIView animateWithDuration:kAnimationDuration animations:^{
 // animate to next point
} completion:self.animationCompletion;

它在工作吗?是的。查看适用于 iOS 的 Arcade Balls 应用程序。我在那里用过。

于 2013-10-30T19:31:30.840 回答