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

添加删除按钮

  • 每个 Cell 还缺少 删除按钮, 在 HMEmoticonPageCell 里面懒加载 删除按钮

    /// 删除按钮
    private lazy var deleteButton: UIButton = {
      let button = UIButton()
    
      button.setImage(UIImage(named: "compose_emotion_delete"), for: UIControlState.normal)
      button.setImage(UIImage(named: "compose_emotion_delete_highlighted"), for: UIControlState.highlighted)
    
      return button
    }()
    
  • 删除按钮 添加到 cell 里.

    /// 准备UI
    private func setupUI() {
      backgroundColor = UIColor(white: 237 / 255.0, alpha: 1)
    
      addEmoticonButtons()
      contentView.addSubview(deleteButton)
      contentView.addSubview(messageLabel)
    }
    
  • layoutEmoticonButtons 方法中 设置删除按钮的位置

    /// 重新布局按钮位置
    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)
    }
    
  • 运行,效果如下

results matching ""

    No results matching ""