简体   繁体   English

同一UITapGestureRecogniser的多个UILabel点击不起作用

[英]multiple UILabels tap for same UITapGestureRecogniser Not working

I am trying to add same UITapGestureRecognizer to multiple views as 我正在尝试将相同的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 This link shows me how to add same gesture recognizer to multiple views,but it doesnot works..Can't i add same gesture recognizer to multiple views? 识别UITapGestureRecogniser的多个UILabels点此链接向我展示了如何向多个视图添加相同的手势识别器,但是它不起作用。我不能向多个视图添加相同的手势识别器吗?

I think you can not do it with one Gesture but you can add different gesture for all and access same method for all. 我认为您无法用一种手势来做到这一点,但是您可以为所有人添加不同的手势并为所有人使用相同的方法。 consider below code: 考虑下面的代码:

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")
        }
    }
}

For more Information you can refer this Apple Documentation . 有关更多信息,请参阅此Apple文档

Hope It will help you. 希望对您有帮助。

For using UITapGestureRecognizer on Uilabel ... you must Set label.userInteractionEnabled = true . 要在Uilabel ...上使用UITapGestureRecognizer ...,必须设置label.userInteractionEnabled = true just add this line to viewWillAppear and it will Work just Fine. 只需将此行添加到viewWillAppear,它将正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM