顶部标题左右滑动切换控制器是一种非常用的左右滚动切换效果,几乎每一个APP都有用到,在这里介绍一下我自己封装的一个Swift版本的简单框架;代码中注释相对详细,故文中没有做过多的解释;废话不多少,直接上效果图:
一.框架介绍
1 2 3 4 5 6 7
| TJTitleStyle.swift
TJTitleView.swift
TJContentView.swift
TJPageView.swift
|
TJTitleStyle.swift
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
|
var isScrollEnable : Bool = false
var normalColor : UIColor = UIColor(r: 0, g: 0, b: 0)
var selectedColor : UIColor = UIColor(r: 255, g: 127, b: 0)
var font : UIFont = UIFont.systemFont(ofSize: 14.0)
var titleMargin : CGFloat = 20
var titleHeight : CGFloat = 44
var isShowBottomLine : Bool = false
var bottomLineColor : UIColor = UIColor.orange
var bottomLineH : CGFloat = 2
var isNeedScale : Bool = false var scaleRange : CGFloat = 1.2
var isShowCover : Bool = false
var coverBgColor : UIColor = UIColor.lightGray
var coverMargin : CGFloat = 5
var coverH : CGFloat = 25
var coverRadius : CGFloat = 12
|
TJPageCollectionLayout.swift自定义布局
代码如下,注释比较详尽,不做赘述
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
| override func prepare() { super.prepare()
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)
let sectionCount = collectionView!.numberOfSections
var prePageCount : Int = 0 for i in 0..<sectionCount { let itemCount = collectionView!.numberOfItems(inSection: i) for j in 0..<itemCount { let indexpath = IndexPath(item: j, section: i) let attr = UICollectionViewLayoutAttributes(forCellWith: indexpath) 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) attr.frame = CGRect(x: itemX, y: itemY, width: itemW, height: itemH)
cellAttrs.append(attr) } prePageCount += (itemCount - 1) / (cols * rows) + 1 } maxWidth = CGFloat(prePageCount) * collectionView!.bounds.width }
|
TJPageCollectionView.swift
创建collectionView显示布局内容,设置代理方法和对外暴露的方法
代理方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| protocol TJPageCollectionViewDateSource : class { func numberOfSections(in pageCollectionView : TJPageCollectionView) -> Int
func pageCollectionView(_ collectionView : TJPageCollectionView, numberOfItemsInSection section : Int) -> Int
func pageCollectionView(_ pageCollectionView : TJPageCollectionView, _ collectionView : UICollectionView, cellForItemAt indexPath : IndexPath) -> UICollectionViewCell }
protocol TJPageCollectionViewDelegate : class { func pageCollectionView(_ pageCollectionView : TJPageCollectionView, didSelectorItemAt indexPath : IndexPath) }
|
二.首页左右滑动调用方法
1.设置显示样式
1 2 3 4 5 6 7 8
| let style = TJTitleStyle()
style.isScrollEnable = true
style.isShowBottomLine = true
style.isShowCover = true
|
2.初始化方法
1 2 3 4 5 6 7 8 9 10 11 12 13
|
let pageView = TJPageView(frame: frame, titles: titles, style: style, childVcs: childVcs, parentVc: self)
view.addSubview(pageView)
|
三.底部类似表情键盘布局调用方法
1.初始化
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| let style = TJTitleStyle() style.isShowBottomLine = true
let layout = TJPageCollectionLayout() layout.cols = 7 layout.rows = 3 layout.minimumLineSpacing = 0 layout.minimumInteritemSpacing = 0 layout.sectionInset = UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10)
let pageCollection = TJPageCollectionView(frame: CGRect(x: 0, y: UIScreen.main.bounds.height - 250, width: UIScreen.main.bounds.width, height: 250), style: style, titles: ["普通", "粉丝"], isTitleInTop: false, layout: layout) pageCollection.delegate = self pageCollection.dataSource = self
pageCollection.register(nib: UINib(nibName: "EmoticonViewCell", bundle: nil), identifier: kEmoticonCellID)
view.addSubview(pageCollection)
|
2.遵循协议
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| extension NextViewController : TJPageCollectionViewDateSource{ func numberOfSections(in pageCollectionView: TJPageCollectionView) -> Int { return 5 } func pageCollectionView(_ collectionView: TJPageCollectionView, numberOfItemsInSection section: Int) -> Int { return 40 } func pageCollectionView(_ pageCollectionView: TJPageCollectionView, _ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { let cell = collectionView.dequeueReusableCell(withReuseIdentifier: kEmoticonCellID, for: indexPath) as! EmoticonViewCell
return cell } }
extension NextViewController : TJPageCollectionViewDelegate{ func pageCollectionView(_ pageCollectionView: TJPageCollectionView, didSelectorItemAt indexPath: IndexPath) { print(indexPath)
} }
|