gpt4 book ai didi

ios - 创建一个简单的标签,在快速点击时切换文本

转载 作者:可可西里 更新时间:2023-11-01 00:39:17 25 4
gpt4 key购买 nike

swift 4.0 iOS 11.x就在您认为自己掌握了窍门时,您意识到自己错过了一些关键的东西。想要创建一个标签,当您点击它时它会自行改变。创建了这个标签的子类。

import UIKit

class TapText: UILabel {

private var changableValues: String = "NESW"
private var currentPosition:Int = 0

required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)!
print("required ")
let tap = UITapGestureRecognizer(target: self, action: Selector(("tapFunction:")))
self.addGestureRecognizer(tap)
}

required override init(frame: CGRect) {
super.init(frame: frame)
print("required override")
let tap = UITapGestureRecognizer(target: self, action: Selector(("tapFunction:")))
self.addGestureRecognizer(tap)
}

func tapFunction(sender:UITapGestureRecognizer) {
print("tapped")
self.text = String(Array(changableValues)[currentPosition])
if currentPosition < changableValues.count {
currentPosition += 1
} else {
currentPosition = 0
}
}

}

我认为这会奏效。正如 Nilish 刚刚指出的那样,我忘记添加 userInteractive,但当我添加时我遇到了崩溃。

2018-03-12 11:14:40.283502+0100 Blah[952:382749] -[Blah.TapText tapFunction:]: unrecognized selector sent to instance 0x111a57700 2018-03-12 11:14:40.285213+0100 QRCodeReader[952:382749] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Blah.TapText tapFunction:]: unrecognized selector sent to instance 0x111a57700' * First throw call stack: (0x184633164 0x18387c528 0x184640628 0x18dfae188 0x184638b10 0x18451dccc 0x18e28aca4 0x18e28f298 0x18dd67a14 0x18dc1eb50 0x18e278b08 0x18e278678 0x18e2777d4 0x18dc1ce5c 0x18dbede7c 0x18e54330c 0x18e545898 0x18e53e7b0 0x1845db77c 0x1845db6fc 0x1845daf84 0x1845d8b5c 0x1844f8c58 0x1863a4f84 0x18dc515c4 0x100887c3c 0x18401856c) libc++abi.dylib: terminating with uncaught exception of type NSException

--- 添加了 _ bar 和 @objc 指令,现在可以工作了……看起来像这样……

required override init(frame: CGRect) {
super.init(frame: frame)
print("fcuk12032018 required override")
let tap = UITapGestureRecognizer(target: self, action: #selector(tapFunction))
self.addGestureRecognizer(tap)
}

@objc func tapFunction(_ sender:UITapGestureRecognizer) {
print("fcuk12032018 tapped")
self.text = String(Array(changableValues)[currentPosition])
if currentPosition < changableValues.count - 1 {
currentPosition += 1
} else {
currentPosition = 0
}
}

谢谢尼蒂什!

最佳答案

在初始化程序中将 userInteraction 设置为 true。然后 UILabel 将响应手势。
关于崩溃问题:尝试将函数设置为 func tapFunction(_sender: UITapGestureRecognizer)。并使用 #selector 设置选择器。

关于ios - 创建一个简单的标签,在快速点击时切换文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49232612/

25 4 0