iOS为UIImageView添加tap Gesture

时间:2014-02-05 23:05:27

标签: ios objective-c image gesture

我在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);
}

2 个答案:

答案 0 :(得分:1)

尝试使用imageView作为参考:

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

如果有效,请告诉我。

答案 1 :(得分:0)

- (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