ios 3.1.3上的手势检测

时间:2011-08-15 19:19:47

标签: iphone ios

在ios 3.1及以上版本中我如何能够在UIView中检测到手势...

在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];

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

由于

1 个答案:

答案 0 :(得分:2)

你将不得不继承UIView(或者如果布局不太复杂,在视图控制器中实现内容)并使用你自己跟踪你想要的手势{{3}方法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:中实现某种最大持续时间或跟踪速度,或检查触摸是否移动了至少最小距离。我认为这是因为人们对所有那些苹果最终提供UIGestureRecognizer的东西做出了不同的决定。