2011-08-15 257 views
0

在IOS 3.1及以上的如何檢測在UIView的手勢...手勢檢測3.1.3

在IOS> = 3.2.3我使用此代碼...(例如):

UISwipeGestureRecognizer *oneFingerSwipeLeft = 
    [[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(oneFingerSwipeLeft:)] autorelease]; 
    [oneFingerSwipeLeft setDirection:UISwipeGestureRecognizerDirectionLeft]; 
    [[self view] addGestureRecognizer:oneFingerSwipeLeft]; 

    UISwipeGestureRecognizer *oneFingerSwipeRight = 
    [[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(oneFingerSwipeRight:)] autorelease]; 
    [oneFingerSwipeRight setDirection:UISwipeGestureRecognizerDirectionRight]; 
    [[self view] addGestureRecognizer:oneFingerSwipeRight]; 

有人有一個想法/例子...

感謝

回答

2

你將不得不子類UIView(或實現視圖控制器內的東西,如果佈局不太複雜) 並跟蹤您使用葉奧爾德UIResponder方法touchesBegan:withEvent:想爲自己的手勢等

例如:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    if(!trackingTouch && [touches count] == 1) 
    { 
     trackingTouch = [touches anyObject]; 
     startingPoint = [trackingTouch locationInView:relevantView]; 
    } 
} 

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    if(trackingTouch && [touches containsObject:trackingTouch]) 
    { 
     CGPoint endingPoint = [trackingTouch locationInView:relevantView]; 
     trackingTouch = nil; 

     if(endingPoint.x < startingPoint.x) 
      NSLog(@"swipe left"); 
     else 
      NSLog(@"swipe right"); 
    } 
} 

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    [self touchesEnded:touches withEvent:event]; 
} 

// don't really care about touchesMoved:withEvent: 

這是一個不完美的解決方案,因爲它假定所有手指向下手指級數必然刷卡。您可能需要在touchesMoved:withEvent:中實施某種類型的最長持續時間或跟蹤速度,或檢查觸摸是否至少移動了最小距離。我認爲這是因爲人們對蘋果公司最終提供的所有這些東西做出了不同的決定。

+0

非常感謝Tommy! – Maxime