Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在同一UIImageView中添加多个图像,并在每次点击时更改它们_Image_Nsarray_Tap - Fatal编程技术网

在同一UIImageView中添加多个图像,并在每次点击时更改它们

在同一UIImageView中添加多个图像,并在每次点击时更改它们,image,nsarray,tap,Image,Nsarray,Tap,我正在尝试实现一个UIImageView,它在每次点击时显示不同的.png(以NSArray组织)。当点击到达最后一个.png时,下一次点击将显示第一个.png,以此类推。 我在这里找到了一个古老的答案: 这似乎是正确的方向。但是,它似乎不适用于新的XCode,并且由于主题已经结束,因此不再有可能要求澄清或更新。 有人知道如何对整个ImageView进行子类化以获得预期结果吗?解决方案比我想象的要简单得多 由于无法按我要求的方式将自定义值传递给选择器,因此我创建了一个helper属性,该属性保存

我正在尝试实现一个UIImageView,它在每次点击时显示不同的.png(以NSArray组织)。当点击到达最后一个.png时,下一次点击将显示第一个.png,以此类推。 我在这里找到了一个古老的答案: 这似乎是正确的方向。但是,它似乎不适用于新的XCode,并且由于主题已经结束,因此不再有可能要求澄清或更新。
有人知道如何对整个ImageView进行子类化以获得预期结果吗?

解决方案比我想象的要简单得多

由于无法按我要求的方式将自定义值传递给选择器,因此我创建了一个helper属性,该属性保存当前显示图像的索引,在每次点击时递增该索引,并相应地设置图像

@interface YourViewController ()

@property (nonatomic, strong) NSArray *images;
@property (nonatomic, assign) NSInteger currentImageIndex;

@end


@implementation YourViewController

- (void)viewDidLoad {

// Here you fill your images array
self.images = ...   

//  Set currentImageIndex to 0 and display first image 
self.currentImageIndex = 0;
[self displayImageWithIndex:self.currentImageIndex];

// Additional code, like your button target/action
}
- (void)myButtonWasTapped:(id)sender
{
if(self.currentImageIndex + 1 < [self.images count]) {
    self.currentImageIndex++;
} else {
    self.currentImageIndex = 0;
}
[self displayImageWithIndex: self.currentImageIndex];
}

- (void)displayImageWithIndex:(NSInteger)index {
self.imageView.image = [self.images objectAtIndex:index];
}

@end
@界面YourViewController()
@属性(非原子,强)NSArray*图像;
@属性(非原子,赋值)NSInteger currentImageIndex;
@结束
@ViewController的实现
-(无效)viewDidLoad{
//在这里,您可以填充图像数组
self.images=。。。
//将currentImageIndex设置为0并显示第一个图像
self.currentImageIndex=0;
[带索引的自显示图像:self.currentImageIndex];
//附加代码,如按钮目标/操作
}
-(无效)myButtonWasTapped:(id)发件人
{
如果(self.currentImageIndex+1<[self.images计数]){
self.currentImageIndex++;
}否则{
self.currentImageIndex=0;
}
[带索引的自显示图像:self.currentImageIndex];
}
-(void)显示带索引的图像:(NSInteger)索引{
self.imageView.image=[self.images objectAtIndex:index];
}
@结束
请注意:可能有更好的方法,但在我的案例中就是这样。
希望这能有所帮助。

对不起,我忘了提到UIImage视图是通过编程方式创建的,我没有对触摸屏使用任何iAction。非常感谢。