How to detect or define the the orientation of a pinch gesture with UIPinchGestureRecognizer?

限于喜欢 提交于 2019-12-10 10:27:08

问题


I'm using UIPinchGestureRecognizer to detect pinch gestures, something like:

- (void) initPinchRecon {
 UIPinchGestureRecognizer *pinchRecognizer = [[[UIPinchGestureRecognizer alloc] 
              initWithTarget:self
              action:@selector(Perform_Pinch:)] autorelease];
 [self addGestureRecognizer:pinchRecognizer];

 [pinchRecognizer setScale:20.0f];
}

- (void) Perform_Pinch:(UIPinchGestureRecognizer*)sender{
 NSLog(@"PINCH");
} 

And it works well to detect a simple pinch gesture: It's possible to determine (or define myself) the angle or orientation of the pinch gesture ?, for example, to differentiate between an horizontal and an vertical pinch gesture ?

Thanks


回答1:


A very simple solution is to implement the gesture handler like this:

-(void)handlePinchGesture:(UIPinchGestureRecognizer *)recognizer {
if (recognizer.state != UIGestureRecognizerStateCancelled) {
    if (recognizer.numberOfTouches == 2) {
        CGPoint firstPoint = [recognizer locationOfTouch:0 inView:recognizer.view];
        CGPoint secondPoint = [recognizer locationOfTouch:1 inView:recognizer.view];

        CGFloat angle = atan2(secondPoint.y - firstPoint.y, secondPoint.x - firstPoint.x);

        // handle the gesture based on the angle (in radians)
    }
}


来源:https://stackoverflow.com/questions/3559577/how-to-detect-or-define-the-the-orientation-of-a-pinch-gesture-with-uipinchgestu

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!