【问题标题】:iOS Add tap Gesture to UIImageViewiOS将点击手势添加到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);
}

【问题讨论】:

  • 好的,您已经概述了问题。你做了什么来解决它?您需要提出使用原始图像大小与当前图像大小的比例来将视图坐标转换为图像坐标的逻辑。

标签: ios objective-c image gesture


【解决方案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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-04-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-08
      相关资源
      最近更新 更多