嵌套的UICollectionView和Responder链

时间:2017-07-28 15:24:59

标签: ios swift

我有两个UICollectionVews

  • 其中一个(父级)是一个全屏单元格的分页集合视图。

  • 另一个(孩子一个)是"页面内的过滤器" 两者都有相同的滚动方向

我的问题是,当我滚动孩子一个,它到达结束时,父母开始移动。我想避免这种情况。我尝试了很多东西

* ScrollView委托 *的touchesBegan

有什么想法吗? 谢谢!

2 个答案:

答案 0 :(得分:0)

我觉得这很容易。 设置父UICollectionView

console.log(`|${graphTitle.style.fontSize}|`); //outputs ||

或者听起来不合理。如果父UICollectionView可以滚动,您如何归档目标?因为UICollectionView由单元格组成(子单元格)。单元格将不可避免地影响父UICollectionView。

也许孩子一个位于单元格的一部分,你可以使用

collectionView.scrollEnabled = NO; 的方法:UIScrollViewDelegate,用于设置父collectionView的scrollViewDidScroll属性。

答案 1 :(得分:0)

我认为你更喜欢这个答案。 我们知道泛手势识别器是在UIScrollView中构建的。 我们可以添加其他协调的平移手势。

  

因为苹果说:' UIScrollView的内置平移手势识别器必须有它的   滚动视图作为其代理。'

        let pan = UIPanGestureRecognizer(target: self, action: #selector(ViewController.panGestureRecognizerAction(recognizer:)))
          pan.delegate = self
          mainScrollView.addGestureRecognizer(pan)

          func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, 
                                shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
                return true
            }
     // So we can get the argus of the pan gesture while not affecting the scroll 
     after the setting. 

            var mainScrollEnabled = false
            var subScrollEnabled = false
    // Then we define two BOOL values to identify the scroll of the collectionView    

        func scrollViewDidScroll(_ scrollView: UIScrollView) {
                if scrollView == mainScrollView {
                    if scrollView.contentOffset.y >= maxOffsetY {
                        scrollView.setContentOffset(CGPoint(x: 0, y: maxOffsetY), animated: false)
                        mainScrollView.isScrollEnabled = false
                        subScrollView.isScrollEnabled = true
                        subScrollEnabled = true
                         mainScrollEnabled = false
                    }
                }else {
                    if scrollView.contentOffset.y <= 0 {
                        scrollView.setContentOffset(CGPoint(x: 0, y: 0), animated: false)
                        subScrollView.isScrollEnabled = false
                        mainScrollView.isScrollEnabled = true
                        mainScrollEnabled = true
                        subScrollEnabled = false
                    }
                }
            }

// Then we handle the situation that the collectionView reaches the end , by the pan gesture's recognizer .    
            var currentPanY: CGFloat = 0

            func panGestureRecognizerAction(recognizer: UIPanGestureRecognizer) {
                    if recognizer.state != .changed{
                        currentPanY = 0
                        // clear the data of last time after finishing the scroll 
                        mainScrollEnabled = false
                        subScrollEnabled = false
                    }else {
                       let currentY = recognizer.translation(in: mainScrollView).y
                       // So the collectionView reaches the end.
                        if mainScrollEnabled || subScrollEnabled {
                            if currentPanY == 0 {
                                currentPanY = currentY  //get the y
                            }
                            let offsetY = currentPanY - currentY //get the offsetY

                            if mainScrollEnabled {
                               let supposeY = maxOffsetY + offsetY
                                if supposeY >= 0 {
                                    mainScrollView.contentOffset = CGPoint(x: 0, y: supposeY)
                                }else {
                                    mainScrollView.contentOffset = CGPoint.zero
                                }
                            }else {
                                subScrollView.contentOffset = CGPoint(x: 0, y: offsetY)
                            }
                        }
                    }
                }
相关问题