开发者

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

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:p开发者_JS百科inchRecognizer];

 [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 ?


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)
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜