ios - IBAction 循环浏览图像

标签 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

最佳答案

试试这个....

将图像添加到您的项目中并将这些图像名称存储到 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]];
 }

关于ios - IBAction 循环浏览图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28870931/

相关文章:

iphone - 我如何使一个表格 View 指向具有另一个表格 View 的页面?

ios - 分析 CAKeyframeAnimation 时出现警告

ios - drawRect 圆和动画大小/颜色

ios - 对 NSArray 结果进行排序很奇怪

iphone - 一个 NIB 中的多个自定义 UITableViewCell - 如何在代码中引用?

ios - UICollectionView indexPath.row 或项目,项目不稳定,跳来跳去

objective-c - 此 Obj-C 文档中的 `+monthArray` 是什么?

iphone - 如何防止TableViewCell在回收过程中重复?

ios - 我如何应对 Facebook API Graph 弃用警告?

ios - 在 iOS 中,是什么导致了这个崩溃?