如何延迟方法/动画



所以我有一个简单的动画。它只是将一堆UILabels从屏幕的右上角俯冲到它们设置的坐标。我希望每个标签之间有一点延迟,所以它们一个接一个地涓涓细流。现在它太快了:

-(void)drawLabels
{

for(int i=0; i<[self.onScreenLabels count]; i++)
{
    UILabel *label = self.onScreenLabels[i];
    int x = label.frame.origin.x;
    int y= label.frame.origin.y;
    label.center=CGPointMake(320, 0);
    [self.view addSubview:label];
    [UIView animateWithDuration:0.3 animations:^{
        label.center=CGPointMake(x, y);

    }];
    NSDate *future = [NSDate dateWithTimeIntervalSinceNow: 0.5 ];
    [NSThread sleepUntilDate:future];

}
}

我想在屏幕上绘制每个标签后有一个延迟,你可以看到上面我尝试使用 NSDate 和 NSThread,但它似乎没有任何区别。有什么想法吗?谢谢

另一种使用 animateWithDuration:delay 的方法:

CGFloat delay = 0.0f;
for(int i=0; i<[self.onScreenLabels count]; i++)
{
    UILabel *label = self.onScreenLabels[i];
    int x = label.frame.origin.x;
    int y = label.frame.origin.y;
    label.center=CGPointMake(320, 0);
    [self.view addSubview:label];
    [UIView animateWithDuration:0.3 delay:delay options:0 animations:^{
        label.center=CGPointMake(x, y);
    } completion:^(BOOL finished){
    }];
    delay += 0.5f;                  // add 1/2 second delay to each label (0, 0.5, 1.0, 1.5)
}

怎么回事

[self performSelector:@selector(moveLabel) withObject:label afterDelay:0.5];

尝试使用NSTimer类。举个例子:

[NSTimer scheduledTimerWithTimeInterval:0.5
target:self
selector:@selector(drawLabels)
userInfo:nil
repeats:YES];

这将创建一个计时器,该计时器将在调用 drawLabels 方法的此类中每 0.5 秒触发一次。您可能还想编辑您的方法

最新更新