Как настроить действие в метке? - PullRequest
0 голосов
/ 12 июня 2019

Вот мой код:

добавлен жест в viewDidLoad

let tap = UITapGestureRecognizer(target: self, action: #selector(tapLabel(tap:)))
            toastLabel.addGestureRecognizer(tap)
            toastLabel.isUserInteractionEnabled = true

func showLongToast( message: String) {        
    toastLabel = UILabel(frame: CGRect(x: controller.view.frame.origin.x + 20, y: controller.view.frame.size.height-200, width: controller.view.frame.size.width - 40, height: 125))
    toastLabel.numberOfLines = 0
    toastLabel.textAlignment = .center
    toastLabel.contentMode = .center
    toastLabel.backgroundColor = UIColor.white.withAlphaComponent(0.7)
    toastLabel.textColor = UIColor(red: 74/255, green: 74/255, blue: 74/255, alpha: 1)
    toastLabel.font = UIFont(name: "Montserrat-Medium", size: 15.0)
    let trimmedString = message.trimmingCharacters(in: .whitespacesAndNewlines)
    let string = NSMutableAttributedString(string: trimmedString)
    string.setColorForText("Enter Manually", with: #colorLiteral(red: 1, green: 0.4196078431, blue: 0.1812392979, alpha: 1))
    toastLabel.attributedText = string
    toastLabel.layer.cornerRadius = 25
    toastLabel.clipsToBounds = true
    controller.view.addSubview(toastLabel)
    controller.view.bringSubviewToFront(toastLabel)
  }

У меня есть вызов функции из viewController:

showLongToast(message: "Please Hold the lens or choose you can Enter Manually.", controller: self)

Но тостовое сообщение не может больше действовать? Есть идеи пожалуйста, оставьте комментарий. Спасибо.

Ответы [ 3 ]

1 голос
/ 12 июня 2019

Вот так должны выглядеть ваши viewDidLoad() и tapLabel(tap:),

override func viewDidLoad() {
    super.viewDidLoad()
    self.showLongToast(message: "Please Hold the lens or choose you can Enter Manually.", controller: self)
    let tap = UITapGestureRecognizer(target: self, action: #selector(tapLabel(tap:)))
    toastLabel.addGestureRecognizer(tap)
    toastLabel.isUserInteractionEnabled = true
}

@objc func tapLabel(tap: UITapGestureRecognizer) {
    print("tapped..!!!")
}

И подпись showLongToast не будет компилироваться с вашим кодом.Это должно быть как,

func showLongToast( message: String, controller: UIViewController) {
    //your code here...
}

enter image description here

0 голосов
/ 12 июня 2019
    override func viewDidLoad() {
    let tap = UITapGestureRecognizer(target: self, action: #selector(tapLabel(tap:)))
    toastLabel.addGestureRecognizer(tap)
    toastLabel.isUserInteractionEnabled = true
    }    
    func showLongToast( message: String) {        
        toastLabel.frame = CGRect(x: controller.view.frame.origin.x + 20, y: controller.view.frame.size.height-200, width: controller.view.frame.size.width - 40, height: 125)
        toastLabel.numberOfLines = 0
        toastLabel.textAlignment = .center
        toastLabel.contentMode = .center
        toastLabel.backgroundColor = UIColor.white.withAlphaComponent(0.7)
        toastLabel.textColor = UIColor(red: 74/255, green: 74/255, blue: 74/255, alpha: 1)
        toastLabel.font = UIFont(name: "Montserrat-Medium", size: 15.0)
        let trimmedString = message.trimmingCharacters(in: .whitespacesAndNewlines)
        let string = NSMutableAttributedString(string: trimmedString)
        string.setColorForText("Enter Manually", with: #colorLiteral(red: 1, green: 0.4196078431, blue: 0.1812392979, alpha: 1))
        toastLabel.attributedText = string
        toastLabel.layer.cornerRadius = 25
        toastLabel.clipsToBounds = true
        controller.view.addSubview(toastLabel)
        controller.view.bringSubviewToFront(toastLabel)
}
0 голосов
/ 12 июня 2019

Вы добавляете распознаватель жестов tap к toastLabel в viewDidLoad(), но затем вы создаете new UILabel внутри showLongToast()

Изменить:

toastLabel = UILabel(frame: CGRect(x: controller.view.frame.origin.x + 20, y: controller.view.frame.size.height-200, width: controller.view.frame.size.width - 40, height: 125))

в showLongToast() на:

toastLabel.frame = CGRect(x: controller.view.frame.origin.x + 20, y: controller.view.frame.size.height-200, width: controller.view.frame.size.width - 40, height: 125)
...