Objective c 按循环顺序显示图像
我有一张5张照片的照片。我想在循环中一个接一个地显示它们。 我的代码:Objective c 按循环顺序显示图像,objective-c,cocoa-touch,ios,uiimage,Objective C,Cocoa Touch,Ios,Uiimage,我有一张5张照片的照片。我想在循环中一个接一个地显示它们。 我的代码: NSArray *tabImage = [[NSArray alloc] init]; tabImage = [[NSArray arrayWithObjects: [UIImage imageNamed:@"picto_1.png"], [UIImage imageNamed:@"picto_2.png"], [UIImage
NSArray *tabImage = [[NSArray alloc] init];
tabImage = [[NSArray arrayWithObjects:
[UIImage imageNamed:@"picto_1.png"],
[UIImage imageNamed:@"picto_2.png"],
[UIImage imageNamed:@"picto_3.png"],
[UIImage imageNamed:@"picto_4.png"],
[UIImage imageNamed:@"picto_5.png"],
nil] retain];
int var = 0;
var = indexPath.row;
for (int var = 0; var < 20; var++) {
pictoImage.image = [tabImage objectAtIndex:(var % 5)];
}
NSArray*tabImage=[[NSArray alloc]init];
tabImage=[[NSArray arrayWithObjects:
[UIImage ImageName:@“picto_1.png”],
[UIImage ImageName:@“picto_2.png”],
[UIImage ImageName:@“picto_3.png”],
[UIImage ImageName:@“picto_4.png”],
[UIImage ImageName:@“picto_5.png”],
无]保留];
int-var=0;
var=indexPath.row;
对于(int-var=0;var<20;var++){
pictoImage.image=[tabImage objectAtIndex:(变量%5)];
}
我一直在拍同一张照片
谢谢您的帮助。您没有给框架时间将图像实际渲染到窗口 您的所有更新都合并到一个“setNeedsDisplay”调用中。在运行循环的下一次迭代中,图像视图将使用在循环中设置的最后一个图像进行渲染。您不打印任何内容 您将连续重写
pictoImage
20次,最后一次赋值是在var为19时,因此它应该是picto_4.png
而且,整个
var
声明没有多大意义。。。首先将其设置为0
,然后将其设置为indexPath.row
,在循环中它被完全重新定义……我猜pictoImage
是一个UIImageView
。在这种情况下,请查看属性,特别是animationImages
属性
pictoImage.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:@"picto_1.png"],
[UIImage imageNamed:@"picto_2.png"],
[UIImage imageNamed:@"picto_3.png"],
[UIImage imageNamed:@"picto_4.png"],
[UIImage imageNamed:@"picto_5.png"],
nil];
// How many seconds it should take to go through all images one time.
pictoImage.animationDuration = 5.0;
// How many times to repeat the animation (0 for indefinitely).
pictoImage.animationRepeatCount = 4;
[pictoImage startAnimating];
如果
pictoImage
是一个UIImageView
,那么@Hagelin的答案是最好的选择。现在,如果pictoImage
不是UIImageView
对象,那么您必须采取不同的方法,这肯定不是循环
设置一个具有所需时间间隔的NSTimer,并调用一个方法来更新pictoImage
并在tabImages
上迭代。应该是这样的:-
- (void)setup {
...
[NSTimer scheduledTimerWithTimeInterval:2.0
target:self
selector:@selector(updatePictoImage:)
userInfo:nil
repeats:YES];
...
}
- (void)updatePictoImage:(NSTimer*)theTimer {
static int i = 0;
if ( i > 20 ) {
pictoImage.image = [tabImages objectAtIndex:(i % 5)];
} else {
[theTimer invalidate];
}
}
这应该每2秒更新一次
pictoImage
,其中包含tabImages
中的图像。如何打印每张图片?如果“打印”是指“渲染到窗口”,则有很多选项。如果坚持同步执行此操作,请给runloop一些时间在更改图像之间运行。在这里查看我的答案:否则,使用Hagelin答案中的内置动画支持,使用不同的延迟执行PerformSelector AfterDelay等。所有这些都将让运行循环运行,这意味着窗口将更新以反映您的更改。可能重复