【问题标题】:UIPanGestureRecognizer elastic effect issueUIPanGestureRecognizer 弹性效果问题
【发布时间】:2013-10-19 22:41:49
【问题描述】:

我在单元格中添加了一个 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];
}

【问题讨论】:

    标签: ios uitableview uipangesturerecognizer


    【解决方案1】:

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


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

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

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

    【讨论】:

    • 我也试过这样做,效果很好,但我找不到制作弹性效果的方法。你知道我该怎么做吗?
    • 你为什么不展示那个算法并问那个问题?如果计算出的位置 > 50,则根据您要寻找的效果,根据某些规则(平方根、对数、...)减去一个值。
    • 我没有函数式算法。我认为最好的方法是[self setCenter:CGPointMake(originalCenter.x+horizontalTranslation-0.8*(horizontalTranslation-50), originalCenter.y)];,但感觉并不顺畅...
    • 添加了一个让您入门的想法
    • 它不起作用...我会寻找更好的算法。同时,我将使用[self setCenter:CGPointMake(originalCenter.x+horizontalTranslation-0.8*(horizontalTran‌​slation-50), originalCenter.y)];,它并不完美,但它是一个好方法。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-08-13
    • 2017-03-16
    • 1970-01-01
    • 2011-05-18
    • 2018-04-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多