UIScrollView - 没有吸附效果回到起始位置

5
我想在未拖动足够的情况下恢复我的UIScrollView的内容偏移:
- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(CGPoint *)targetContentOffset {
    self.endingOffset = scrollView.contentOffset;

    if(abs(verticalOffset) > [self cellHeight] / 9) { // If the user scrolled enough distance, attempt to scroll to the next cell
        ...
    } else if(self.nextCellOrigin.y != 0) { // The scroll view is still scrolling and the user didn't drag enough
        ...
    } else { // If the user didn't drag enough
        self.tableView.decelerationRate = UIScrollViewDecelerationRateNormal;
        (*targetContentOffset) = self.startingOffset;
    }
}

回到原始位置的代码在else部分,它总是有效的。然而,当我滚动不够并且手势快速时,它会弹回。如果我只滚动一点点,然后将该位置保持略微长于平常的时间,它就能平滑地恢复。

我在UIScrollView的API参考中没有找到用户触摸时间的任何信息,即使我找到了,也不清楚如何使用它来更改恢复代码的行为。我还尝试使用setContentOffset:animated:将滚动到的位置,但似乎无法解决抖动问题。

有什么想法吗?


如果我理解正确的话,它的行为就像反弹动画(也是UIScrollView),所以我认为这很正常。你越快地滑动它,它就会越快地反弹回来;如果你在某个位置停留一段时间,它计算动画的速度就会减慢。 - InvalidReferenceException
我其实是想让它的行为像带有分页功能的UITableView或UICollectionViewFlowLayout。 - Rey Gonzales
我有一个节标题。当我启用分页时,节标题会偏移视图,单元格不对齐。我决定编写自己的分页功能,并且已经完成大部分工作,直到一个用户决定测试它并向我展示了这个问题。 - Rey Gonzales
如果有人需要其他参考,请查看此链接:http://stackoverflow.com/questions/14311576/pull-down-uiscrollview-and-hold-y-position/29692692#29692692 - Narasimha Nallamsetty
我在这里回答了,如果你需要可以看看...http://stackoverflow.com/questions/14311576/pull-down-uiscrollview-and-hold-y-position/29692692#29692692 - Narasimha Nallamsetty
显示剩余2条评论
1个回答

5

你尝试过记录速度以查找抖动发生时的情况吗?

编辑

你可以尝试实现这两个scrollView代理方法,而不是使用willEndDragging方法。这种解决方案将给scrollView带来不同的感觉,但请试一试。

checkOffset方法填充所有需要的逻辑。

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {

    // if scrollView's contentOffset reached its final position during scroll, it will not decelerate, so you need a check here too
    if (!decelerate) {
       [self checkOffset];
    }
}    

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
   [self checkOffset];
}


- (void)checkOffset {
    CGPoint newOffset;
    ...
    // Do all the logic you need to move the content offset, then:
    ...
    [self.scrollView setContentOffset:newOffset animated:YES];
}

编辑 #2: 如果您将以下内容添加到我的解决方案中,可能会获得更好的结果。请尝试 ;)

- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(CGPoint *)targetContentOffset {
    // This should force the scrollView to stop its inertial deceleration, by forcing it to stop at the current content offset
    *targetContentOffset = scrollView.contentOffset;
}

嗯,我想速度会增加,但我们如何使用它来改变滚动视图移动到目标偏移的速度呢? - Rey Gonzales
@Hermione333和我想的一样。如果速度太快,scrollView将会非常快地移动到目标位置,所以它看起来像是不平稳的弹回。查看我的更新答案以获取解决方案。 - Alessandro Orrù
谢谢,我一下火车就会检查这个。 - Rey Gonzales

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接