UILabel手势回应

时间:2015-12-10 07:41:48

标签: ios swift

@IBOutlet weak var imageTitle: UILabel!
@IBOutlet weak var profileImage: UIImageView!

    func titleStyle(){
    imageTitle.layer.borderWidth = 1.0
            imageTitle.layer.borderColor = UIColor.grayColor().CGColor
            imageTitle.layer.cornerRadius = 10.0

            // Apply Gesture to label
            imageTitle.userInteractionEnabled = true
            imageTitle.tag = 10
            imageTitle.addGestureRecognizer(UIGestureRecognizer(target: self, action: "tappedLabel"))
        }

点击标签时,这些代码似乎不起作用。

func tappedLabel(gesture: UIGestureRecognizer){
        print("Hi")
    }

2 个答案:

答案 0 :(得分:4)

我认为你错过了":"同时添加动作。

imageTitle.addGestureRecognizer(UIGestureRecognizer(target: self, action: "tappedLabel:"))

答案 1 :(得分:0)

我认为您的问题可能在于使用UIGestureRecognizer而不是UITapGestureRecogniser

尝试:

imageTitle.addGestureRecognizer(UITapGestureRecognizer(target: self, action: "tappedLabel:"))

我不能100%确定为什么使用UIGestureRecognizer无法正常工作,但我自己也遇到了同样的问题,这对我有用。