2010-08-24 64 views
3

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

,而且運作良好,檢測一個簡單的捏放:這是可能的,以確定(或定義自己)縮放手勢的角度或方向?,例如,區分水平和垂直捏手勢?

感謝

回答

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