Ios UIScrollView-恢复到不带捕捉的起始位置
如果拖动不够,我想还原UIScrollView的内容偏移量:Ios UIScrollView-恢复到不带捕捉的起始位置,ios,cocoa-touch,uitableview,uiscrollview,paging,Ios,Cocoa Touch,Uitableview,Uiscrollview,Paging,如果拖动不够,我想还原UIScrollView的内容偏移量: - (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(CGPoint *)targetContentOffset { self.endingOffset = scrollView.contentOffset; if(abs(verticalOffs
- (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滚动到这个位置,但这似乎并不能解决这个问题
有什么想法吗?你有没有试着记录下速度,以了解发生急动时的情况 编辑: 您可以尝试实现这两个scrollView委托方法,而不是WillendDraging方法。这个解决方案将给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:
如果您也将此添加到我的解决方案中,也许您可以获得更好的结果。。试试看;)
如果我理解正确,它的行为就像反弹动画(也是UIScrollView),所以我认为这是正常的。如果在它计算动画的速度降低时保持在位置a,则它反弹得越快。实际上,我正试图让它在启用分页的情况下表现得像UITableView或UICollectionViewFlowLayout。好的,为什么不在滚动视图上启用分页呢?我有节标题。启用分页时,节标题会偏移视图,并且单元格不会对齐。我决定编写自己的分页功能,并将大部分功能记录下来,直到有用户决定测试它并向我透露这个问题。也许您可以使用–scrollRectToVisible:animated:每次用户滚动到页面的一半或一半以上以进入下一页时(返回时则相反)。并保持分页处于启用状态,这样,如果用户滚动不够,它将执行通常的例程。Hm。我想象速度增加了,但我们如何用它来改变滚动视图移动到目标偏移量的速度呢?赫敏333和我的想法一样。如果速度太高,scrollView将非常快地移动到targetPosition,因此它看起来像是不平稳地反弹。请看我的最新答案以获得解决方案。谢谢,我一下车就会检查。
- (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;
}