IMChatEmojiBarView.swift 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. //
  2. // IMChatEmojiBarView.swift
  3. // O2Platform
  4. //
  5. // Created by FancyLou on 2020/6/11.
  6. // Copyright © 2020 zoneland. All rights reserved.
  7. //
  8. import UIKit
  9. import CocoaLumberjack
  10. protocol IMChatEmojiBarClickDelegate {
  11. func clickEmoji(emoji: String)
  12. }
  13. class IMChatEmojiBarView: UIView {
  14. @IBOutlet weak var collectionView: UICollectionView!
  15. private let emojiList: [String] = {
  16. var list: [String] = []
  17. for i in 1...87 {
  18. if i < 10 {
  19. list.append("[0\(i)]")
  20. }else {
  21. list.append("[\(i)]")
  22. }
  23. }
  24. return list
  25. }()
  26. var delegate: IMChatEmojiBarClickDelegate? = nil
  27. override func awakeFromNib() {
  28. collectionView.register(UINib(nibName: "IMChatEmojiItemCell", bundle: nil), forCellWithReuseIdentifier: "IMChatEmojiItemCell")
  29. collectionView.delegate = self
  30. collectionView.dataSource = self
  31. DDLogDebug("list size \(emojiList.count)")
  32. }
  33. }
  34. extension IMChatEmojiBarView: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
  35. func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  36. return emojiList.count
  37. }
  38. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
  39. return CGSize(width:SCREEN_WIDTH / 10, height: 42)
  40. }
  41. func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  42. if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "IMChatEmojiItemCell", for: indexPath) as? IMChatEmojiItemCell {
  43. cell.setEmoji(emoji: self.emojiList[indexPath.row])
  44. return cell
  45. }
  46. return UICollectionViewCell()
  47. }
  48. func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
  49. if delegate != nil {
  50. delegate?.clickEmoji(emoji: self.emojiList[indexPath.row])
  51. }
  52. collectionView.deselectItem(at: indexPath, animated: false)
  53. }
  54. }