UIView可以为自己分配UITapGestureRecognizer吗?

时间:2018-05-14 21:28:20

标签: ios swift

我试图创建一个识别轻击手势的UIView,但是UIView没有正确注册点击。这是UIView子类本身的代码:

import UIKit

class ActionCell: SignalTableCell, UIGestureRecognizerDelegate {

    var icon: UIImageView!
    var actionType: UILabel!
    var actionTitle: UILabel!

    var a:Action?
    var tap:UITapGestureRecognizer?

    required init(frame: CGRect) {
        //
        super.init(frame:frame)

        tap = UITapGestureRecognizer(target: self, action: #selector(self.touchTapped(_:)))
        tap?.delegate = self
        addGestureRecognizer(tap!)


    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)

        tap = UITapGestureRecognizer(target: self, action: #selector(self.touchTapped(_:)))
        tap?.delegate = self
        addGestureRecognizer(tap!)


    }

    @objc func touchTapped(_ sender: UITapGestureRecognizer) {
        print("OK")
    }

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code

        self.isUserInteractionEnabled = true


    }

    override func layoutSubviews() {


        if(icon == nil) {
            let rect = CGRect(origin: CGPoint(x: 10,y :20), size: CGSize(width: 64, height: 64))
            icon = UIImageView(frame: rect)
            addSubview(icon)
        }
        icon.image = UIImage(named:(a?.icon)!)
        if(actionType == nil) {
            let rect = CGRect(origin: CGPoint(x: 100,y :20), size: CGSize(width: 200, height: 16))
            actionType = UILabel(frame: rect)
            addSubview(actionType)
        }
        actionType.text = a.type
        if(actionTitle == nil) {
            let rect = CGRect(origin: CGPoint(x: 100,y :80), size: CGSize(width: 200, height: 16))
            actionTitle = UILabel(frame: rect)
            addSubview(actionTitle)
        }
        actionTitle.text = a?.title
    }

    func configure( a:Action ) {
        self.a = a
    }

    override func setData( type:SignalData ) {

        a = (type as! Action)
    }
}

我只是想做到这一点,以便这个UIView可以知道它何时被点击。如果不添加单独的UIViewController,这可能吗?这看起来似乎应该相当简单,但它看起来并不令人困惑。

我已经完成了代码并调用了init方法并添加了手势识别器,但它没有触发。

3 个答案:

答案 0 :(得分:0)

如果是表格视图单元格,我建议不要使用点击手势,因为它可能会干扰didSelectRowAtIndexPath:和其他委托方法。但是,如果您仍想保持点按手势,请尝试在tap?.cancelsTouchesInView = false之前添加addGestureRecognizer(tap!),看看是否有效。

如果您只是想知道它的点击时间,您还可以覆盖以下UIResponder方法:

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    super.touchesBegan(touches, with: event)

    // do your stuff
}

答案 1 :(得分:0)

我认为,覆盖 touchesBegan 方法更容易。像这样:

TX0x20x150xe60x860xce

答案 2 :(得分:0)

最有可能点击了actionTypeActionTitleicon,并且点击不会失败,因为标签和图片默认禁用用户互动。为每个作为主视图子视图的字段设置isUserInteractionEnabled = true

覆盖func layoutSubviews(){

    if(icon == nil) {
        let rect = CGRect(origin: CGPoint(x: 10,y :20), size: CGSize(width: 64, height: 64))
        icon = UIImageView(frame: rect)
        icon.isUserInteractionEnabled = true
        addSubview(icon)
    }
    icon.image = UIImage(named:(a?.icon)!)
    if(actionType == nil) {
        let rect = CGRect(origin: CGPoint(x: 100,y :20), size: CGSize(width: 200, height: 16))
        actionType = UILabel(frame: rect)
        actionType.isUserInteractionEnabled = true
        addSubview(actionType)
    }
    actionType.text = a.type
    if(actionTitle == nil) {
        let rect = CGRect(origin: CGPoint(x: 100,y :80), size: CGSize(width: 200, height: 16))
        actionTitle = UILabel(frame: rect)
        actionTitle.isUserInteractionEnabled = true
        addSubview(actionTitle)
    }
    actionTitle.text = a?.title
}