ios 检测 UIScrollView 位置
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14012444/
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
Detecting UIScrollView Position
提问by Gergely Kovacs
I am trying to use a UIScrollView as a navigation device in my iOS application. Basically, I want the application to load specific content, based on the position of a paging-enabled UIScrollView. It is sort of like a navigation wheel.
我试图在我的 iOS 应用程序中使用 UIScrollView 作为导航设备。基本上,我希望应用程序根据启用分页的 UIScrollView 的位置加载特定内容。它有点像导航轮。
Currently, I have a two-page (2*320) scrollview, and I am using the scrollViewDidEndDragging
delegate method and contentoffset.x
to load the appropriate content. I try to determine the position of the scrollview as such:
目前,我有一个两页 (2*320) 的滚动视图,我正在使用scrollViewDidEndDragging
委托方法并contentoffset.x
加载适当的内容。我尝试这样确定滚动视图的位置:
if (scrollView.contentOffset.x < 320) {
// Load content 1
}
else if (scrollView.contentOffset.x >= 320) {
// Load content 2
}
My problem is that I either do not understand how to work with contentoffset or it is not suitable to deal with my problem. Either way, I am stuck. Can someone let me know where I went wrong and/or show me a more efficient way to determine scrollview position?
我的问题是我要么不明白如何使用 contentoffset 要么不适合处理我的问题。无论哪种方式,我都被困住了。有人可以让我知道我哪里出错了和/或告诉我一种更有效的方法来确定滚动视图位置吗?
I am stupid, I forgot to mention what the problem is... Basically, I cannot track the position of the scrollview. When I move to the second page it only registers changes if I go over the 620 limit (bounce rightward). But when I go back to the starting position (page 1), it does register the changes. In other words, my tracking is not accurate.
我很笨,我忘了提到问题是什么......基本上,我无法跟踪滚动视图的位置。当我移动到第二页时,它只会在我超过 620 限制(向右弹跳)时记录更改。但是当我回到起始位置(第 1 页)时,它确实记录了更改。换句话说,我的跟踪不准确。
Delegate and everything else is working fine, I log them!
委托和其他一切正常,我记录了它们!
回答by Mick MacCallum
It's hard to say because you never specifically mentioned what the problem is, but I'll take a couple guesses at it.
很难说,因为你从来没有特别提到问题是什么,但我会猜测一下。
If the delegate method isn't being called at all you need to remember to set the scroll views delegate:
如果根本没有调用委托方法,您需要记住设置滚动视图委托:
[myScrollView setDelegate:self];
[myScrollView setDelegate:self];
Otherwise, the problem with using scrollViewDidEndDragging
to detect the scroll views offset is that it will check the offset at the point where you stop dragging which could be within the destination pages rect, or within the starting pages rect. As an alternative, I'd suggest you use scrollViewDidEndDecelerating
to check the content offset as it will be called as soon as the scroll view comes to a stop at its destination page.
否则,scrollViewDidEndDragging
用于检测滚动视图偏移的问题在于,它会在您停止拖动的位置检查偏移,这可能位于目标页面 rect 内,或者在起始页面 rect 内。作为替代方案,我建议您使用scrollViewDidEndDecelerating
检查内容偏移量,因为一旦滚动视图在其目标页面停止,它就会被调用。
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
if (scrollView.contentOffset.x < 320) {
NSLog(@"%@",NSStringFromCGPoint(scrollView.contentOffset));
}
else if (scrollView.contentOffset.x >= 320) {
NSLog(@"%@",NSStringFromCGPoint(scrollView.contentOffset));
}
}
回答by filou
I think this could help you :). With scrollViewDidScroll
you always get the exact position of your scrollView:
我认为这可以帮助你:)。与scrollViewDidScroll
你总是得到你的滚动视图的确切位置:
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
if (scrollView.contentOffset.x < 320)
{
// Load content 1
}
else if (scrollView.contentOffset.x >= 320)
{
// Load content 2
}
}
NSLog(@"Position: %g", scrollView.contentOffset.x);
NSLog(@"Position: %g", scrollView.contentOffset.x);
Do not forget the delegate UIScrollViewDelegate
in your .h file.
不要忘记UIScrollViewDelegate
.h 文件中的委托。
回答by auerd
Here a solution for getting the current position (page) as integer Value: Use scrollViewDidEndDecelerating: methode:
这是获取当前位置(页面)为整数值的解决方案:使用 scrollViewDidEndDecelerating: 方法:
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
NSLog(@"%i", (int)(_scrollView.contentOffset.x / _scrollView.frame.size.width));
}
回答by Pomme2Poule
If for any reason, you'd like to be notified before the view ended decelerating; for example, to perform animations. You can also use this:
如果出于任何原因,您希望在视图结束减速之前收到通知;例如,执行动画。你也可以使用这个:
func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
// Where the scroll will end (vertically)
let offSetY = targetContentOffset.pointee.y
// I've got a scroll view with paging enabled so I use this to check which "page" is selected.
if offSetY == firstPageView.frame.origin.y {
// Do something…
} else if offSetY == secondPageView.frame.origin.y {
// Do something…
}
}
回答by A.G
Using UIScrollViewDelegate,
使用 UIScrollViewDelegate,
func scrollViewWillBeginDragging(scrollView: UIScrollView){
print("dragging begins")
print("Position: \(scrollView.contentOffset.x) , \(scrollView.contentOffset.y) ")
}