如何使用UIPinchGestureRecognizer检测或定义捏合手势的方向?

问题描述 投票:3回答:1

我正在使用UIPinchGestureRecognizer来检测捏捏手势,例如:

- (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");
} 

并且可以很好地检测简单的捏合手势:可以确定(或定义自己)捏合手势的角度或方向?例如,区分水平捏合手势和垂直捏合手势?

ios iphone-sdk-3.0 uipinchgesturerecognizer
1个回答
4
投票

一个非常简单的解决方案是像这样实现手势处理程序:

-(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)
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.