当覆盖scrollViewWillEndDragging时,UIScrollView并不总是动画减速

问题描述 投票:0回答:1

这是我的覆盖代码 - 它只是计算捕捉到的位置:

- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset {

    if(targetContentOffset->y < 400) {
        targetContentOffset->y = 0;
        return;
    }

    int baseCheck = 400;

    while(baseCheck <= 10000) {
        if(targetContentOffset->y > baseCheck && targetContentOffset->y < baseCheck + 800) {
            targetContentOffset->y = (baseCheck + 340);
            return;
        }
        baseCheck += 800;
    }

    targetContentOffset->y = 0;
}

当我按住手指超过一两秒来拖动滚动视图然后抬起手指时,它通常会动画到位。然而,当我快速“轻弹”时,它很少有动画 - 它只是捕捉到 targetContentOffset。我正在尝试模拟默认分页行为(除了尝试捕捉到自定义位置)。

有什么想法吗?

animation pagination uiscrollview custompaging snapping
1个回答
6
投票

我最终不得不手动为其设置动画。在同一函数中,我将 targetContentOffset 设置为用户停止的位置(当前 contentOffset),以便它不会触发它自己的动画,然后将 contentoffset 设置为我的计算。此外,我添加了速度检查以触发“自动”页面更改。它并不完美,但希望它能帮助其他人解决同样的问题。

(我修改了上面的函数以提高可读性,因为没有人需要看到我的计算)

- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset {

    CGPoint newOffset = CGPointMake(targetContentOffset->x, targetContentOffset->y);
    *targetContentOffset = CGPointMake([broadsheetCollectionView contentOffset].x, [broadsheetCollectionView contentOffset].y);

    if(velocity.y > 1.4) {
        newOffset.y += pixelAmountThatWillMakeSurePageChanges;
    }
    if(velocity.y < -1.4) {
        newOffset.y -= pixelAmountThatWillMakeSurePageChanges;
    }

    // calculate newoffset

    newOffset.y = calculatedOffset;
    [scrollView setContentOffset:newOffset animated:YES];
}
© www.soinside.com 2019 - 2024. All rights reserved.