0

I am trying to set a delay for my animation, so once it appears and then disappears, I want to wait a certain amount of seconds for it to reappear. I have tried placing it in multiple spots throughout my code, but it was all the same result.

 - (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;
        }];
    }];
}
user3839056
  • 47
  • 1
  • 8

1 Answers1

0

setAnimationDelay should use in beginAnimation and commitAnimation block, and it was old way to do animation in iOS. In your case try this:

- (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];
}];

}

LongNV
  • 892
  • 9
  • 21
  • it says scripted value is not an array – user3839056 Jul 29 '14 at 02:16
  • Sorry, I don't get what is your error? scripted value is not an array? which line of code show that error? – LongNV Jul 29 '14 at 03:04
  • Also, I noticed that you set duration of animation = 0.0f. Therefore, it won't do animated effect. It is likely your button will appear and disappear immediately. – LongNV Jul 29 '14 at 03:07
  • and i would like it to appear and then disappear with it staying on the screen for a second or two. – user3839056 Jul 29 '14 at 04:07
  • So you need to set fadeDuration = 2.0f instead of 0.0. Also, maximum of alpha value is 1.0, set it to 4.0 make no sense. Current delay value is 9 seconds, it might be too long. – LongNV Jul 29 '14 at 06:28