iOS ScrollView嵌套ScrolloView滑动处理 swift

本文demo下载地址   DFLinkageViewDemo

实现效果:如下

层级结构:

iOS ScrollView嵌套ScrolloView滑动处理 swift_第1张图片

 

核心内容;

1.让外层scrollView和内层scrollview同时滚动scroll

DFLinkageScrollView 内部实现

public func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
        return true
    }

2.两个scrollview滚动的时候改变他们的contentoffset

public func scrollViewDidScroll(_ scrollView: UIScrollView) {
        if scrollView == self {
            contentViewDidScroller(view: scrollView)
        } else if scrollView == plateContentView {
            plateContentViewDidScroll(view: scrollView)
        } else {
            linkageViewDidScroll(view: scrollView)
        }
    }

    func contentViewDidScroller(view: UIScrollView) {
        //标签常悬
        let y = view.contentOffset.y
        let headerY = _headerHeight()
        if canPlateScroll {
            self.contentOffset = CGPoint(x: 0, y: headerY)
        } else if y >= headerY {
            canPlateScroll = true
        }
    }
    
    func linkageViewDidScroll(view: UIScrollView) {
        if !canPlateScroll {
            view.contentOffset = CGPoint.zero
        } else if view.contentOffset.y <= 0 {
            canPlateScroll = false
        }
    }

以上逻辑封装在DFLinkageScrollView中。在使用的时候不用关注

 

使用步骤

1. 初始化控件

2.设置headerView--需要设置frame!!

3.设置plateTitleArr--悬停部分显示内容

4.设置plateControllers--底部collectionView的cell显示内容

func setupView() {
        contentView.frame = CGRect(x: 0, y: DFHeaderMargin, width: DFScreenWidth, height: DFScreenHeight - DFHeaderMargin);
        self.view.addSubview(contentView)
        
        imageHeader.frame = CGRect(x: 0, y: 0, width: DFScreenWidth, height: DFHeaderHeight);
        self.contentView.contentHeader = imageHeader
        
        let titleArr = ["精选","图片","汽车","科技","美女"]
        
        contentView.plateTitleArr = titleArr as NSArray
        
        let imageArr = ["boutique.jpg","picture.jpg","car.jpg","science.jpg","jialing.jpeg"]
        
        var controllerArr = Array()
        for i in 0..

5.DemoPageViewController中设置linkageView  --DFLinkageScrollView中设置代理用

linkageView为UIViewController的分类属性

func setupView() {
        self.linkageView = contentView
        self.contentView.frame = CGRect(x: 0, y: 0, width: DFScreenWidth, height: DFScreenHeight - 51 - DFHeaderMargin )
        self.view.addSubview(contentView)
        colorView.frame = CGRect(x: 0, y: 25, width: DFScreenWidth, height: 100)
        colorView.backgroundColor = randomColor()
        
        self.contentView.addSubview(self.colorView)
        
        for i in 0..<4 {
            let imageView = UIImageView.init()
            imageView.image = UIImage(named: imageName ?? "jialing.jpeg")
            imageView.frame = CGRect(x: 0, y: 150 + i*200, width: Int(DFScreenWidth), height: 200)
            self.contentView.addSubview(imageView)
        }
        
        self.contentView.contentSize = CGSize(width: DFScreenWidth, height: 950)
        
    }

 

最后command +R 就OK

你可能感兴趣的:(iOS ScrollView嵌套ScrolloView滑动处理 swift)