创建一个简单的标签,在swift

时间:2018-03-12 10:10:19

标签: ios swift uigesturerecognizer uitapgesturerecognizer

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:]:无法识别的选择器发送到实例0x111a57700   2018-03-12 11:14:40.285213 + 0100 QRCodeReader [952:382749] *   由于未捕获的异常而终止应用程序   'NSInvalidArgumentException',原因:' - [Blah.TapText tapFunction:]:   无法识别的选择器发送到实例0x111a57700'   * 第一次抛出调用堆栈:(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:   以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
    }
}

感谢Nitish!

2 个答案:

答案 0 :(得分:3)

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

答案 1 :(得分:1)

您的代码中似乎错过了 userInteractionEnabled 。 您可以在viewController中添加它,如下所示:

final class MyViewController: UIViewController {
   @IBOutlet private var myLabel: TapText!

    override func viewDidLoad() {
        super.viewDidLoad()

        myLabel.userInteractionEnabled = true
}

或直接在您的自定义类中:

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