Reputation: 931
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:
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!
Upvotes: 2
Views: 1433
Reputation: 18551
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
Upvotes: 0
Reputation: 20541
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];
Upvotes: 1