2011-08-15 242 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