ios iPhone - 知道 UIScrollView 是否到达顶部或底部
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7706152/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
iPhone - knowing if a UIScrollView reached the top or bottom
提问by DuckDucking
Is there a way to know if a UIScrollView
has reached the top or bottom inside
有没有办法知道 aUIScrollView
是否到达了内部的顶部或底部
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView
willDecelerate:(BOOL)decelerate
?
?
thanks.
谢谢。
回答by Luke
Implement the UIScrollViewDelegate
in your class, and then add this:
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
}
}
Hope this is what you are after! Else have a tinker by adding more conditions to the above code and NSLog the value of scrollOffset.
希望这就是你所追求的!或者通过向上述代码添加更多条件和 NSLog scrollOffset 的值来修补。
回答by Alexander Dvornikov
Well, contentInsets
are also involved, when you try to determine whether scrollView is at the top or at the bottom. You might also be interested in cases when your scrollView is abovethe top and belowthe bottom.
Here is the code I use to find top and bottom positions:
嗯,contentInsets
也涉及到了,当你尝试判断scrollView是在顶部还是底部的时候。您可能也有兴趣的情况下,当你滚动视图是上方的顶部和下方的底部。这是我用来查找顶部和底部位置的代码:
Swift:
迅速:
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:
目标-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
回答by ytbryan
If you want the code in 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
}
}
回答by Rudolf Adamkovi?
Simple and clean extension:
简单干净的扩展:
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
}
}
On GitHub:
在 GitHub 上:
回答by JPN
I figured out exactly how to do it:
我想出了确切的方法:
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!
}
回答by Radu Ghitescu
Do it like this(for Swift 3):
这样做(对于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
}
}
}