动画延迟

时间:2014-07-29 01:23:27

标签: xcode delay uiviewanimation

我正在尝试为我的动画设置延迟,所以一旦它出现然后消失,我想等待一定的秒数再次出现。我尝试将它放在我的代码中的多个位置,但结果完全相同。

 - (void) startRedDot {
    redDotTimer = [NSTimer scheduledTimerWithTimeInterval:1.5
                                                   target:self
                                                 selector:@selector(moveButtonWithAnimation)
                                                 userInfo:nil
                                                  repeats:YES];
    [redDotTimer fire];
}

-(void) moveButtonRandomly {
    CGSize limits;
    CGPoint newPosition;

    // Get limits
    limits.width = gameView.frame.size.width - redButton.frame.size.width;
    limits.height = gameView.frame.size.height - redButton.frame.size.height;

    // Calculate new Position
    newPosition = (CGPoint){ limits.width * drand48(), limits.height * drand48() };

    // Set new frame
    redButton.frame = (CGRect){ newPosition, redButton.frame.size };
}

- (void) moveButtonWithAnimation {
    CGFloat fadeDurration;
    if ( !redButton )
        return; // only invoke the button if it exists
    fadeDurration = 0.0f;


    //Fade Out
    [UIView animateWithDuration: fadeDurration animations:^{
        redButton.alpha = 0.0f;

    } completion:^(BOOL finished) {
        // Move the button while it is not visible
        [self moveButtonRandomly];

        [UIView setAnimationDelay:9.0];

        // Fade in
        [UIView animateWithDuration: fadeDurration animations:^{
            redButton.alpha = 4.0f;
        }];
    }];
}

1 个答案:

答案 0 :(得分:0)

setAnimationDelay应该在beginAnimation和commitAnimation块中使用,这是在iOS中做动画的老方法。在你的情况下试试这个:

- (void) moveButtonWithAnimation {
CGFloat fadeDurration;
if ( !redButton )
    return; // only invoke the button if it exists
fadeDurration = 2.0f;


//Fade Out
[UIView animateWithDuration: fadeDurration animations:^{
    redButton.alpha = 0.0f;

} completion:^(BOOL finished) {
    // Move the button while it is not visible
    [self moveButtonRandomly];
    [UIView animateWithDuration:fadeDurration delay:2.0 options:0 animations:^{
        redButton.alpha = 1.0f;
    } completion:nil];
}];

}