我试图在每次点击时实现一个显示不同.png(在NSArray中组织)的UIImageView。当点按到达最后一个.png时,下一个点按将显示第一个.png,依此类推。 我在这里找到了一个旧答案: Add 4 image in imageview and change the picture with tap or swipe gesture 这似乎朝着正确的方向发展。但是,它似乎并不适用于新的XCode,因为主题已经关闭,所以不再需要澄清或更新。 有没有人知道如何将整个ImageView子类化以获得预期的结果?
答案 0 :(得分:0)
解决方案比我想象的容易得多。
由于选择器的自定义值无法按照我的要求传递,因此我创建了一个辅助属性,它保存当前显示图像的索引,在每次点击时递增,并相应地设置图像。
@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
请注意:可能有更好的方法,但这就是我的情况。 希望这会有所帮助。