如果拖动得不够,我想恢复 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: 滚动到该位置,但这似乎并不能解决抖动问题。
有什么想法吗?
您是否尝试过记录速度以了解发生急动时的情况?
编辑:
您可以尝试实现这两个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;
}