多个 UILabel 轻按相同的 UITapGestureRecogniser 不工作

Posted

技术标签:

【中文标题】多个 UILabel 轻按相同的 UITapGestureRecogniser 不工作【英文标题】:multiple UILabels tap for same UITapGestureRecogniser Not working 【发布时间】:2015-07-03 06:40:12 【问题描述】:

我正在尝试将相同的 UITapGestureRecognizer 添加到多个视图中

  var tapGesture = UITapGestureRecognizer(target: self, action: "selectOptionsForRating:")
        tapGesture.numberOfTapsRequired = 1

        serviceLbl.addGestureRecognizer(tapGesture)
        pickUpTimeLbl.addGestureRecognizer(tapGesture)
        drivingLbl.addGestureRecognizer(tapGesture)
        communicateLbl.addGestureRecognizer(tapGesture)
        bikeConditionLbl.addGestureRecognizer(tapGesture)
        dropOffLbl.addGestureRecognizer(tapGesture)

        serviceLbl.tag = Service
        pickUpTimeLbl.tag = PickUpTime
        drivingLbl.tag = Driving
        communicateLbl.tag = Communicate
        bikeConditionLbl.tag = BikeCondition
        dropOffLbl.tag = DropOff


    

    func selectOptionsForRating(tapsender:UITapGestureRecognizer)

        var sender = tapsender.view

        if sender!.tag == Service 
             println("tap gesture is working fine ")

        else if sender!.tag == PickUpTime 
            println("tap gesture is working fine ")

        else if sender!.tag == Driving 
            println("tap gesture is working fine ")

        else if sender!.tag == Communicate 
            println("tap gesture is working fine ")

        else if sender!.tag == BikeCondition 
            println("tap gesture is working fine ")

        else if sender!.tag == DropOff 
            println("tap gesture is working fine ")

        

Recognizing multiple UILabels tap for UITapGestureRecogniser此链接向我展示了如何将相同的手势识别器添加到多个视图,但它不起作用..我不能将相同的手势识别器添加到多个视图吗?

【问题讨论】:

【参考方案1】:

我认为你不能用一个手势来做到这一点,但你可以为所有人添加不同的手势并为所有人访问相同的方法。考虑下面的代码:

import UIKit

class ViewController: UIViewController 

    @IBOutlet weak var serviceLbl: UILabel!
    @IBOutlet weak var pickUpTimeLbl: UILabel!
    @IBOutlet weak var drivingLbl: UILabel!
    @IBOutlet weak var communicateLbl: UILabel!
    @IBOutlet weak var bikeConditionLbl: UILabel!

    @IBOutlet weak var dropOffLbl: UILabel!

    override func viewDidLoad() 
        super.viewDidLoad()

        var tapGestureForserviceLbl = UITapGestureRecognizer(target: self, action: "selectOptionsForRating:")
        var tapGestureForpickUpTimeLbl = UITapGestureRecognizer(target: self, action: "selectOptionsForRating:")
        var tapGestureFordrivingLbl = UITapGestureRecognizer(target: self, action: "selectOptionsForRating:")
        var tapGestureForcommunicateLbl = UITapGestureRecognizer(target: self, action: "selectOptionsForRating:")
        var tapGestureForbikeConditionLbl = UITapGestureRecognizer(target: self, action: "selectOptionsForRating:")
        var tapGestureFordropOffLbl = UITapGestureRecognizer(target: self, action: "selectOptionsForRating:")

        tapGestureForserviceLbl.numberOfTapsRequired = 1
        tapGestureForpickUpTimeLbl.numberOfTapsRequired = 1
        tapGestureFordrivingLbl.numberOfTapsRequired = 1
        tapGestureForcommunicateLbl.numberOfTapsRequired = 1
        tapGestureForbikeConditionLbl.numberOfTapsRequired = 1
        tapGestureFordropOffLbl.numberOfTapsRequired = 1

        serviceLbl.userInteractionEnabled = true
        pickUpTimeLbl.userInteractionEnabled = true
        drivingLbl.userInteractionEnabled = true
        communicateLbl.userInteractionEnabled = true
        bikeConditionLbl.userInteractionEnabled = true
        dropOffLbl.userInteractionEnabled = true

        serviceLbl.addGestureRecognizer(tapGestureForserviceLbl)
        pickUpTimeLbl.addGestureRecognizer(tapGestureForpickUpTimeLbl)
        drivingLbl.addGestureRecognizer(tapGestureFordrivingLbl)
        communicateLbl.addGestureRecognizer(tapGestureForcommunicateLbl)
        bikeConditionLbl.addGestureRecognizer(tapGestureForbikeConditionLbl)
        dropOffLbl.addGestureRecognizer(tapGestureFordropOffLbl)

        serviceLbl.tag = 1
        pickUpTimeLbl.tag = 2
        drivingLbl.tag = 3
        communicateLbl.tag = 4
        bikeConditionLbl.tag = 5
        dropOffLbl.tag = 6
    

    func selectOptionsForRating(tapsender:UITapGestureRecognizer)

        var sender = tapsender.view!.tag

        switch sender 
        case 1 :
            println("tap gesture is working fine 1")
        case 2 :
            println("tap gesture is working fine 2")
        case 3 :
            println("tap gesture is working fine 3")
        case 4 :
            println("tap gesture is working fine 4")
        case 5 :
            println("tap gesture is working fine 5")
        case 6 :
            println("tap gesture is working fine 6")
        default :
            println("tap gesture can not find view")
        
    

更多信息可以参考Apple Documentation。

希望对你有所帮助。

【讨论】:

是的..我知道我可以制作手势识别器的新实例..但为什么我不能只使用一个? 你的答案在这里:developer.apple.com/library/prerelease/ios/documentation/UIKit/… 感谢您的链接...我看起来一样...所以,是不是由于命中测试方法...所以添加到许多视图的 Tapgesture 识别器对找到响应者感到困惑? 【参考方案2】:

要在 Uilabel 上使用 UITapGestureRecognizer ...您必须设置 label.userInteractionEnabled = true 。只需将此行添加到 viewWillAppear 即可。

【讨论】:

实际上,如果只有一个标签,它就可以正常工作......当有多个视图时,我希望相同的手势识别器工作......在我的情况下标签 OP 的问题是关于多个 UILabel 不能在同一个 TapRecognizer 上工作。

以上是关于多个 UILabel 轻按相同的 UITapGestureRecogniser 不工作的主要内容,如果未能解决你的问题,请参考以下文章

Swift - 多个 UILabel 点击事件到相同的功能

多个 UILabel 实例

为 UITapGestureRecognizer 识别多个 UILabel 点击

Swift:在披露指标上重新激活轻按手势

轻按手势仅在轻按 3 次后响应

多行 UILabel 与固有内容大小的 UILabel 宽度相同