【问题标题】:How to get current touch point and previous touch point in UIPanGestureRecognizer method?如何在 UIPanGestureRecognizer 方法中获取当前触摸点和上一个触摸点?
【发布时间】:2012-11-07 12:45:09
【问题描述】:

我是 iOS 新手,我在我的项目中使用 UIPanGestureRecognizer。在拖动视图时,我需要获取当前的接触点和上一个接触点。我正在努力获得这两点。

如果我使用touchesBegan 方法而不是使用UIPanGestureRecognizer,我可以通过以下代码得到这两点:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    CGPoint touchPoint = [[touches anyObject] locationInView:self];
    CGPoint previous=[[touches anyObject]previousLocationInView:self];
}

我需要在UIPanGestureRecognizer事件触发方法中得到这两点。我怎样才能做到这一点?请指导我。

【问题讨论】:

    标签: ios objective-c uipangesturerecognizer uitouch touchesbegan


    【解决方案1】:

    你可以用这个:

    CGPoint currentlocation = [recognizer locationInView:self.view];
    

    如果没有找到,则通过设置当前位置并每次添加当前位置来存储以前的位置。

    previousLocation = [recognizer locationInView:self.view]; 
    

    【讨论】:

      【解决方案2】:

      当您将UIPanGestureRecognizer 链接到 IBAction 时,每次更改都会调用该操作。手势识别器还提供了一个名为state 的属性,它指示它是第一个UIGestureRecognizerStateBegan、最后一个UIGestureRecognizerStateEnded 还是只是UIGestureRecognizerStateChanged 之间的一个事件。

      要解决您的问题,请尝试以下方法:

      - (IBAction)panGestureMoveAround:(UIPanGestureRecognizer *)gesture {
          if ([gesture state] == UIGestureRecognizerStateBegan) {
              myVarToStoreTheBeganPosition = [gesture locationInView:self.view];
          } else if ([gesture state] == UIGestureRecognizerStateEnded) {
             CGPoint myNewPositionAtTheEnd = [gesture locationInView:self.view];
             // and now handle it ;)
          }
      }
      

      你也可以看看名为translationInView:的方法。

      【讨论】:

        【解决方案3】:

        如果你不想存储任何东西,你也可以这样做:

        let location = panRecognizer.location(in: self)
        let translation = panRecognizer.translation(in: self)
        let previousLocation = CGPoint(x: location.x - translation.x, y: location.y - translation.y)
        

        【讨论】:

          【解决方案4】:

          UITouch中有一个函数可以获取视图中的上一次触摸

          • (CGPoint)locationInView:(UIView *)view;
          • (CGPoint)previousLocationInView:(UIView *)view;

          【讨论】:

            【解决方案5】:

            您应该按如下方式实例化您的平移手势识别器:

            UIPanGestureRecognizer* panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)];
            

            那么你应该将 panRecognizer 添加到你的视图中:

            [aView addGestureRecognizer:panRecognizer];
            

            - (void)handlePan:(UIPanGestureRecognizer *)recognizer 方法将在用户与视图交互时被调用。在handlePan: 中,你可以得到这样的触摸点:

            CGPoint point = [recognizer locationInView:aView];
            

            还可以获取panRecognizer的状态:

            if (recognizer.state == UIGestureRecognizerStateBegan) {
                //do something
            } else if (recognizer.state == UIGestureRecognizerStateEnded) {
               //do something else
            }
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2013-09-01
              • 2014-04-18
              相关资源
              最近更新 更多