3

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

また、単純なピンチ ジェスチャを検出するのにも適しています: ピンチ ジェスチャの角度または向きを決定 (または定義) することは可能です (たとえば、水平方向のピンチ ジェスチャと垂直方向のピンチ ジェスチャを区別するなど)。

4

1 に答える 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)
    }
}
于 2011-12-24T00:00:12.437 に答える