IBAction通过点击图像循环

时间:2015-03-05 05:49:57

标签: ios objective-c iphone

我想使用一种IBAction方法,当点击循环图像时。即,一个图像和一个按钮被显示为卸载,当用户点击按钮时,新的图像显示在其位置等。现在它只显示阵列中的第一个图像。当我第二次点击按钮时没有任何反应。有人可以帮我编辑一下,循环浏览我的所有图片吗?谢谢。

修改

//when i tap the button only the first image shows in the array. What should I edit from the current app?

#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}



//Implementing our method
- (IBAction)buttonPressed

{
NSArray *imagesArray = @[@"image1.png",@"image2.png",@"image3.png",@"image4.png"];
count ++;

if(count == imagesArray.count)
{
    count = 0;
}

yourImageView.image = [UIImage imageNamed:[imagesArray objectAtIndex:count]];
}

@end

1 个答案:

答案 0 :(得分:1)

试试这个......

将图像添加到项目中,并将这些图像名称存储到NSArray

代表。

//declare imagesArray as instance variable

NSArray *imagesArray = @[@"image1.png",@"image2.png",@"image3.png",@"image4.png"];

并在按钮操作方法

 - (IBAction)buttonPressed 
 { 
          count ++;

          if(count == imagesArray.count)
          {
           count = 0;
          }

          yourImageView.image = [UIImage imageNamed:[imagesArray objectAtIndex:count]];
 }