在 UICollectionViewCell 中使用时 UIButton 交互不流畅
Posted
技术标签:
【中文标题】在 UICollectionViewCell 中使用时 UIButton 交互不流畅【英文标题】:UIButton interaction is not smooth when used in UICollectionViewCell 【发布时间】: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)
【问题讨论】:
【参考方案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")
【讨论】:
以上是关于在 UICollectionViewCell 中使用时 UIButton 交互不流畅的主要内容,如果未能解决你的问题,请参考以下文章
在 UICollectionViewCell 中更新 UITableView
如何在 UICollectionViewCell 中添加 UICollectionView?
如何在 UICollectionViewCell 上设置 UILabel?