重写scrollViewWillEndDragging时,UIScrollView并不总是为减速设置动画

3rd*_*Bot 7 paging animation custompaging uiscrollview snapping

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

- (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;
}
Run Code Online (Sandbox Code Playgroud)

当我用手指按住一两秒钟来拖动滚动视图然后抬起我的手指时,它通常会动画到位.然而,当我快速"轻弹"它很少动画 - 它只是捕捉到targetContentOffset.我试图模拟默认的分页行为(尝试捕捉到自定义位置除外).

有任何想法吗?

3rd*_*Bot 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];
}
Run Code Online (Sandbox Code Playgroud)

  • 我尝试使用水平UICollectionView,虽然它工作,但结果动画是紧张的; 我认为有两个动画同时发生.在`dispatch_async`中包装`setContentOffset:`修复了它. (5认同)