【问题标题】:Swift - handle UICollectionViewCell TapSwift - 处理 UICollectionViewCell Tap
【发布时间】:2019-12-03 14:46:16
【问题描述】:

点击我的UICollectionViewCell 时,我的行为很奇怪。

这是我的cell-设置:

    class ContentCell: UICollectionViewCell {

    let buttonView: UIButton = {
        let v = UIButton()
        v.translatesAutoresizingMaskIntoConstraints = false
        v.layer.shadowOpacity = 1
        v.layer.shadowOffset = CGSize(width: 1.5, height: 1.5)
        v.layer.shadowRadius = 3
        v.layer.shadowColor = UIColor.darkGray.cgColor
        return v
    }()

    let theLabel: UILabel = {
        let v = UILabel()
        v.translatesAutoresizingMaskIntoConstraints = false
        v.textAlignment = .center
        return v
    }()

    let testLabel: UILabel = {
        let v = UILabel()
        v.translatesAutoresizingMaskIntoConstraints = false
        v.text = "Test Label"
        v.font = UIFont(name: "AvenirNext-DemiBold", size: 18)
        v.textColor = .darkGray
        v.textAlignment = .center
        return v
    }()

    override init(frame: CGRect) {
        super.init(frame: frame)
        commonInit()
    }

    required init?(coder: NSCoder) {
        super.init(coder: coder)
        commonInit()
    }


    func commonInit() -> Void {
//        contentView.addSubview(theLabel)
        contentView.layer.cornerRadius = 3.0;
        contentView.addSubview(testLabel)
        contentView.addSubview(buttonView)
        // constrain label to all 4 sides
        NSLayoutConstraint.activate([

            buttonView.topAnchor.constraint(equalTo: contentView.topAnchor),
            buttonView.leadingAnchor.constraint(equalTo: contentView.leadingAnchor),
            buttonView.trailingAnchor.constraint(equalTo: contentView.trailingAnchor),
            buttonView.heightAnchor.constraint(equalToConstant:150),

            testLabel.topAnchor.constraint(equalTo: buttonView.bottomAnchor,constant: 1),
            testLabel.bottomAnchor.constraint(equalTo: contentView.bottomAnchor),
            testLabel.leadingAnchor.constraint(equalTo: contentView.leadingAnchor),
            testLabel.trailingAnchor.constraint(equalTo: contentView.trailingAnchor),
        ])
        buttonView.addTarget(self, action: #selector(listTapped(_:)), for: .touchUpInside)
    }

    @objc func listTapped(_ sender: Any){
        print("listTapped")
    }
}

奇怪的是,如果我短时间点击我的手机,@objc func listTapped 就会被调用。但是,如果单击它以获得更长的计时器,则会发生“单击”动画,但我的 func listTapped 不会被调用。所以看来我有两个不同的“点击”处理程序??

我对此很陌生,所以如果有人能向我解释导致该问题的原因,我将不胜感激。

【问题讨论】:

  • 这是轻按而不是长按
  • 这是什么意思?如果我长按它,我会得到“突出显示”-动画
  • 你能试试我的回答@Chris 并告诉我它是否有效
  • @Was'SiimBenHssen 不,不工作。我仍然可以“点击”它或“按下”它

标签: ios swift uicollectionview uicollectionviewcell


【解决方案1】:

尝试删除此代码并使用点击手势更改点击操作

// remove this code

buttonView.addTarget(self, action: #selector(listTapped(_:)), for: .touchUpInside)

// add tap gesture in the top of your commitinit function

let tap = UITapGestureRecognizer(target: self, action: #selector(listTapped(_:)))
    tap.delegate = self
    buttonView.addGestureRecognizer(tap)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-04-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多