gpt4 book ai didi

ios - 多个 UILabels 点击相同的 UITapGestureRecogniser 不工作

转载 作者:可可西里 更新时间:2023-11-01 00:37:32 27 4
gpt4 key购买 nike

我正在尝试将相同的 UITapGestureRecognizer 添加到多个 View 中作为

  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此链接向我展示了如何将相同的手势识别器添加到多个 View ,但它不起作用。我不能将相同的手势识别器添加到多个 View 吗?

最佳答案

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

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 .

希望对你有帮助。

关于ios - 多个 UILabels 点击相同的 UITapGestureRecogniser 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31200193/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com