iOS 头条首页滚动式页面实现 -- swift

2019-07-12  本文已影响0人  huxinwen

在平时项目开发过程中经常会遇见类似头条首页滚动式需求,效果如下:


未命名s.gif

最近在熟悉swift,就用swift语言封装了一个这样的工具,欢迎大神指正。

实现思路:

init(frame: CGRect, numbersOfContentView: ()->Int, contentViewAtIndex: (_ index:Int, _ contentFrame:CGRect) -> UIView, titles:[String]) {
        super.init(frame: frame)
        let titleScro = HXWLabelScrollView.init(frame: CGRect(x: 0.0, y: 0.0, width: bounds.width, height: 50.0), numberOfLables: numbersOfContentView, titles: titles)///title view
        titleScro.labelScrollViewDelegate = self
        addSubview(titleScro)
        titleSc = titleScro
        let contenScro = HXWDisplayContentView.init(frame: CGRect(x: 0.0, y: 50.0, width: bounds.width, height: bounds.height-50.0), numbersOfContentView: numbersOfContentView, contentViewAtIndex: contentViewAtIndex)///content view
        contenScro.isPagingEnabled = true
        contenScro.delegate = self
        addSubview(contenScro)
        contentSc = contenScro
    }
///点击title回调HXWLabelScrollViewDelegate
extension HXWSelectScrollView :HXWLabelScrollViewDelegate{
    
    func didSelectTitleAtIndex(_ index: Int) {
        currentPage = index;
        scrollContentToPage(page: index)
    }
    func scrollContentToPage(page:Int) {///将内容更新的对应的title页面
        let offset = UIScreen.main.bounds.width*CGFloat(page)
        self.contentSc?.contentOffset = CGPoint(x: offset, y: 0.0)
    }
}
///滑动内容页面回调UIScrollViewDelegate
extension HXWSelectScrollView :UIScrollViewDelegate{
    
    func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
        print( "scrollViewDidEndDragging + \(scrollView.contentOffset.x) + \(decelerate)")
        didDraging = true
        
    }
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        if didDraging {///手滑动页面
            print("scrollViewDidScroll + \(scrollView.contentOffset.x)")
            let currentOffset = CGFloat(currentPage)*UIScreen.main.bounds.width
            let scrollOffset = scrollView.contentOffset.x
            let offset = scrollOffset - currentOffset
            print("\(offset)")
            if offset > UIScreen.main.bounds.width/2.0 {//右翻
                currentPage += 1
                titleSc?.scrollToIndex(index: currentPage)
            }else if offset < UIScreen.main.bounds.width/2.0*(-1.0){///左翻
                currentPage -= 1
                titleSc?.scrollToIndex(index: currentPage)
            }else if offset == 0{//没变
            }
            didDraging = false
        }
    }
}

demo的调用如下:

 override func viewDidLoad() {
        super.viewDidLoad()
        let scro = HXWSelectScrollView.init(frame: view.bounds, numbersOfContentView: { () -> Int in
             return 16///数量
        }, contentViewAtIndex: { (index,contentBounds) -> UIView in///内容试图
            let view = UIView.init()
            view.backgroundColor = UIColor.lightGray
            let conten = UILabel.init()
            view.addSubview(conten)
            conten.font = UIFont.systemFont(ofSize: 30.0)
            conten.text = "hello \(index)"
            conten.textAlignment = NSTextAlignment.center
            conten.frame = contentBounds
            return view
        }, titles: ["hello","hello","hello","hello","hello","hello","hello","hello","hello","hello","hello","hello","hello","hello","hello","hello"])///title名称
        
        view.addSubview(scro)
        scroView = scro
    }

具体见demo,欢迎指正!!!

上一篇下一篇

猜你喜欢

热点阅读