如果我点击了子视图,我该如何查看识别器?

时间:2012-11-12 16:51:33

标签: ios uigesturerecognizer

当我点击图像时,我想显示另一个弹出窗口,但它不起作用!

我的手柄Tap方法如下:

-(void)handleTapView:(UITapGestureRecognizer*)recognizer
{
   CGPoint startPoint = [recognizer locationInView:recognizer.view];
   NSLog(@"handle Tap VIEW!!!!!!!!");

   if ([recognizer.view isKindOfClass:[UIImageView class]] ) {
      NSLog(@"Tap Image!!!!!!!!");        
   }
   else if ([self.popover isPopoverVisible]) {
      [self.popover dismissPopoverAnimated:YES];        
   }
   else {
      ShapesListViewController *shapes = (ShapesListViewController*) [self.storyboard instantiateViewControllerWithIdentifier:@"ShapesListViewController"];

      UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:shapes];
      UIPopoverController *pop = [[UIPopoverController alloc] initWithContentViewController:nav];

      shapes.delegate = self;

      self.popover = pop;   

      CGRect popoverRect;
      popoverRect.origin = startPoint;
      popoverRect.size.width = 1;
      popoverRect.size.height =1;

      [pop presentPopoverFromRect:popoverRect inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
   }   
}

在viewdidload中:

ImageView *imgv = [[ImageView alloc] initWithImage:[UIImage imagNamed:@"delete.png"]];
imgv.center = CGPointMake(250,250);     
[self.view addSubview:imgv];

ImageView *imgv2 = [[ImageView alloc] initWithImage:[UIImage imageNamed:@"gear.png"]];

imgv2.center = CGPointMake(400,400);
[self.view addSubview:imgv2];


//Tap Recognizer
self.singelTapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapView:)];
[self.singelTapGestureRecognizer setNumberOfTapsRequired:1];
[self.view addGestureRecognizer:self.singelTapGestureRecognizer];

1 个答案:

答案 0 :(得分:0)

您需要为要捕获其手势的每个视图附加一个单独的gestureRecogniser - 在您的情况下,两个您的imageViews。

UITapGestureRecognizer *tapGR1, *tapGR2;
SEL selector = @selector(handleTapView:);
tapGR1 = [[UITapGestureRecognizer alloc] initWithTarget:self 
                                                 action:selector];
tapGR2 = [[UITapGestureRecognizer alloc] initWithTarget:self 
                                                 action:selector];
       [imgv1 addGestureRecognizer:tapGR1];
       [imgv2 addGestureRecognizer:tapGR2];

不要将tapGR附加到他们的超级视图。

然后你还需要在每个imageViews上设置userInteractionEnabled,否则他们会忽略触摸(UIImageView默认为userInteractionEnabled = NO):

        imgv1.userInteractionEnabled = YES;
        imgv2.userInteractionEnabled = YES;

在你handleTapView中,你需要稍微重组一下。将条件序列中的else if更改为if,否则第三个子句将永远不会被触发。

每个recongnizer的recognizer.view将正确识别被轻击的imageView。这将是popover应该在imageView的超级视图的坐标中呈现的矩形 - 它是frame属性。

所以:

 [pop presentPopoverFromRect:recognizer.view.frame 
                      inView:self.view    
    permittedArrowDirections:UIPopoverArrowDirectionAny 
                    animated:YES];