UIPanGestureRecognizer弹性效果问题

时间:2013-10-19 22:41:50

标签: ios uitableview uipangesturerecognizer

我在单元格中添加了UIPanGestureRecognizer,允许用户水平滑动。当单元格x原点变得超过50时,我希望单元格移动得更慢,就像UIScrollView限制所发生的那样,但是我遇到了一个问题:单元格不按照我应该的方式操作。

我在屏幕上从左到右滑动手指多次,当我将手指放在与起始位置不同的位置时。

这是我用来移动单元格的代码(我的猜测是当我达到50时创建弹性效果的方式有问题):

- (void)slideCell:(UIPanGestureRecognizer *)panGestureRecognizer {
    CGFloat horizontalTranslation = [panGestureRecognizer translationInView:self].x;

    if (panGestureRecognizer.state == UIGestureRecognizerStateChanged) {
        CGFloat retention = 1;

        if (self.frame.origin.x > 50) retention = 5;

        [self setCenter:CGPointMake(self.center.x+horizontalTranslation/retention, self.center.y)];
    }

    [panGestureRecognizer setTranslation:CGPointZero inView:self];
}

1 个答案:

答案 0 :(得分:0)

问题是您一直将翻译重置为零,因此您将失去有关用户触摸移动的绝对距离的知识。如果您更改算法以便根据平移设置框架而不重置(绝对平移),那么当您向后拖动时计算将是准确的。


尝试不同的算法,尝试类似:

CGFloat tx = [panGestureRecognizer translationInView:self].x;

[self setCenter:CGPointMake(originalCenter.x + (tx - (tx > 50 ? sqrt(tx - 50) : 0)), originalCenter.y)];

如果减速太快,请尝试使用powf,以便指定系数。