Trying to change a UIImageView each 10 seconds

情到浓时终转凉″ 提交于 2019-12-11 12:17:47

问题


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?


回答1:


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..




回答2:


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




回答3:


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
}


来源:https://stackoverflow.com/questions/10371100/trying-to-change-a-uiimageview-each-10-seconds

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!