【发布时间】:2012-07-17 05:24:50
【问题描述】:
有什么方法可以知道UITableView是向上还是向下滚动?
【问题讨论】:
标签: iphone ios ipad uitableview
有什么方法可以知道UITableView是向上还是向下滚动?
【问题讨论】:
标签: iphone ios ipad uitableview
-(void) scrollViewDidScroll:(UIScrollView *)scrollView
{
CGPoint currentOffset = scrollView.contentOffset;
if (currentOffset.y > self.lastContentOffset.y)
{
// Downward
}
else
{
// Upward
}
self.lastContentOffset = currentOffset;
}
【讨论】:
-(void)scrollViewWillEndDragging:(UIScrollView *)scrollView
withVelocity:(CGPoint)velocity
targetContentOffset:(inout CGPoint *)targetContentOffset{
if (velocity.y > 0){
NSLog(@"up");
}
if (velocity.y < 0){
NSLog(@"down");
}
}
【讨论】:
targetContentOffset (那是动画结束后滚动结束的位置):if (scrollView.contentOffset.y<0){ // scroll is above 0,0 aka negative space where the refreshview would be }
我们可以这样做吗?
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
if ([scrollView.panGestureRecognizer translationInView:scrollView].y > 0) {
// down
} else {
// up
}
}
【讨论】:
UITableView 是UIScrollView 的子类,因此您可以将自己设置为UIScrollViewDelegate 并获取滚动视图委托回调。
这些委托方法之一 (-scrollViewDidScroll:) 的参数是滚动的滚动视图,您可以将它与您的表格视图进行比较,以了解滚动的滚动视图。
对不起,我看错了你的问题。我以为你想知道 哪个 表视图正在滚动(我错过了“方式”)。
要知道方向,您可以将先前的偏移量保留在变量中,并查看增量(current.y - previous.y)是正数(向下滚动)还是负数(向上滚动)。
【讨论】:
您可以跟踪内容偏移量的差异。将旧的保留在成员/静态变量中并检查当前。如果旧值较低,则滚动向下,反之亦然。
【讨论】:
override func scrollViewWillEndDragging(scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
if targetContentOffset.memory.y < scrollView.contentOffset.y {
//println("Going up!")
} else {
// println("Going down!")
}
}
【讨论】:
你可以通过这种方式实现UIScrollView的委托方法来做到这一点,很优雅。
PS:lastOffset 和 scrollingUpward 是 ViewController 的属性。
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
CGPoint currentOffset = scrollView.contentOffset;
self.scrollingUpward = currentOffset.y > self.lastOffset.y;
self.lastOffset = currentOffset;
}
【讨论】:
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
if (yourTableView.isDragging || yourTableView.isDecelerating)
{
// your tableview is scrolled.
// Add your code here
}
}
这里你必须替换你的tableview名称而不是“yourTableView”。
yourTableView.isDragging - 如果用户开始滚动,则返回 YES。这可能需要一些时间或距离才能开始。
yourTableView.isDecelerating - 如果用户没有拖动(触摸向上)但滚动视图仍在移动,则返回 YES。
【讨论】: