iPhone - 知道UIScrollView是否达到顶部或底部
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了iPhone - 知道UIScrollView是否达到顶部或底部相关的知识,希望对你有一定的参考价值。
有没有办法知道UIScrollView
是否到达顶部或底部内部
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView
willDecelerate:(BOOL)decelerate
?
谢谢。
在你的类中实现UIScrollViewDelegate
,然后添加:
-(void)scrollViewDidScroll: (UIScrollView*)scrollView
{
float scrollViewHeight = scrollView.frame.size.height;
float scrollContentSizeHeight = scrollView.contentSize.height;
float scrollOffset = scrollView.contentOffset.y;
if (scrollOffset == 0)
{
// then we are at the top
}
else if (scrollOffset + scrollViewHeight == scrollContentSizeHeight)
{
// then we are at the end
}
}
希望这就是你所追求的!否则,通过向上面的代码添加更多条件并使用NSLog scrollOffset的值来修改。
当你试图确定scrollView是在顶部还是在底部时,contentInsets
也参与其中。当您的scrollView位于顶部上方和底部下方时,您可能也会感兴趣。这是我用来查找顶部和底部位置的代码:
迅速:
extension UIScrollView {
var isAtTop: Bool {
return contentOffset.y <= verticalOffsetForTop
}
var isAtBottom: Bool {
return contentOffset.y >= verticalOffsetForBottom
}
var verticalOffsetForTop: CGFloat {
let topInset = contentInset.top
return -topInset
}
var verticalOffsetForBottom: CGFloat {
let scrollViewHeight = bounds.height
let scrollContentSizeHeight = contentSize.height
let bottomInset = contentInset.bottom
let scrollViewBottomOffset = scrollContentSizeHeight + bottomInset - scrollViewHeight
return scrollViewBottomOffset
}
}
Objective-C的:
@implementation UIScrollView (Additions)
- (BOOL)isAtTop {
return (self.contentOffset.y <= [self verticalOffsetForTop]);
}
- (BOOL)isAtBottom {
return (self.contentOffset.y >= [self verticalOffsetForBottom]);
}
- (CGFloat)verticalOffsetForTop {
CGFloat topInset = self.contentInset.top;
return -topInset;
}
- (CGFloat)verticalOffsetForBottom {
CGFloat scrollViewHeight = self.bounds.size.height;
CGFloat scrollContentSizeHeight = self.contentSize.height;
CGFloat bottomInset = self.contentInset.bottom;
CGFloat scrollViewBottomOffset = scrollContentSizeHeight + bottomInset - scrollViewHeight;
return scrollViewBottomOffset;
}
@end
如果你想要swift中的代码:
override func scrollViewDidScroll(scrollView: UIScrollView) {
if (scrollView.contentOffset.y >= (scrollView.contentSize.height - scrollView.frame.size.height)) {
//reach bottom
}
if (scrollView.contentOffset.y <= 0){
//reach top
}
if (scrollView.contentOffset.y > 0 && scrollView.contentOffset.y < (scrollView.contentSize.height - scrollView.frame.size.height)){
//not top and not bottom
}
}
简单而干净的扩展:
import UIKit
extension UIScrollView {
var scrolledToTop: Bool {
let topEdge = 0 - contentInset.top
return contentOffset.y <= topEdge
}
var scrolledToBottom: Bool {
let bottomEdge = contentSize.height + contentInset.bottom - bounds.height
return contentOffset.y >= bottomEdge
}
}
在GitHub上:
https://github.com/salutis/swift-scroll-view-edges
我弄明白该怎么做:
CGFloat maxPosition = scrollView.contentInset.top + scrollView.contentSize.height + scrollView.contentInset.bottom - scrollView.bounds.size.height;
CGFloat currentPosition = scrollView.contentOffset.y + self.topLayoutGuide.length;
if (currentPosition == maxPosition) {
// you're at the bottom!
}
这样做(对于Swift 3):
class MyClass: UIScrollViewDelegate {
func scrollViewDidScroll(_ scrollView: UIScrollView) {
if (scrollView.contentOffset.y >= (scrollView.contentSize.height - scrollView.frame.size.height)) {
//scrolled to top, do smth
}
}
}
以上是关于iPhone - 知道UIScrollView是否达到顶部或底部的主要内容,如果未能解决你的问题,请参考以下文章
UIScrollView scrollToTop 没有被 iPhone 调用
iphone:UIScrollView 分页启用,无缩放和无预览
iPhone Page Control 仅显示在 UIScrollView 的第一页上
iPhone 下拉刷新像 Tweetie - LIGHT VERSION
如何在 iPhone SDK 中使用 Objective C 实现 UIViewAutoresizingFlexibleHeight 到 UIScrollView?