xcode 图像阵列 Iphone

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5066580/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 20:16:45  来源:igfitidea点击:

Image array Iphone

iphonexcodeimageimageview

提问by Magnus

I want to change the image in an imageview at the click of a button.

我想通过单击按钮更改图像视图中的图像。

Sample code:

示例代码:

NSMutableArray *array = [[NSMutableArray alloc]init];

[array addObject:[UIImage imageNamed:@"pic1.png"]];
[array addObject:[UIImage imageNamed:@"pic2.png"]];
[array addObject:[UIImage imageNamed:@"pic3.png"]];

NSLog(@"%i" , [array count]);

for (int i = 0; i < [array count]; i++) {
    [type setImage:[array objectAtIndex:i]];
}

When I click the button, it displays pic3 and nothing else. Can anyone point me in the right direction?

当我单击按钮时,它显示 pic3 而没有其他内容。任何人都可以指出我正确的方向吗?

回答by Ratinho

you just set the image of the imageVIew to be pic1,then pic2 and then pic3.
if you want to change the image every click on button u should init ur array in ViewDidLoad, set int index=0;
and then, in the -(IBAction) u should inc the index and set the new pic, example:

您只需将 imageVIew 的图像设置为 pic1,然后是 pic2,然后是 pic3。
如果你想在每次点击按钮时改变图像你应该在 ViewDidLoad 中初始化你的数组,设置 int index=0;
然后,在 -(IBAction) 中,您应该包含索引并设置新图片,例如:

index=(index+1)%[array count];
[type setImage:[array objectAtIndex:index]];

回答by Amit Singh

Use this:

用这个:

- (IBAction)buttonClicked:(id)sender
{
   static int i = 0; 
   if(i == 3)
     i = 0;
   i++;
   [type setImage:[array objectAtIndex:i]];
}

Now connect this method to your button action and type to your imageView.

现在将此方法连接到您的按钮操作并键入您的 imageView。

回答by Bogatyr

There is no delay in between setting images1, 2, and 3, so you only ever see 3.

设置图像 1、2 和 3 之间没有延迟,因此您只会看到 3。