停止仅在顶部过度滚动UITableView?

caf*_*num 5 objective-c uitableview ios swift

类似的问题Stop UITableView在顶部和底部滚动?,但我需要略微不同的功能.我想要我的桌子,以便它可以在底部过度滚动,但不能在顶部过度滚动.据我了解,

tableView.bounces = false
Run Code Online (Sandbox Code Playgroud)

允许在顶部和底部禁用过度滚动,但是,我只需要在顶部禁用此功能.喜欢

tableView.bouncesAtTheTop = false
tableView.bouncesAtTheBottom = true
Run Code Online (Sandbox Code Playgroud)

Moh*_*shi 9

对于Swift 2.2,使用

func scrollViewDidScroll(scrollView: UIScrollView) {
    if scrollView == self.tableView {
        if scrollView.contentOffset.y <= 0 {
            scrollView.contentOffset = CGPoint.zero
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

对于Objective C

    -(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    if (scrollView.contentOffset.y<=0) {
        scrollView.contentOffset = CGPointZero;
    }
}
Run Code Online (Sandbox Code Playgroud)

}


ore*_*ren 3

bounce您可以通过更改tableView的属性来实现它scrollViewDidScroll(您需要成为tableView的委托)

最后Y有一个属性:

var lastY: CGFloat = 0.0
Run Code Online (Sandbox Code Playgroud)

将初始反弹设置为 false viewDidLoad

tableView.bounces = false
Run Code Online (Sandbox Code Playgroud)

和:

func scrollViewDidScroll(scrollView: UIScrollView) {
    let currentY = scrollView.contentOffset.y
    let currentBottomY = scrollView.frame.size.height + currentY
    if currentY > lastY {
        //"scrolling down"
        tableView.bounces = true
    } else {
        //"scrolling up"
        // Check that we are not in bottom bounce
        if currentBottomY < scrollView.contentSize.height + scrollView.contentInset.bottom {
            tableView.bounces = false
        }
    }
    lastY = scrollView.contentOffset.y
}
Run Code Online (Sandbox Code Playgroud)