【问题标题】:iphone slide movement recognizeriphone滑动动作识别器
【发布时间】:2010-09-22 08:11:06
【问题描述】:

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

谢谢

【问题讨论】:

    标签: iphone slide gesture-recognition


    【解决方案1】:

    看看我的代码:

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

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

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

    【讨论】:

      【解决方案2】:

      用一些代码回答你的问题,这是我的一个很好的例子,在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];   
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-08-28
        • 2016-08-04
        • 1970-01-01
        • 2014-07-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多