UIScrollView - 在没有快照的情况下恢复到起始位置

Posted

技术标签:

【中文标题】UIScrollView - 在没有快照的情况下恢复到起始位置【英文标题】:UIScrollView - Revert to starting position without snaps 【发布时间】:2013-04-03 23:37:01 【问题描述】:

如果拖得不够,我想恢复 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 部分,它总是有效的。但是,当我没有足够的滚动并快速做出手势时,它会弹回来。如果我稍微滚动一下,然后在该位置保持比平时稍长的时间,它就会顺利恢复。

我没有在 API 参考中找到任何关于用户触摸 UIScrollView 多长时间的内容,即使我这样做了,我也不能立即明白如何使用它来更改我的还原代码的行为。我也尝试使用 setContentOffset:animated: 滚动到该位置,但这似乎无法解决抖动问题。

有什么想法吗?

【问题讨论】:

如果我理解正确,它的行为就像弹跳动画(也是 UIScrollView),所以我认为这是正常的。你越快它反弹的速度越快,如果你保持在一个位置一段时间,它计算动画的速度就会降低。 我实际上是想让它表现得像启用了分页的 UITableView 或 UICollectionViewFlowLayout。 好的,为什么不在滚动视图上启用分页? 我有部分标题。当我启用分页时,部分标题会偏移视图并且单元格未对齐。我决定编写自己的分页功能并完成大部分功能,直到用户决定对其进行测试并向我揭示这个问题。 也许你可以使用——scrollRectToVisible:animated:每次用户滚动到页面的一半或更多以进入下一个(返回相反)。并保持分页启用,这样如果用户没有足够的滚动,它将执行通常的例程。 【参考方案1】:

您是否尝试过记录速度以了解急动发生时的情况?

编辑

您可以尝试实现这两个 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;

【讨论】:

嗯。我想速度会增加,但是我们如何使用它来改变滚动视图移动到目标偏移量的速度。 @Hermione333 和我的想法一样。如果速度太高,scrollView 会非常快地移动到 targetPosition,所以看起来回弹不顺畅。查看我的更新答案以获取解决方案。 谢谢,我一下车就去看看。

以上是关于UIScrollView - 在没有快照的情况下恢复到起始位置的主要内容,如果未能解决你的问题,请参考以下文章

在 UIScrollView 中拍摄 CATiledLayer 支持的视图的图像快照

Azure Fileshare - 在没有备份服务的情况下还原快照

Swift UIScrollView 在没有 pagingEnabled 的情况下捕捉到子视图宽度

如何在没有标记的情况下获取 forgeviewer 的快照

Android LibVLC,在没有 TextureView 的情况下拍摄 RTSP 流的快照

在启用分页的情况下使用 UIScrollView