UIPangestureRecognizer

UIPangestureRecognizer

本文介绍了UIPanGestureRecognizer 不能顺利移动对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 UIPanGestureRecognizer 来移动我的对象,但我似乎无法让它顺利移动.当我朝特定方向移动并更改为相反方向时,它不会立即做出反应.我认为该值可能有问题,因为它在->"方向为正,在<-"方向为负.

Im trying to use UIPanGestureRecognizer to move my object around, but i cannot seem to get it moving smoothly. When i move in a particular direction and change to the opposite it does not react instantly. I assume that there might be something wrong with the value, since it is positive in the "->" direction and negative in "<-".

我的代码如下:

- (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;
        }
    }

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

Does anybody have a more proper way to solve this task?

提前致谢.

推荐答案

你应该重新设置 UIPanGestureRecognizer 的翻译值:

You should reset the translation value of UIPanGestureRecognizer:

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

这篇关于UIPanGestureRecognizer 不能顺利移动对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-14 13:51