用NSTimer改变UIImage? [英] Changing UIImage with NSTimer?
问题描述
所以我这个项目的最终目标是拥有一些图像和一个 UIImageview
并让图像每10秒更换一次,我目前拥有 NSTimer
设置和图像数组但是我不知道如何同时使用 NSTimer
和UIImage视图。
到目前为止,这就是我所拥有的:
So my ultimate goal for this project is to have a couple images and a UIImageview
and to have the image change every 10 seconds, I currently have the NSTimer
set up and the array of images however I don't know how to utilize both the NSTimer
and UIImage view together.
So far this is what I have:
在我的.h:
#import <UIKit/UIKit.h>
@interface HomeViewController : UIViewController {
NSTimer *myTimer;
IBOutlet UIImageView *imageview;
}
@property (nonatomic, strong) NSArray *images;
在我的.m
- (void)viewDidLoad
{
[super viewDidLoad];
myTimer = [NSTimer scheduledTimerWithTimeInterval:10.00 target:self selector:@selector(changeImage) userInfo:nil repeats:YES];
}
- (void)changeImage
{
_images = @[@"Background 3.png", @"Background.png", @"Background 2.png", @"Background 4.png", @"Background 5.png"];
//stuck here, how do I change the image using the NStimer interval????
}
所以现在你已经看过代码了,我是无法弄清楚changeImage方法应该包含哪些内容。正好回顾一下我希望数组中的5个图像在UIImage视图中每10秒循环一次。我已经尝试了很多选项,但我无法确定它。任何帮助将不胜感激。
So now that you've seen the code, I am having trouble figuring out what should go inside the changeImage method. Just in review what I would like is for the 5 images in the array to cycle every 10 seconds in the UIImage view. I've tried lots of options I just can't fiqure it out. Any help would be greatly appreciated.
推荐答案
您可以使用以下代码执行此操作。
You can simply do this using the following code.
- (void)viewDidLoad
{
[super viewDidLoad];
_images = @[@"Background 3.png", @"Background.png", @"Background 2.png", @"Background 4.png", @"Background 5.png"];
myTimer = [NSTimer scheduledTimerWithTimeInterval:10.00 target:self selector:@selector(changeImage) userInfo:nil repeats:YES];
}
- (void)changeImage
{
static int counter = 0;
if([_images count] == counter+1)
{
counter = 0;
}
imageview.image = [UIImage imageNamed:[_images objectAtIndex:counter];
counter++;
}
这篇关于用NSTimer改变UIImage?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!