julesmummdry
julesmummdry

Reputation: 251

Showing every 5 seconds a new UIImageView with the help of NSTimer and UIView Animation?

I am trying to create a Calibration View! I have 12 calibration points as UIImageViews. Now I want to show every point for 5 seconds. So the whole calibration time is 1 minute. The Alpha of the ImageViews is set to 0.0! In the UI Animation I want to set the Alpha to 1.0 for one point after an other, for each point only for 5 seconds.
So far I did this (see the code)! But this animates me (fades in) after 5 seconds for 5 seconds all 12 Points at once!How can I solve this for one after another with a NSTimer and UI Animation? Thanks

-(id)init{
      _calibrationViewArray = [[NSMutableArray alloc]init];
      _calibrationPoint1 = [[UIImageView alloc]initWithFrame:(CGRectMake(115.5,113.0,25.0,25.0))];
      _calibrationPoint1.backgroundColor = [UIColor redColor];
      _calibrationPoint1.alpha = 0.0;
      [self addSubview:_calibrationPoint1];
      [_calibrationViewArray addObject:_calibrationPoint1];

      [NSTimer scheduledTimerWithTimeInterval:5.0 target:self selector:@selector(onTimer) userInfo:nil repeats:YES];
}

-(void)onTimer{

    for (int i = 0; i < [_calibrationViewArray count]; i++) {

        UIImageView* currentCalibrationPoint = [_calibrationViewArray objectAtIndex:i];

        [UIView beginAnimations:@"Calibration" context:nil];
        [UIView setAnimationDuration:5.0];

        // Make the animatable changes.
        currentCalibrationPoint.alpha = 1.0;


        // Commit the changes and perform the animation.
        [UIView commitAnimations];
    }

}

Upvotes: 0

Views: 262

Answers (3)

Wain
Wain

Reputation: 119031

In your onTimer method you shouldn't be looping over all of the image views, you should have a currentIndex or similar variable so you can just get the next image view and run an animation on it alone.

Upvotes: 1

Peter Sarnowski
Peter Sarnowski

Reputation: 11970

Do not use timer at all.

Use the:

  [UIView animateWithDuration: animations: completion:]

method.

In the animation block fade one of the dots, then in completion block run the animation again.

Upvotes: 0

Ra&#250;l Ju&#225;rez
Ra&#250;l Ju&#225;rez

Reputation: 2159

Declare a variable in your class to be:

int cont;

Initialize variable when is needed:

cont=0;

Change your code to this:

-(void)onTimer{

    //Loop is not needed, with the timer and counter is enough


    UIImageView* currentCalibrationPoint = [_calibrationViewArray objectAtIndex:cont];

    [UIView beginAnimations:@"Calibration" context:nil];
    [UIView setAnimationDuration:5.0];

    // Make the animatable changes.
    currentCalibrationPoint.alpha = 1.0;


    // Commit the changes and perform the animation.
    [UIView commitAnimations];

    if(cont>0){
         //Hide previous view (here you can make another animation, instead of changing alpha right away)
         [_calibrationViewArray objectAtIndex:cont-1].alpha = 0.0;
    }
    cont++;//+1

}

Upvotes: 2

Related Questions