2010-09-22 59 views
0

我正在开发一个新的应用程序,我需要实现很多应用程序中经常使用的功能。我想分别从左到右为“下一页”的情况以及从右到左的另一个滑动手势实现“下一页”/“上一页”功能。 我已经看到了一些关于GestureRecognizer的信息,可能可以帮助我,但不幸的是,我正在开发3.1.2固件版本,目前还不支持。 任何建议或任何教程链接?iphone滑动移动识别器

谢谢

回答

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]; 
} 
+0

酷给出!非常感谢你 !! – Marco 2010-09-22 15:24:06

1

看看我的代码:

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

可以更改刷卡等方向;-)

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