zoukankan      html  css  js  c++  java
  • UIImageView添加响应事件无响应

    通常给UIImageView添加响应按如下方法:

    UIImageView *newView = [[UIImageView alloc]initWithFrame:CGRectMake(x, y, IMG_WIDTH, IMG_HEIGHT)];
            newView.userInteractionEnabled = YES;
            newView.tag = i;
            
            UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self
                                                                                        action:@selector(TakePhotoFor:)];  
            [newView addGestureRecognizer:singleTap];
            [singleTap release];
            
            //加载已经拍摄的图片
            [newView setImage:[UIImage imageNamed:@"MPV.png"]];        
            
            [self.view addSubview:newView];
            [newView release];

    但是今天我在一个循环里创建N个UIImageView,然后在循环外创建的UITapGestureRecognizer,结果点击后就没响应。后来查资料发现Gesture recognizers can be associated to a single view only。也就是要在循环内为每个UIImageView创建并添加UITapGestureRecognizer。

    like this:

      for (i = 0; i<18; i++) {
            x = StartX + (i%6)*OFFSET_X;
            y = StartY + (i/6)*OFFSET_Y;
            
            UIImageView *newView = [[UIImageView alloc]initWithFrame:CGRectMake(x, y, IMG_WIDTH, IMG_HEIGHT)];
            newView.userInteractionEnabled = YES;
            newView.tag = i;
            
            UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self
                                                                                        action:@selector(TakePhotoFor:)];  
            [newView addGestureRecognizer:singleTap];
            [singleTap release];
            
            //加载已经拍摄的图片
            [newView setImage:[UIImage imageNamed:@"MPV.png"]];        
            
            [self.view addSubview:newView];
            [newView release];
        }

    问题解决。

  • 相关阅读:
    ASP.NET Ajax基础-1
    项目管理必读之书-》人月神话
    Discuz2.5菜鸟解析-1
    Jquery初学者指南-1
    敏捷日记
    精品图书大推荐2
    Jquery初学者指南-2
    纯javaScript脚本来实现Ajax功能例子一
    周五面试笑话一则
    JavaScript基础-4
  • 原文地址:https://www.cnblogs.com/cokecoffe/p/2589885.html
Copyright © 2011-2022 走看看