问题描述
我正在使用 UIPageViewController
,我需要在用户滑动时获取ViewController的滚动位置,这样我可以在视图转换到的时候部分淡化某些资源下一个 UIViewController
。
I am using a UIPageViewController
, and I need to get the scroll position of the ViewController as the users swipe so I can partially fade some assets while the view is transitioning to the next UIViewController
.
UIPageViewController的委托和数据源方法
似乎没有提供对此的任何访问,并且在内部我假设 UIPageViewController
必须在某处使用滚动视图,但它似乎没有直接子类化它所以我无法调用
The delegate and datasource methods of UIPageViewController
don't seem to provide any access to this, and internally I'm assuming that the UIPageViewController
must be using a scroll view somewhere, but it doesn't seem to directly subclass it so I'm not able to call
func scrollViewDidScroll(scrollView: UIScrollView) {
}
我看过一些其他帖子建议获取对 pageViewController!.view.subviews 然后第一个索引是一个scrollView,但这似乎非常 hacky。我想知道是否有更标准的方法来处理这个问题。
I've seen some other posts suggestion to grab a reference to the pageViewController!.view.subviews
and then the first index is a scrollView, but this seems very hacky. I'm wondering if there is a more standard way to handle this.
推荐答案
你可以在你的<$里面搜索UIScrollView C $ C> UIPageViewController 。要做到这一点,你必须实现 UIScrollViewDelegate
。
You can search for the UIScrollView inside your UIPageViewController
. To do that, you will have to implement the UIScrollViewDelegate
.
之后你可以得到你的scrollView:
After that you can get your scrollView:
for v in pageViewController.view.subviews{
if v.isKindOfClass(UIScrollView){
(v as UIScrollView).delegate = self
}
}
之后,你是能够使用所有UIScrollViewDelegate方法,因此您可以覆盖 scrollViewDidScroll
方法,您可以在其中获取scrollPosition:
After that, you are able to use all the UIScrollViewDelegate-methods and so you can override the scrollViewDidScroll
method where you can get the scrollPosition:
func scrollViewDidScroll(scrollView: UIScrollView) {
//your Code
}
或者如果你想要单行:
let scrollView = view.subviews.filter { $0 is UIScrollView }.first as! UIScrollView
scrollView.delegate = self
这篇关于获取UIPageViewController的滚动位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!