我想实现自动更改图片库。这是我在控制器中的代码。我有一个名为image的uiimageview。我想将图像数组链接到我的图像视图,让它在几秒钟后自动更改。我该怎么办?
- (void)viewDidLoad{
[super viewDidLoad];
[self performSelector:@selector(changeImage) withObject:nil afterDelay:2];
}
-(void)changeImage {
NSArray *images = [NSArray arrayWithObjects:[UIImage imageNamed:@"animated-fish-1.jpg"], [UIImage imageNamed:@"tumblr_7"], [UIImage imageNamed:@"th_nature_4.jpg"],nil];
image.animationImages = images;
// how to let the array of image load and link to the perform selector??
// what should i continue from here?
}
答案 0 :(得分:4)
将图像存储在ivar中
@interface YourClass : SomeSuperClass
@property (nonatomic, strong) NSArray *images;
@end
请勿在{{1}}中启动计时器,因为视图不会出现在屏幕上,所以没有任何意义 - 将其移至viewDidLoad
viewDidAppear:
答案 1 :(得分:1)
要使用计时器为图像设置动画,请尝试使用
- (void)viewDidLoad
{
[super viewDidLoad];
[NSTimer scheduledTimerWithTimeInterval:1
target:self
selector:@selector(animateImages)
userInfo:nil
repeats:YES];
imageCount = 1;
}
-(void)animateImages
{
imageCount = imageCount + 1;
NSString *imageName = [NSString stringWithFormat:@"image%i.png"];
[theImage setImage:[UIImage imageNamed:imageName]];
}
答案 2 :(得分:0)
您应该使用NSTimer,例如,您可以使用此方法:
此方法允许您定义要使用的选择器并每隔x秒重复
答案 3 :(得分:0)
试试这样, 采取一个全局变量索引
- (void)viewDidLoad{
[super viewDidLoad];
index=0;
NSArray *images = [NSArray arrayWithObjects:[UIImage imageNamed:@"animated-fish-1.jpg"], [UIImage imageNamed:@"tumblr_7"], [UIImage imageNamed:@"th_nature_4.jpg"],nil];
imageview.image = [images objectAtIndex:index];
[NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(changeImage) userInfo:nil repeats:YES];
}
-(void)changeImage {
index++;
if(index==[images count]){
index=0;
}
imageview.image = [images objectAtIndex:index];
}