wat i want is to show an image in image view and fade the image after 5 seconds and show the next image .the images are stored in an array objects...i was able to make a su
Here's what worked for me when I wanted to animate a set of messages as images to the user. No matter how many images you have this code will rotate through them.
[self.messageBottom = [[UIImageView alloc] initWithFrame:CGRectMake(289, 262, 171, 38)];
[self addSubview:messageBottom];
[messageBottom setAlpha:0.0];
[messageBottom release];
self.messageTop = [[UIImageView alloc] initWithFrame:CGRectMake(289, 262, 171, 38)];
[self addSubview:messageTop];
[messageTop setAlpha:1.0];
[messageTop release];
self.messageImgsArray =[NSArray arrayWithObjects:
[UIImage imageNamed:@"image1.png"],
[UIImage imageNamed:@"image2.png"],
[UIImage imageNamed:@"image3.png"],
[UIImage imageNamed:@"image4.png"],
[UIImage imageNamed:@"image5.png"],nil];
topIndex = 0, bottomIndex = 1;
[messageTop setImage:[messageImgsArray objectAtIndex:topIndex]];
//The timers time interval is the imageViews animation duration devided by the number of images in the animationImages array. 20/5 = 4
NSTimer *timer = [NSTimer timerWithTimeInterval:2.0
target:self
selector:@selector(onTimer)
userInfo:nil
repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];
}
-(void)onTimer{
[UIView animateWithDuration:1 animations:^{
//set the image of the image that is not currently visible
if (messageTop.alpha == 0) {
[messageTop setImage:[messageImgsArray objectAtIndex:topIndex]];
}
if (messageBottom.alpha == 0) {
[messageBottom setImage:[messageImgsArray objectAtIndex:bottomIndex]];
}
//make sure the images rotate
[messageTop setAlpha:messageTop.alpha == 0 ? 1 : 0];
[messageBottom setAlpha:messageBottom.alpha == 0 ? 1 : 0];
//make sure the images play in a loop
topIndex = topIndex < [messageImgsArray count]-1 ? bottomIndex+1 : 0;
bottomIndex = bottomIndex < [messageImgsArray count]-1 ? bottomIndex+1 : 0;
}];
}