专业java、php、iOS、C++、网页设计、平面设计、网络营销、游戏开发、前端与移动开发培训机构

最近表情显示提示Label

  • HMEmoticonPageCell 懒加载 最近使用表情的Label

    /// 最近使用表情的 Label
    private lazy var recentInfoLabel: UILabel = {
      let label = UILabel()
    
      label.textColor = UIColor.gray
      label.font = UIFont.systemFont(ofSize: 12)
      label.text = "最近使用的表情"
      label.sizeToFit()
    
      return label
    }()
    
  • HMEmoticonPageCellsetupUI 方法中添加 最近使用表情的 label recentInfoLabel
    /// 准备UI
    private func setupUI() {
      backgroundColor = UIColor(white: 237 / 255.0, alpha: 1)
      addEmoticonButtons()
      contentView.addSubview(deleteButton)
      contentView.addSubview(messageLabel)
      contentView.addSubview(recentInfoLabel)
    }
    
  • HMEmoticonPageCelllayoutEmotioncButtons 方法中 设置 recentInfoLabelcenter

    /// 重新布局按钮位置
    private func layoutEmoticonButtons() {
      let margin: CGFloat = 5
      let bottomMargin: CGFloat = 25
    
      // 计算表情按钮的宽度与高度
      let itemW = (frame.width - 2 * margin) / CGFloat(HMEmoticonColumnOfPage)
      let itemH = (frame.height - bottomMargin) / CGFloat(HMEmoticonRowOfPage)
    
      for i in 0..<emoticonButtons.count {
          let button = emoticonButtons[i]
    
          // 计算当前按钮是处于哪一行和哪一列
          let column = i % HMEmoticonColumnOfPage
          let row = i / HMEmoticonColumnOfPage
    
          // 计算x和y值
          let x = itemW * CGFloat(column) + margin
          let y = itemH * CGFloat(row)
          button.frame = CGRect(x: x, y: y, width: itemW, height: itemH)
      }
    
      // 设置删除按钮的位置
      let deleteX = (CGFloat(HMEmoticonColumnOfPage) - 1) * itemW + margin
      let deleteY = (CGFloat(HMEmoticonRowOfPage) - 1) * itemH
      deleteButton.frame = CGRect(x: deleteX, y: deleteY, width: itemW, height: itemH)
    
      /// 最近使用表情的 label
      recentInfoLabel.center = CGPoint(x: frame.width * 0.5, y: frame.height - recentInfoLabel.frame.height)
    }
    
  • 运行,每个cell里面都有 最近使用表情的 label, 只有在 最近 这个分组里面才需要,在 CZEmoticonPageCellindexPath 属性判断 不是第0组就隐藏最近使用label

    var indexPath: NSIndexPath? {
      didSet {
          messageLabel.text = "当前第\(indexPath!.section)组, 第\(indexPath!.item)页"
    
          // 不是第0组就隐藏最近使用label
          recentInfoLabel.hidden = indexPath?.section != 0
      }
    }
    
  • 运行,从其他组在切换到最近这组,发现 最近使用labelpageControl 都存在.在设置 pageControl 的时候来确定隐藏和显示 pageControl

    /**
    设置pageControl
    parameter indexPath: 当前页的indexPath
    */
    private func setPageControl(indexPath: NSIndexPath) {
      // 设置pageControl的总页数 = 当前表情包的页数
      pageControl.numberOfPages = CZEmoticonManager.sharedInstance.packages[indexPath.section].pageEmoticons.count
    
      // 当前页 = 当section的item
      pageControl.currentPage = indexPath.item
    
      // 第0组就隐藏pageControl
      pageControl.hidden = indexPath.section == 0
    }
    

results matching ""

    No results matching ""