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

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

有任何想法吗?

4

1 回答 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;
}
于 2013-04-03T23:44:43.260 回答