iphone滑动运动识别器

时间:2010-09-22 08:11:06

标签: iphone slide gesture-recognition

我正在开发一个新的应用程序,我需要实现在许多应用程序中经常使用的功能。我想分别用“滑动手势”实现“下一页”/“上一页”功能,从左到右分别用于“下一页”情况,从右到左分别用于另一页。 我见过GestureRecognizer可能对我有所帮助,但不幸的是我在3.1.2固件版本下开发并且还不支持。 任何教程的建议或链接?

谢谢

2 个答案:

答案 0 :(得分:1)

看看我的代码:

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

您可以更改滑动的方向等; - )

编辑:哦,我没有看到你问题的结束:p 所以你应该实现一个UIView并检测touchesBegan和touchesEnd,保存CGPoint的开始和结束,并决定它是否是滑动或nop;)

答案 1 :(得分:0)

要使用某些代码回答您的问题,这是我在this thread.

中给出的一个很好的示例版本
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = touches.anyObject;
    //Define "CGPoint startTouchPosition;" in  your header
    startTouchPosition = [touch locationInView:self];
    [super touchesBegan:touches withEvent:event];
}

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{
    UITouch *touch = touches.anyObject;
    CGPoint currentTouchPosition = [touch locationInView:self];

    // If the swipe tracks correctly.
   double diffx = startTouchPosition.x - currentTouchPosition.x + 0.1;
   double diffy = startTouchPosition.y - currentTouchPosition.y + 0.1;

   //If the finger moved far enough: swipe
    if(abs(diffx / diffy) > 1 && abs(diffx) > 100)
    {
       if (!swipeHandled) {
        [self respondToSwipe];

        //Define "bool swipeHandled;" in your header
        swipeHandled = true;
       }
    }

   [super touchesMoved:touches  withEvent:event];
}

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    swipeHandled = true;
    [super touchesEnded:touches withEvent:event];   
}