gpt4 book ai didi

ios - 如何在 Swift 4 中多次点击 UIButton 以将不同的字符串打印回 outputLabel?目标是创建 T9 文本

转载 作者:行者123 更新时间:2023-11-30 11:45:44 24 4
gpt4 key购买 nike

我在尝试让 UI 按钮进行多次点击时遇到问题,我知道这并不理想,但如果有人可以告诉我我做错了什么或为我指出正确的方向,那就太好了。下面是我当前的代码,我正在尝试创建一个名为“twoPressed”的函数,该函数根据点击的次数进行响应。

var resultOfLabel = ""

@IBOutlet weak var outputLabel: UILabel!

override func viewDidLoad() {
super.viewDidLoad()

let tapGesture1 = UITapGestureRecognizer(target: self, action: #selector(singleTap(gesture:)))
UIButton.addGestureRecognizer(tapGesture1)

let tapGesture2 = UITapGestureRecognizer(target: self, action: #selector(doubleTap(gesture:)))
tapGesture2.numberOfTapsRequired = 2
UIButton.addGestureRecognizer(tapGesture2)

let tapGesture3 = UITapGestureRecognizer(target: self, action: #selector(tripleTap(gesture:)))
tapGesture3.numberOfTapsRequired = 3
UIButton.addGestureRecognizer(tapGesture3)

let tapGesture4 = UITapGestureRecognizer(target: self, action: #selector(quadrupleTap(gesture:)))
tapGesture4.numberOfTapsRequired = 4
UIButton.addGestureRecognizer(tapGesture4)

// Do any additional setup after loading the view.
}

@IBAction func twoPressed(sender: UIButton) {

@objc func singleTap() {
print(“2”)
}

@objc func doubleTap() {
print(“a”)
}

@objc func tripleTap() {
print(“b”)
}

@objc func quadrupleTap() {
print(“c”)
}

resultOfLabel += "\(sender)"

outputLabel.text = resultOfLabel

}

最佳答案

除了从IBAction方法中获取这4个函数之外没有问题

let tapGesture1 = UITapGestureRecognizer(target: self, action: #selector(self.singleTap(_:)))
UIButton.addGestureRecognizer(tapGesture1)

//

@objc func singleTap(_ sender: UITapGestureRecognizer? = nil) {
print(“2”)
self.twoPressed(sender.View as! UIButton)
}

@objc func doubleTap() {
print(“a”)
}

@objc func tripleTap() {
print(“b”)
}

@objc func quadrupleTap() {
print(“c”)
}


@IBAction func twoPressed(sender: UIButton) {

resultOfLabel += "\(sender)"
outputLabel.text = resultOfLabel

}

关于ios - 如何在 Swift 4 中多次点击 UIButton 以将不同的字符串打印回 outputLabel?目标是创建 T9 文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48845908/

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