Well, I have a UIImageview in one of my views. I would like to change the image every 10 seconds, but I don't have any idea of how can I make it.
Any suggestion?
Well, I have a UIImageview in one of my views. I would like to change the image every 10 seconds, but I don't have any idea of how can I make it.
Any suggestion?
Create array of images first.
NSArray *images = [NSArray arrayWithObjects:[UIimage imagenamed:@"image1.png"],[UIimage imagenamed:@"image2.png"],[UIimage imagenamed:@"image3.png"],[UIimage imagenamed:@"image4.png"]]];
Then set array to UIImageViews "animationImages"property
imageView.animationImages = images;
Set time for animation.
imageView.animationDuration = 10;
Start animation..
[imageView startAnimating];
To stop animation just do this
[imageView stopAnimating];
Pretty simple..
You will need to use timer.
NSTimer *timer = [NSTimer timerWithTimeInterval:10 target:self selector:@selector(changeImage) userInfo:nil repeats:true];
and implement method for changing image:
- (void) changeImage {
imageView.image = newImage;
}
You can put this timer into ViewController containing your ImageView or you you can subclass ImageView and let it manage its image itself (especially useful if there are more independent imageview that need to change its image
For Swift 4 place Timer.scheduledTimer
call in viewWillAppear
or viewDidAppear
:
Timer.scheduledTimer(timeInterval: 10,
target: self,
selector: #selector(self.changeView),
userInfo: nil,
repeats: true)
and for changeView function change image
@objc func changeView() {
//change image in this function
}