我正在开发一个应用程序。我需要在另一个 UIImageView 中显示 9 个 UIImageViews。如果我们单击任何图像,然后识别所选 UIImageView 并更改所选 UIImageView 的图像。下一次也需要做同样的事情,但我们需要用新更改的图像显示以前选择的图像。为此,我编写了类似的代码
for (int h=0; h<8; h++)
{
int x=[[questions.marker_left objectAtIndex:h] intValue];
int y=[[questions.marker_top objectAtIndex:h] intValue];
img3=[[UIImageView alloc]initWithFrame:CGRectMake(x,y, 30, 30)];
img3.userInteractionEnabled=YES;
img1.userInteractionEnabled=YES;
img3.tag=h;
//img2.backgroundColor=[UIColor greenColor];
img3.image=[UIImage imageNamed:@"PINpurple.png"];
tap5=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(calculate:)];
tap5.delegate=self;
[img3 addGestureRecognizer:tap5];
[img1 addSubview:img3];
}
-(void)calculate:(UITapGestureRecognizer*)recognizer
{
NSInteger i=(recognizer).view.tag;
NSString *str=[questions.result objectAtIndex:i];
NSLog(@"%@",str);
if([str isEqualToString:@"true"])
{
img3.image=[UIImage imageNamed:@"PINgreen.png"];
}
else
{
img3.image=[UIImage imageNamed:@"PINred.png"];
}
}
选择的图像将被更改,但在选择下一个 UIImageView 后,以前的 UIImageView 会得到旧图像。所以请帮助我,如何做到这一点?