HJPageView拓展 - 礼物界面和表情界面的搭建

首先查看我们的需要实现的动态效果图:
标题在上面的显示效果
标题在上.gif
标题在下面的显示效果
标题在下.gif

在UI的设置中需要注意的是titleY值、PageControlY值以及CollectionViewY值需要根据我们的需求来决定我们的title是显示在上或下

extension HJPageCollectionView {
    
    fileprivate func setupUI(){
    
        // 1.创建titleView
        let titleY = isTitleInTop ? 0 : bounds.height - style.titleHeight
        let titleFrame = CGRect(x: 0, y: titleY, width: bounds.width, height: style.titleHeight)
            titleView = HJTitleView(frame: titleFrame, titles: titles, style: style)
        titleView.delegate = self
        titleView.backgroundColor = UIColor.randomColor()
        addSubview(titleView)
        
        // 2.创建UIPageControl
        let pageHeight : CGFloat = 20
        let pageY = isTitleInTop ? (bounds.height - pageHeight) :(bounds.height - pageHeight - style.titleHeight)
        let pageFrame = CGRect(x: 0, y: pageY, width: bounds.width, height: pageHeight )
           pageControl = UIPageControl(frame: pageFrame)
        pageControl.numberOfPages = 4
        pageControl.isEnabled = false
        pageControl.backgroundColor = UIColor.randomColor()
        addSubview(pageControl)
        
        // 3.创建CollectionView
        let collectionY = isTitleInTop ? style.titleHeight : 0
        let collectionFrame = CGRect(x: 0, y: collectionY, width: bounds.width, height: bounds.height - pageHeight - style.titleHeight)
        collectionView = UICollectionView(frame: collectionFrame, collectionViewLayout: layout)
        collectionView.backgroundColor = UIColor.randomColor()
        collectionView.isPagingEnabled = true
        collectionView.showsHorizontalScrollIndicator = false
        collectionView.bounces = false
        collectionView.dataSource = self
        collectionView.delegate = self
       
        
        addSubview(collectionView)
    }

}

自定义FlowLayoutHJPageCollectionLayout中的重新布局,重写prepare()方法(override func prepare())以及对应的每一个Cell方法override fund layoutAttributesForElements还有override var collectionViewContentSize: CGSize内容高度的方法

import UIKit

class HJPageCollectionLayout: UICollectionViewFlowLayout {
    
        var cols : Int = 4
        var rows : Int = 2
    fileprivate lazy var attricell : [UICollectionViewLayoutAttributes] = [UICollectionViewLayoutAttributes]()
    fileprivate lazy var maxWidth : CGFloat = 0
}

extension HJPageCollectionLayout {
    
    override func prepare() {
        super.prepare()
        
        // 0. 计算item的宽度&高度
        let itemW = (collectionView!.bounds.width - sectionInset.left - sectionInset.right - minimumInteritemSpacing * CGFloat(cols - 1)) / CGFloat(cols)
        let itemH = (collectionView!.bounds.height - sectionInset.top - sectionInset.bottom - minimumLineSpacing * CGFloat(rows - 1)) / CGFloat(rows)
        
        // 1. 获取有多少section
        let sectionCount = collectionView!.numberOfSections
        
        // 2. 每个section中有多少Item
        var prePageCount : Int = 0
        for i in 0..<sectionCount {
            
            let itemCount = collectionView!.numberOfItems(inSection: i)
            
            for j in 0..<itemCount {
                
                // 2.1 获取cell 对应的indexpath
                let indexpath = IndexPath(item: j, section: i)
                
                // 2.2 根据indexpath创建对应的UICollectionViewLayoutAttributes
                let attri = UICollectionViewLayoutAttributes(forCellWith: indexpath)
                
                // 2.3 计算j在该组中第几页
                let page = j / (cols * rows)
                let index = j % (cols * rows)
                
                let itemY = sectionInset.top + (itemH + minimumLineSpacing) * CGFloat(index / cols)
                let itemX = CGFloat(prePageCount + page) * collectionView!.bounds.width + sectionInset.left + (itemW + minimumInteritemSpacing) * CGFloat(index % cols)
                
                attri.frame = CGRect(x: itemX, y: itemY, width: itemW, height: itemH)
                
                attricell.append(attri)
            }
            
            prePageCount += (itemCount - 1) / (cols * rows) + 1
        }
        
        // 3.计算最大宽度
        maxWidth = CGFloat(prePageCount) * collectionView!.bounds.width

    }

}

extension HJPageCollectionLayout {

    override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {

        return attricell
    }
}

extension HJPageCollectionLayout {
    
    override var collectionViewContentSize: CGSize{
    
        return CGSize(width: maxWidth, height: 0)
    }
}

通过UICollectionView继承自UIScrollView中的scrollViewDidEndDecelerating scrollViewDidEndDragging两个滚动方法来tiltView的滚动到相应的位置,cell所在的位置let point = CGPoint(x: layout.sectionInset.left + collectionView.contentOffset.x + 1, y: layout.sectionInset.top + 1)计算出Cell当前所在indexPath.sectionindexPath.Item的位置

//MARK: -UICollectionViewDelegate
extension HJPageCollectionView : UICollectionViewDelegate {

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        delegate?.pageCollectionView(self, didSelectItemAt: indexPath)
    }
    
    func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
        
        scrollViewEndscroll()
         scrollView.isScrollEnabled = true
    }
    
    func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
        
        if !decelerate {
        
            scrollViewEndscroll()
        } else {
            scrollView.isScrollEnabled = false
        }
    }
    
       fileprivate func scrollViewEndscroll() {
        
        // 1. 取出cell当前的位置
        let point = CGPoint(x: layout.sectionInset.left + collectionView.contentOffset.x + 1, y: layout.sectionInset.top + 1)
        guard  let cellindexPath = collectionView.indexPathForItem(at: point) else { return }
        
        // 2. 判断分组是否发生了变化
        if sourceIndexpath.section != cellindexPath.section {
            // 2.1修改pageControl的个数
            let itemCount = datasource?.pageCollectionView(self, numberOfItemsInSection: cellindexPath.section) ?? 0
            
            pageControl.numberOfPages = (itemCount - 1) / (layout.cols * layout.rows) + 1
            
            // 2.2调整titleView的位置
            titleView.setTitleWithProgress(progress: 1.0, sourceIndex: sourceIndexpath.section, targetIndex: cellindexPath.section)
            
            // 2.3 记录最新indexPath值
            sourceIndexpath = cellindexPath
        }
        // 3.根据idexPath设置pageControl
        pageControl.currentPage = cellindexPath.item / (layout.cols * layout.rows)
    }
}

//MARK: -titleViewDeleagate 
extension HJPageCollectionView : HJTitleViewDelegate {
    
    func titleView(_ titleView: HJTitleView, selectedIndex index: Int) {
        
        let sectionindex = IndexPath(item: 0, section: index)
        collectionView.scrollToItem(at: sectionindex, at: .left, animated: false)
        collectionView.contentOffset.x -= layout.sectionInset.left
        
        scrollViewEndscroll()
    }

}

Demo下载

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 200,527评论 5 470
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 84,314评论 2 377
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 147,535评论 0 332
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,006评论 1 272
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 62,961评论 5 360
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,220评论 1 277
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,664评论 3 392
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,351评论 0 254
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,481评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,397评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,443评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,123评论 3 315
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,713评论 3 303
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,801评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,010评论 1 255
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,494评论 2 346
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,075评论 2 341

推荐阅读更多精彩内容