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

照片选择器监听cell点击事件

  • 点击cell加号按钮 需要弹出系统的相册.系统的相册需要控制器来完成.所有cell里面的事件需要控制器来处理. 使用代理
  • 定义 cell的点击事件协议

    // MARK: - cell的点击事件协议
    // 1.定义协议
    protocol HMPhotoSelectorCellDelegate: NSObjectProtocol {
      // 加号按钮点击
      func photoSelectorCellDidClickAddButton(cell: HMPhotoSelectorCell)
    
      // 删除按钮点击
      func photoSelectorCellDidClickDeleteButton(cell: HMPhotoSelectorCell)
    }
    
  • CZPhotoSelectorCell 添加 cellDelegate 属性
    /// 代理
    weak var delegate: HMPhotoSelectorCellDelegate?
    
  • 按钮添加点击事件

    /// 加号按钮
    private lazy var addButton: UIButton = {
      let button = UIButton()
    
      button.setImage(UIImage(named: "compose_pic_add"), for: UIControlState.normal)
      button.addTarget(self, action: #selector(addButtonDidClick), for: .touchUpInside)
    
      return button
    }()
    
    /// 删除按钮
    private lazy var deleteButton: UIButton = {
      let button = UIButton()
    
      button.setImage(UIImage(named: "compose_photo_close"), for: UIControlState.normal)
      button.addTarget(self, action: #selector(deleteButtonDidClick), for: .touchUpInside)
    
      return button
    }()
    
  • HMPhotoSelectorCell 添加对应的按钮点击响应事件
    // MARK: - 按钮点击事件
    func addButtonDidClick() -> Void {
      delegate?.photoSelectorCellDidClickAddButton(cell: self)
    }
    
    func deleteButtonDidClick() -> Void {
      delegate?.photoSelectorCellDidClickDeleteButton(cell: self)
    }
    
  • HMPhotoSelectorViewControllercellForItemAtIndexPath 设置cell的代理

    override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
      let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! HMPhotoSelectorCell
    
      cell.backgroundColor = UIColor.brown
      cell.delegate = self
    
      return cell
    }
    
  • 扩展 HMPhotoSelectorViewController 实现 HMPhotoSelectorCellDelegate 协议

    // MARK: - 扩展 HMPhotoSelectorViewController //
    extension HMPhotoSelectorViewController: HMPhotoSelectorCellDelegate {
      func photoSelectorCellDidClickDeleteButton(cell: HMPhotoSelectorCell) {
    
      }
    
      func photoSelectorCellDidClickAddButton(cell: HMPhotoSelectorCell) {
    
      }
    }
    

results matching ""

    No results matching ""