底部toolbar中,為button自定義type類(lèi)型(直接使用tag值也是可以的),在button的點(diǎn)擊事件中,執(zhí)行閉包,讓外界監(jiān)聽(tīng)button的點(diǎn)擊事件
// 定義一個(gè)常量(添加子控件時(shí),0的tag值默認(rèn)被占用,所以創(chuàng)建時(shí)最好添加一個(gè)系數(shù))
// 內(nèi)部創(chuàng)建button的時(shí)候,讓其加上一個(gè)中間值,調(diào)用閉包時(shí),再減去個(gè)中間值,這樣傳出去的Tag值和CollectionView的索引可以對(duì)應(yīng),外界不需要在進(jìn)行設(shè)置
let tmpvalue: Int = 1000
// 表情鍵盤(pán)類(lèi)型
enum JSEmoticonToolBarType: Int {
case Default = 0 //默認(rèn)
case Emoji = 1 //Emoji
case Langxiaohua = 2 //浪小花
}
// MARK: - Button點(diǎn)擊事件
@objc private func buttonClick (button: UIButton) -> Void {
// button的選中狀態(tài)
if button == currentButton {
return
}
button.selected = true
currentButton?.selected = false
currentButton = button
buttonClickClosure?(type: JSEmoticonToolBarType(rawValue: button.tag)!)
}
監(jiān)聽(tīng)按鈕點(diǎn)擊事件的閉包實(shí)現(xiàn)中,通過(guò)參數(shù)傳入button的Tag值,轉(zhuǎn)化為CollectionView Cell對(duì)應(yīng)的索引,跳轉(zhuǎn)到指定Cell,完成點(diǎn)擊按鈕調(diào)到指定表情分組
// 接收toolbar中的哪個(gè)按鈕點(diǎn)擊
toolbar.buttonClickClosure = {[weak self] (type: JSEmoticonToolBarType) -> () in
// 根據(jù)button的type轉(zhuǎn)換為CollectionView的索引,跳轉(zhuǎn)到對(duì)應(yīng)分區(qū)的表情首頁(yè)
let indexPath = NSIndexPath(forItem: 0, inSection: type.rawValue)
self?.pageView.scrollToItemAtIndexPath(indexPath, atScrollPosition: UICollectionViewScrollPosition.Left, animated: false)
}
滾動(dòng)CollectionView改變toolbar按鈕選中
- 計(jì)算方式 (只試用于cell和item一樣大,即ItemSize均為0)
- 當(dāng)前的collectionView界面上展示給用戶最多兩個(gè)cell
- 相對(duì)于 collectionView 左側(cè)起點(diǎn)為O點(diǎn) 屏幕中心點(diǎn)x = 屏幕的寬度/ 2 + contentOffset.x
- 由于cell貼在pageView 每個(gè)cell都有自己的frame
- 如果判斷出 哪個(gè)cell的frame 包含屏幕的中心點(diǎn) 就代表它在屏幕中顯示區(qū)域占比大
- 通過(guò)cell 和 collectionView 得到indexPath
- indexPath 得到 section
- section 0 1 2 viewWithTag 通過(guò)該方法 得到button 把button狀態(tài)發(fā)生改變
代碼:
extension JSEmoticonKeyBoardView: UICollectionViewDelegate {
func scrollViewDidScroll(scrollView: UIScrollView) {
// 屏幕中心點(diǎn)距離CollectionView左邊的距離
let centerX = SCREENW / 2 + scrollView.contentOffset.x
// 獲取當(dāng)前屏幕顯示的Cell
let cellArr = pageView.visibleCells().sort { (cell1, cell2) -> Bool in
return cell1.frame.origin.x < cell2.frame.origin.x
}
// 當(dāng)屏幕上顯示2個(gè)cell的時(shí)候
if cellArr.count == 2 {
// 取出Cell
let firstCell = cellArr.first!
let lastCell = cellArr.last!
// 獲取占屏幕比例大的Cell
var indexPath: NSIndexPath
// 如果中心點(diǎn)在第一個(gè)cell中,代表第一個(gè)Cell占屏幕比例多
if firstCell.frame.contains(CGPointMake(centerX, 0)) {
indexPath = pageView.indexPathForCell(firstCell)!
} else {
indexPath = pageView.indexPathForCell(lastCell)!
}
// 滾動(dòng)CollectionView時(shí),改變toolbar下面按鈕的選中狀態(tài)
toolbar.switchSelectedButton(indexPath.section + tmpvalue)
}
}
}
CollectionView 數(shù)據(jù)源方法中
// 數(shù)據(jù)源方法
extension JSEmojiconPageView: UICollectionViewDataSource {
func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
return JSEmoticonTool.sharedTool.allEmoticons.count
}
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return JSEmoticonTool.sharedTool.allEmoticons[section].count
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier(collectionViewCellId, forIndexPath: indexPath) as! JSEmojiconPageViewCell
cell.indexpath = indexPath
cell.emoticons = JSEmoticonTool.sharedTool.allEmoticons[indexPath.section][indexPath.item]
return cell
}
}
效果圖.png