iOS将点击手势添加到UIImageView

Posted

技术标签:

【中文标题】iOS将点击手势添加到UIImageView【英文标题】:iOS Add tap Gesture to UIImageView 【发布时间】:2014-02-05 23:05:27 【问题描述】:

我在 UIImageView 中有一个图像,它有一个缩放手势,正如您从代码中看到的那样。

我希望能够插入图像的可点击区域,而不是屏幕,所以如果我点击具有最小或最大缩放缩放的图像点,我总是返回相同的值。

我可以通过 UIGestureRecognizer 判断是否可以?为什么我不能弄清楚我是否工作。

谢谢

我的代码:

UITapGestureRecognizer *twoFingerTap = [[UITapGestureRecognizer alloc] initWithTarget:self         action:@selector(handleTwoFingerTap:)];
[twoFingerTap setNumberOfTouchesRequired:2];
[img addGestureRecognizer:twoFingerTap];
float minimumScale = 0.4;//This is the minimum scale, set it to whatever you want. 1.0 = default
scroll.maximumZoomScale = 4.0;
scroll.minimumZoomScale = minimumScale;
scroll.zoomScale = minimumScale;
[scroll setContentMode:UIViewContentModeScaleAspectFit];
[img sizeToFit];
[scroll setContentSize:CGSizeMake(img.frame.size.width, img.frame.size.height)];

[control setSelectedSegmentIndex:0];
[control addTarget:self
            action:@selector(action:)
  forControlEvents:UIControlEventValueChanged];

//THIS IS MY RECOGNIZER ON IMAGE
UIGestureRecognizer *recognizerImage = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapImage:)];
[(UITapGestureRecognizer *)recognizer setNumberOfTapsRequired:2];
[img addGestureRecognizer:recognizer];



- (void)tapImage:(UITapGestureRecognizer *)recognizer 

    CGPoint location = [recognizer locationInView:self.view];
    NSLog(@"x %f y %f",location.x, location.y);

【问题讨论】:

好的,您已经概述了问题。你做了什么来解决它?您需要提出使用原始图像大小与当前图像大小的比例来将视图坐标转换为图像坐标的逻辑。 ***.com/questions/9008975/… 【参考方案1】:

尝试使用 imageView 作为参考:

CGPoint location = [recognizer locationInView:recognizer.view];

让我知道这是否有效。

【讨论】:

【参考方案2】:
- (void)tapImage:(UITapGestureRecognizer *)recognizer 

    // CGPoint location = [recognizer locationInView:self.view]; // self.view will always be the same. You need to use the view on which the user tapped. It's `recognizer.view`

    CGPoint location = [recognizer locationInView:recognizer.view]; 

    NSLog(@"x %f y %f",location.x, location.y);

如果你想了解更多关于 UIGestures 的信息,那么我为你准备了一个很棒的教程。

http://www.raywenderlich.com/6567/uigesturerecognizer-tutorial-in-ios-5-pinches-pans-and-more

【讨论】:

以上是关于iOS将点击手势添加到UIImageView的主要内容,如果未能解决你的问题,请参考以下文章

如果将点击手势识别器添加到其背景视图中,则不会调用 iOS didSelectItemAtIndexPath

将点击手势添加到任何视图都不会触发

iOS:从顶视图到底视图拦截点击手势事件

iOS - 在图像视图上点击手势问题

以编程方式将点击手势添加到 UILabel

将点击手势添加到插座集合