iOS-scrollViewDidEndDecelerating同时为scrollView和collectionView运行 [英] iOS - scrollViewDidEndDecelerating runs for both scrollView and collectionView
问题描述
我有一个VC,其中包含一个 collectionView
和一个 scrollView
.我通过滚动scrollView将此代码更改为 pageController
的当前页面:
func scrollViewDidEndDecelerating(_ scrollView:UIScrollView){让pageNumber = round(scrollView.contentOffset.x/scrollView.frame.size.width)self.pageController.currentPage = Int(页面编号)}
它对于 scrollView
来说很好用,但问题是当我甚至滚动 collectionView
时,它都会声明并导致 pageController
中发生不必要的更改!>
我该怎么办?
除了上面发布的答案外,您还可以使用视图的tag属性.
只需通过xib或通过代码为滚动视图分配一个标记(Int).
yourScrollView.tag = 10
然后在scrollview委托方法中检查此标记:
func scrollViewDidEndDecelerating(_ scrollView:UIScrollView){如果scrollView.tag == 10 {///您的滚动视图已滚动} 别的 {//集合视图已滚动}}
I have a VC that contains a collectionView
and a scrollView
. I put this code to change current page of pageController
by scrolling in scrollView :
func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
let pageNumber = round(scrollView.contentOffset.x / scrollView.frame.size.width)
self.pageController.currentPage = Int(pageNumber)
}
It works nice for scrollView
but the problem is when i even scroll in collectionView
it declares and causes unwanted changing in pageController
!
What should i do?
In addition to the answers posted above, You can make use of the tag property of the view.
Just assign a tag (Int) to your scrollview either in xib or via code.
yourScrollView.tag = 10
And in the scrollview delegate method check for this tag:
func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
if scrollView.tag == 10 {
///Your scrollview was scrolled
} else {
// Collection view was scrolled
}
}
这篇关于iOS-scrollViewDidEndDecelerating同时为scrollView和collectionView运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!