ios - UIScrollView - 无需快照即可恢复到起始位置

标签 ios cocoa-touch uitableview uiscrollview paging

如果我拖得不够多,我想恢复我的 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 委托(delegate)方法而不是 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;
}

关于ios - UIScrollView - 无需快照即可恢复到起始位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15800111/

相关文章:

ios - 如何修复 iOS 7 状态栏的高度和宽度问题

ios - AFNetworking 使用 URL 设置 UITableViewCell 的 imageView 图像,设置图像后 imageview 框架仍然为 0

ios - Swift:如何设置在 CAKeyFrameAnimation 完成时发生的 Action

objective-c - 在 [tableview reloadData] 之前重新加载 UITableViewController 模型

ios - 如何确定当前正在运行哪些进程

iphone - 当当前选定的表格单元格移出屏幕后,如何向其发送消息?

iphone - 如何将本地通知重复间隔设置为自定义时间间隔?

ios - UIRefreshControl 将在除 iPhone 6+ 以外的所有手机上关闭

ios - UITableView 部分索引无法滚动到搜索栏索引

ios - 防止 UITableView contentInset 属性影响其 backgroundView?