【问题标题】:UIPanGestureRecognizer not moving object smoothlyUIPanGestureRecognizer 不能平滑移动物体
【发布时间】:2012-03-16 16:17:13
【问题描述】:

我正在尝试使用 UIPanGestureRecognizer 来移动我的对象,但我似乎无法让它顺利移动。当我朝一个特定的方向移动并转向相反的方向时,它不会立即做出反应。我认为该值可能有问题,因为它在“->”方向上是正数,在“

我的代码如下:

- (void)panDetected:(UIPanGestureRecognizer *)panRecognizer
    {    
        CGPoint pointA = [panRecognizer locationInView:self.view];
        CGPoint pointB = [panRecognizer translationInView:self.view];

        if(panRecognizer.state == UIGestureRecognizerStateEnded ||
           panRecognizer.state == UIGestureRecognizerStateChanged) {     
            _camera.x += pointB.x * 0.0001f;
        }
    }

有没有人有更合适的方法来解决这个任务?

提前致谢。

【问题讨论】:

    标签: ios uikit uigesturerecognizer uipangesturerecognizer


    【解决方案1】:

    你应该重置 UIPanGestureRecognizer 的翻译值:

    - (void)panDetected:(UIPanGestureRecognizer *)panRecognizer
    {    
       CGPoint point = [panRecognizer translationInView:self.view];
       _camera.x += point.x * 0.0001f;
       [panRecognizer setTranslation:CGPointZero inView:self.view];
    }
    

    【讨论】:

      【解决方案2】:

      斯威夫特 3:

      func panDetected(recognizer: UIPanGestureRecognizer) {
          let translation = recognizer.translation(in: recognizer.view)
      
          _camera.x += translation.x
          _camera.y += translation.y
      
          recognizer.setTranslation(CGPoint.zero, in: recognizer.view)
      }
      

      不乘以 0.0001f 对我有好处 :)

      【讨论】:

      • .0001f 乘法是因为原始答案使用 .0001f,可能是作为缩放运动的一种方式
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-02-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-12-20
      • 1970-01-01
      相关资源
      最近更新 更多