【问题标题】:Objective C: trigger method at pan valueObjective C: pan 值的触发方法
【发布时间】:2023-03-30 00:14:01
【问题描述】:

根据Pan gesture 值触发method 的最佳方法是什么?

我想在我的pan location x 介于 500 到 600 之间时触发method。人们通常如何处理这个问题??

【问题讨论】:

    标签: ios objective-c ipad uipangesturerecognizer


    【解决方案1】:

    您需要覆盖gestureRecognizerShouldBegin。然后您可以检测 translation.x 并查看值之间的位置。

    - (BOOL)gestureRecognizerShouldBegin:(UIPanGestureRecognizer *)panGestureRecognizer {
       CGPoint translation = [panGestureRecognizer translationInView:self.view];
       if (translation.x > 500 && translation.x < 600)
          [self callCustom];
       return true;
    }
    
    - (void) callCustom {
     //do what you need to do
    }
    

    【讨论】:

      【解决方案2】:

      我假设您有一个处理平移手势的方法。使该方法看起来像这样。

      - (void)handlePan:(UIPanGestureRecognizer *)recognizer {
      
          if (recognizer.state == UIGestureRecognizerStateChanged) {
      
              CGPoint movement = [recognizer translationInView:self.view];
      
              if (movement.x >= 500 && movement.x <= 600) {
      
                  [self methodToCall];
              }
          }
      }
      

      【讨论】:

      • 如果我只想方法调用一次怎么办?说我想启动特定的 UIView。根据您给出的建议,只要您的锅仍在继续,它就会继续调用该方法。谢谢!
      • 我会使用一个 BOOL 变量,您在第一次调用该方法时设置为 YES,然后设置一个 if 条件,仅当您的变量设置为 @ 时才调用您的方法987654324@
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-11-29
      • 2011-11-30
      • 2014-07-23
      • 1970-01-01
      • 2016-07-28
      • 1970-01-01
      • 2023-03-29
      相关资源
      最近更新 更多