0

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

我的句柄 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];
4

1 回答 1

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 附加到他们的超级视图。

然后您还需要userInteractionEnabled在每个 imageViews 上进行设置,否则它们将忽略触摸(UIImageView 默认为userInteractionEnabled = NO):

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

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

每个recognizer.viewrecongnizers 将正确识别被点击的 imageView。这将是弹出框应该在 imageView 的超级视图的坐标中出现的矩形 - 它是frame属性。

所以:

 [pop presentPopoverFromRect:recognizer.view.frame 
                      inView:self.view    
    permittedArrowDirections:UIPopoverArrowDirectionAny 
                    animated:YES];
于 2013-09-04T06:42:10.470 回答