【问题标题】:How to switch between views using Pan Gesture Recognizer in iOS?如何在 iOS 中使用平移手势识别器在视图之间切换?
【发布时间】:2013-01-10 15:02:32
【问题描述】:

我有两个视图,我想使用向右平移手势(如 Facebook ios 应用程序中使用的手势)切换到第二个视图。一旦我们开始使用平移手势,视图应该会开始改变。我该如何执行此操作?有人有执行此操作的示例代码吗?

【问题讨论】:

    标签: ios uipangesturerecognizer


    【解决方案1】:

    有一个带有示例项目的 git,您可以通过它参考 Facebook iOS 应用中的侧边菜单效果。

    https://github.com/BenHall/ios_facebook_style_navigation

    编辑:

    好的,那就试试这样的:

       UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(scrollViewSwipedLeft:)];
       [swipeLeft setDirection:UISwipeGestureRecognizerDirectionLeft];
       [self.navigationController.navigationBar addGestureRecognizer:swipeLeft];
    

    然后用这样的方式处理滑动:

    -(void) didSwipedLeft: (UISwipeGestureRecognizer *) gesture {
    
      if (gesture.state != UIGestureRecognizerStateEnded) {
          return;
      }
    
      //do something    
    }
    

    【讨论】:

    • 示例项目中没有使用平移手势...我需要使用平移手势切换视图..
    • 我想执行这样的操作: UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)]; [panRecognizer setMinimumNumberOfTouches:1]; [panRecognizer setMaximumNumberOfTouches:1]; //[panRecognizer setDelegate:self]; [[自拍] addGestureRecognizer:panRecognizer];
    • - (void)handlePan:(UIPanGestureRecognizer*)recognizer { CGPoint translation = [recognizer translationInView:recognizer.view];识别器.view.center=CGPointMake(recognizer.view.center.x+translation.x,recognizer.view.center.y+translation.y); [recognizer setTranslation:CGPointMake(0, 0) inView:recognizer.view]; }
    • 使用此代码,我可以向任何方向拖动当前视图,同时显示其下方的视图。但是在这段代码中,缺少的元素是平移手势方向,以及在我们平移时更改子视图。
    • 嗯,这就是为什么我发布了UISwipeGestureRecognizer
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多