如何在另一个图像中显示多个图像。

时间:2012-04-05 09:57:07

标签: iphone uiimageview

我正在开发一个applciation.In我需要在另一个UIImageView中显示9个UIImageView。如果我们点击任何图像然后识别选定的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后获取旧图像。所以请帮帮我,怎么做?

1 个答案:

答案 0 :(得分:0)

    //Edit In calculate function
    -(void)calculate:(UITapGestureRecognizer*)recognizer
    {
      NSInteger i=(recognizer).view.tag;

      //Detect selected UIImageView
      UIImageView *selectedImageView=(UIImageView*)recognizer.view;

      NSString *str=[questions.result objectAtIndex:i];

      NSLog(@"%@",str);

     if([str isEqualToString:@"true"])
        {
          selectedImageView.image=[UIImage imageNamed:@"PINgreen.png"];
        }
     else
        {
          selectedImageView.image=[UIImage imageNamed:@"PINred.png"];
        }
    }