如何处理代码创建的UIView事件

时间:2014-12-16 04:17:10

标签: ios objective-c uiimageview uigesturerecognizer

我有未知数量的UIImageViews在代码中创建,而不是在xib文件中,我需要处理这些图像上的点击。每个图像视图的处理都是一样的。我该怎么做?

4 个答案:

答案 0 :(得分:0)

演示的演示代码

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    UIImageView *imgView1 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
    UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapDetected:)];

    singleTap.numberOfTapsRequired = 1;
    [imgView1 setUserInteractionEnabled:YES];
    [imgView1 addGestureRecognizer:singleTap];
    imgView1.tag = 1;
    imgView1.backgroundColor = [UIColor redColor];
    [self.view addSubview:imgView1];

    UIImageView *imgView2 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 200, 100, 100)];
    UITapGestureRecognizer *singleTap2 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapDetected:)];

    singleTap.numberOfTapsRequired = 1;
    [imgView2 setUserInteractionEnabled:YES];
    [imgView2 addGestureRecognizer:singleTap2];
    imgView2.tag = 2;
    imgView2.backgroundColor = [UIColor blueColor];


    [self.view addSubview:imgView2];

}

-(void)tapDetected:(UITapGestureRecognizer *)gestureRecognizer{
    UIImageView *myImg = (UIImageView*)gestureRecognizer.view;
    NSLog(@"tag : %ld",(long)myImg.tag);

}

答案 1 :(得分:0)

尝试使用此代码添加手势识别器

   UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self  action:@selector(action:)];
  [_imgview addGestureRecognizer:tap]; 
  _imgview.userInteractionEnabled = YES;

答案 2 :(得分:0)

您可以添加UITapGestureRecognizer来检测UIImageView上的触摸。

只需使用带有参数的下方法,图片视图和唯一标记,就可以了!

- (void) setTapGestureOnImageView:(UIImageView *)imageView withTag:(NSInteger)tag {
    //this is important, by default user interaction isn't enabled, we have to enable it.
    imageView.userInteractionEnabled = YES;

    //create a tap gesture (in example this is sinlge tap) with target and action to call when user tap
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(detectTap:)];

    //you can customize taps too
    //tap.numberOfTapsRequired = 2;
    //tap.numberOfTouchesRequired = 2;

    //add gesture on image view
    [imageView addGestureRecognizer:tap];
}

- (void)detectTap:(UIGestureRecognizer *)recognizer {
    //Get the tapped image view from recognizer
    UIImageView *imageView = (UIImageView *)recognizer.view;
    //Check for condition, which image view tapped
    if(imageView.tag == 1) {
        //do something 1st imageview
    }
    else if(imageView.tag == 2) {
        //do something for 2nd imageview
    }
    else {
        //do something else
    }
}

答案 3 :(得分:-1)

这可能会对你有用

- (void)viewDidLoad
{
  NSArray *imagesArray = [NSArray arrayWithObjects:@"statement_card_1.png", @"statement_card_2.png", @"statement_card_3.png", @"statement_card_4.png", @"statement_card_5.png", nil];
  short xPadding = 10;
  for (int i = 0; i< imagesArray.count; i++)
  {
    UIImage *imageRecipe =[UIImage imageNamed:[imagesArray objectAtIndex:i]];
    UIImageView *imgView = [[UIImageView alloc]initWithFrame:CGRectMake(xPadding, xPadding+(i*60),imageRecipe.size.width, imageRecipe.size.height)];

    imgView.tag =IMAGETAG + i;
    [imgView setImage:imageRecipe];
    imgView.userInteractionEnabled = YES;

    UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapDetected:)];
    [imgView addGestureRecognizer:singleTap];
    [self.view addSubview:imgView];

   }

}

-(void)tapDetected:(UITapGestureRecognizer *)recognizer
{
UIImageView *TempImg = (UIImageView *)recognizer.view;
NSLog(@"tag : %ld",(long)TempImg.tag);
}