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

标签 ios iphone-sdk-3.0 uipinchgesturerecognizer

我正在使用 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");
} 

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

最佳答案

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

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

关于ios - 如何使用 UIPinchGestureRecognizer 检测或定义捏合手势的方向?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3559577/

相关文章:

iphone - 很棒的 UIKit/Objective-C 代码片段

iphone - iPhone应用程序-与服务器通信时的错误处理

ios - UIPinchGestureRecognizer 缩放 AVCaptureConnection;不按比例缩小

iphone - 如何使用 UIPinchGestureRecognizer 设置最小和最大缩放比例

objective-c - 如何将数组/字典传输到其他viewController?

ios - UITableView subview 中的辅助功能丢失

iphone - 如何从 iPhone 中的图像中去除红眼?

ios - 捏合/缩放/调整 UITextView 大小时文本被截断

ios - Google 云计算在后台使用 Swift 推送通知

ios - 上传 CSR 文件后证书无效