【问题标题】:UIButton interaction is not smooth when used in UICollectionViewCell在 UICollectionViewCell 中使用时 UIButton 交互不流畅
【发布时间】:2019-06-11 11:09:21
【问题描述】:

我有一个 UICollectionViewCell,我在其中添加了 UIButton。通常会调用按钮操作,但有时不会。当我在视图控制器中添加相同的按钮时,交互非常流畅。即使是轻轻的敲击也会触发动作。 下面是按钮的代码:

 func makeTapButton(for superView: UIView) -> UIButton {
      let offSetValue = 15
      let button = UIButton()
      button.backgroundColor = UIColor.yellow
      superView.addSubview(button)
      button.snp.makeConstraints { (make) in
         make.leading.equalToSuperview().offset(-offSetValue)
         make.trailing.equalToSuperview().offset(offSetValue)
         make.top.equalToSuperview().offset(-offSetValue)
         make.bottom.equalToSuperview().offset(offSetValue)
     }
     return button
 }

   func setupCustomView() {
     self.addSubview(containerStackView)
     containerStackView.snp.makeConstraints { (make) -> Void in
        make.top.equalTo(self)
        make.leading.equalTo(self)
        make.trailing.equalTo(self)
        make.bottom.equalTo(self)
    }

    containerStackView.addArrangedSubview(commentStack)
    containerStackView.addArrangedSubview(retweetStack)
    containerStackView.addArrangedSubview(likeStack)
    commentStack.addArrangedSubview(commentImageView)
    commentStack.addArrangedSubview(commentsCountLabel)
    retweetStack.addArrangedSubview(retweetImageView)
    retweetStack.addArrangedSubview(retweetCountLabel)
    likeStack.addArrangedSubview(likeImageView)
    likeStack.addArrangedSubview(likesCountLabel)

    likeButton = makeTapButton(for: likeStack)
    commentButton = makeTapButton(for: commentStack)
    retweetButton = makeTapButton(for: retweetStack)
}

【问题讨论】:

    标签: ios swift uibutton


    【解决方案1】:

    使用放置在collectionview中的UIbutton时尝试下面提到的代码

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    
            let cell:UICollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath as IndexPath) as! UICollectionViewCell
            cell.btnName.addTarget(self, action: #selector(btnSelClk), for: .touchUpInside)
            cell.binSel.tag = collectionView.tag
            cell.binSel.accessibilityValue = String(indexPath.row)
    
            return cell
        }
    
    @objc func btnSelClk(sender:UIButton) {
            selectAry[sender.tag] = sender.accessibilityValue!
             // your button action
    }
    

    【讨论】:

      【解决方案2】:

      在 UICollectionViewCell 类中定义你的按钮,在 UIViewController 类中定义你的函数,因为它们被重用,所以延迟更少;

      import UIKit
      
      class YourCell: UITableViewCell {
      
      @IBOutlet weak var yourBtn: UIButton!
      
      var yourButtonAction: (() -> ())?
      
      
       @IBAction func buttonPressed(_ sender: UISlider) {
      
              yourButtonAction()
          }
      
      }
      

      然后在您调用单元格的 ViewController 中;

      func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
      
       let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "YourCell", for: indexPath) as! YourCell
      
      cell.yourBtn = {[unowned self] in
            // call your functions here, I hope this will be less laggy 
           print("button pressed")
        }
      
      
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-11-18
        • 2017-11-25
        • 1970-01-01
        • 2017-08-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多