Question

I am trying to animate the blinking between two images on an imageview to draw the user's attention to an indicator to noticing the change. I would like the image to only blink one or two times before remaining on the new image. The timeline of events would be something like the following:

  1. Old image has been set and is waiting for a particular event to occur.
  2. Event occurs and blinking animation begins. New image is displayed for 0.25 seconds.
  3. Old image is displayed for 0.25 seconds.
  4. New image is displayed for 0.25 seconds. Animation ends.

Due to the short timeframe of the animation I was under the impression that there was a way to do this without using a NSTimer. My current attempt is below

+(void)blinkImage:(UIImageView*)view toNewImage:(UIImage*)newImage numOfTimes:(int)numOfTimes{

    float waitInterval = 0.25;
    float totalWaitTime = 0.0;
    UIImage *oldImage = view.image;

    view.image = newImage;
    totalWaitTime = totalWaitTime+waitInterval;

    for (int times = 0; times<numOfTimes; times++) {
        [UIView beginAnimations:@"setOld" context:nil];
        [UIView setAnimationDelay:totalWaitTime];
        [UIView setAnimationDuration:waitInterval];
        view.image = oldImage;
        totalWaitTime = totalWaitTime+waitInterval;
        NSLog(@"SetOld; waitTime %f", totalWaitTime);
        [UIView commitAnimations];

        [UIView beginAnimations:@"setNew" context:nil];
        [UIView setAnimationDelay:totalWaitTime];
        [UIView setAnimationDuration:waitInterval];
        view.image = newImage;
        totalWaitTime = totalWaitTime+waitInterval;
        NSLog(@"SetNew; waitTime %f", totalWaitTime);
        [UIView commitAnimations];
    }
}

However, this did not yield the expected results. With the above code, the view simply changed to the new image and did nothing more.

Any help would be greatly appreciated. Thanks in advance!

Was it helpful?

Solution

here put this one line bellow setAnimationDuration

[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:yourView cache:YES];

here different type of Animation.. also you can use bellow another way for animation

#define kAnimationKey @"transitionViewAnimation"

CATransition *animation = [CATransition animation];
    [animation setDelegate:self];   
    [animation setType:kCATransitionFromBottom];
    [animation setDuration:1.0];
    [animation setTimingFunction:[CAMediaTimingFunction functionWithName:
                                  kCAMediaTimingFunctionEaseInEaseOut]];
    [[yourView layer] addAnimation:animation forKey:kAnimationKey];

OTHER TIPS

Why not just use the UIImageView's built in animations?

Animating Images
  animationImages  (property)
  highlightedAnimationImages  (property)
  animationDuration  (property)
  animationRepeatCount  (property)
– startAnimating
– stopAnimating
– isAnimating

Or you could always just set the highlighted image and switch that instead of the entire image

- (id)initWithImage:(UIImage *)image highlightedImage:(UIImage *)highlightedImage
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top