1

我将 UIImageView 作为子视图添加到 UIScrollView 然后我设置图像。

我正在尝试使用 UITapGestureRecognizer。

在 iOS 5 中永远不会调用 UITapGestureRecognizer 的选择器(在 iOS 6 中它确实如此!)。尝试了许多变化。这是我的代码:

    UIImageView *imgView = [[UIImageView alloc]initWithFrame:CGRectMake(index*(IMAGE_WIDTH+10), 10.0, IMAGE_WIDTH, IMAGE_HEIGHT)];
    [imgView setImageWithURL:[NSURL URLWithString:meal.RecommendedImageURL] placeholderImage:[UIImage imageNamed:@""]];
    imgView.layer.cornerRadius = 4;
    [imgView.layer setMasksToBounds:YES];
    [imgView setUserInteractionEnabled:YES];
    [imgView setMultipleTouchEnabled:YES];

    [scrollView addSubview:imgView];

    if (IOS_NEWER_OR_EQUAL_TO_5)    
    {
        UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imageTapped:)];
        [imgView addGestureRecognizer:tapGestureRecognizer];
        tapGestureRecognizer.numberOfTapsRequired = 1;
        tapGestureRecognizer.enabled = YES;
        tapGestureRecognizer.delegate = self;
        [tapGestureRecognizer setCancelsTouchesInView:NO];
    }

这是我的选择器,仅在 iOS5 中调用:

 - (void) imageTapped: (UITapGestureRecognizer *)recognizer
{
    //Code to handle the gesture
    UIImageView *tappedImageView = (UIImageView*)recognizer.view;
    GGFullscreenImageViewController *vc = [[GGFullscreenImageViewController alloc] init];
    vc.liftedImageView = tappedImageView;
    vc.liftedImageView.contentMode = UIViewContentModeScaleAspectFit;
    if (IOS_NEWER_OR_EQUAL_TO_5) {
        [self.parentViewController presentViewController:vc animated:YES completion:nil];
    }
    else
    {
        [self.parentViewController presentModalViewController:vc animated:YES];
    }


}

除此之外,我尝试在我的 UIScrollView 中将 CancelsTouchesInView 设置为 YES,但它也不起作用。谢谢你的帮助!

4

3 回答 3

0

实现 UIGestureRecognizerDelegate 的 3 个方法并返回默认值:

-(BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer;

-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer;

-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch;
于 2013-05-07T12:32:07.787 回答
0

好的,我很容易解决了!问题是将 UITapGestureRecognizer 的委托设置为 self。当我删除委托=自我时,它开始工作:) 感谢您的帮助

于 2013-05-07T13:06:38.397 回答
0
   try to add Gesture in Scrollview then check iskind of class image view
      UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imageTapped:)];
    [scrollview addGestureRecognizer:tapGestureRecognizer];
    tapGestureRecognizer.numberOfTapsRequired = 1;
    tapGestureRecognizer.enabled = YES;
    tapGestureRecognizer.delegate = self;
    [tapGestureRecognizer setCancelsTouchesInView:NO];

       - (BOOL)gestureRecognizer:(UITapGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
         // test if our control subview is on-screen
           if ([touch.view isKindOfClass:[UIImageView class]]) {
           // we touched a button, slider, or other UIControl
             return YES;
           }return NO;
        }
于 2013-05-07T12:29:24.993 回答