【问题标题】:TapGesture recognizer on multiple UIImageView not working多个 UIImageView 上的 TapGesture 识别器不起作用
【发布时间】:2013-04-02 00:20:54
【问题描述】:

多个 UIImageView 上的 TapGesture 识别器无法正常工作,但它会检测到最后添加的 imageviews 手势。我已经这样做了,

 UITapGestureRecognizer *tapped = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(myFunction:)];
tapped.numberOfTapsRequired = 1;
tapped.delegate = self;


UIImageView *sample_book1= [[UIImageView alloc]initWithFrame:CGRectMake(70, 135, 100,125) ];
sample_book1.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"mathematics.png"]];
sample_book1.userInteractionEnabled = YES;
sample_book1.tag = 0;
[sample_book1 addGestureRecognizer:tapped];
[self.view addSubview:sample_book1];

UIImageView *sample_book2= [[UIImageView alloc]initWithFrame:CGRectMake(220, 135, 100,125) ];
sample_book2.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"biology.png"]];
sample_book2.userInteractionEnabled = YES;
sample_book2.tag = 1;
[sample_book2 addGestureRecognizer:tapped];
[self.view addSubview:sample_book2];

UIImageView *sample_book3= [[UIImageView alloc]initWithFrame:CGRectMake(370, 135, 100,125) ];
sample_book3.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"chemistry.png"]];
sample_book3.userInteractionEnabled = YES;
sample_book3.tag = 2;
 [sample_book3 addGestureRecognizer:tapped];
[self.view addSubview:sample_book3];

点击手势在 sample_book1、sample_book2 中不起作用……它只在 sample_book3 中起作用……我做错了什么……

【问题讨论】:

    标签: ios objective-c uiimageview uitapgesturerecognizer


    【解决方案1】:

    您做错了什么是试图以不应该使用的方式使用手势。手势只能附加到 一个 视图。您需要为每个视图创建一个新视图。

    【讨论】:

    【解决方案2】:

    正如 borrrden 所说,在尝试跟踪手势时,每个视图都必须有自己的手势识别器。 对于您的每个 sample_books,您应该使用

    [sample_bookX addGestureRecognizer:[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(myFunction:)]];
    

    而不是尝试多次添加相同的 GR

    myFunction: 收到的参数将是正确的 tapGR,您可以通过调用 sender.view 来访问被点击的 imageView(提供您的 myFunction 签名看起来像

    - (void) myFunction:(UIGestureRecognizer *)sender
    

    干杯,

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多