addGestureRecognizer для UIView не вызывается из класса @IBDesignable - PullRequest
0 голосов
/ 04 апреля 2020

Это код моего класса @IBDesignable UIView. Я уже пытался поместить его в начале до того, как было сделано containerView, после того, как было сделано также. но ничего не работает. Пожалуйста, скажите мне, что не так. Я уже включил userInteractionEnabled в true, а также. Пожалуйста, попросите всю вашу помощь здесь.

import UIKit

public protocol StatusEmblemProtocol : class {
    func statusEmblemClicked()
}

@IBDesignable
final class StatusView: UIView {

    @IBInspectable var borderWidth: Double {
          get {
            return Double(self.layer.borderWidth)
          }
          set {
           self.layer.borderWidth = CGFloat(newValue)
          }
    }
    @IBInspectable var borderColor: UIColor? {
         get {
            return UIColor(cgColor: self.layer.borderColor!)
         }
         set {
            self.layer.borderColor = newValue?.cgColor
         }
    }
    @IBInspectable var shadowColor: UIColor? {
        get {
           return UIColor(cgColor: self.layer.shadowColor!)
        }
        set {
           self.layer.shadowColor = newValue?.cgColor
        }
    }
    @IBInspectable var shadowOpacity: Float {
        get {
           return self.layer.shadowOpacity
        }
        set {
           self.layer.shadowOpacity = newValue
       }
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        commonInit()
    }

    public required init?(coder: NSCoder) {
        super.init(coder: coder)
        commonInit()
    }

    let containerView = UIView()
    let imageView = UIImageView()
    let statusLabel = UILabel()
    let statusFont : UIFont = UIFont(name: "Roboto-Bold", size: 10)!

    public weak var delegate: StatusEmblemProtocol?

    func commonInit() {
        setupContainerView()
        setupImageView()
        setupLabel()
        setupRecognizer()
    }

    func setupRecognizer() {
        let tap = UITapGestureRecognizer(target: self, action: #selector(containerClicked))
        self.isUserInteractionEnabled = true
        self.addGestureRecognizer(tap)
    }

    func setupContainerView() {
        self.addSubview(containerView)
        containerView.isUserInteractionEnabled = true
        containerView.translatesAutoresizingMaskIntoConstraints = false
        containerView.layer.cornerRadius = 9
        containerView.heightAnchor.constraint(equalToConstant: 21).isActive = true
        containerView.backgroundColor = UIColor(hex: "#F2F2F2")
        containerView.layer.borderColor = UIColor(hex: "#707070")?.cgColor
    }

    func setupImageView() {
        containerView.addSubview(imageView)
        imageView.translatesAutoresizingMaskIntoConstraints = false
        imageView.widthAnchor.constraint(equalToConstant: 10).isActive = true
        imageView.heightAnchor.constraint(equalToConstant: 10).isActive = true
        imageView.backgroundColor = UIColor(hex: "#838383")

        NSLayoutConstraint.activate([
        imageView.leftAnchor.constraint(equalTo: containerView.leftAnchor, constant: 10),
        imageView.centerYAnchor.constraint(equalTo: containerView.centerYAnchor)])
    }

    func setupLabel() {
        containerView.addSubview(statusLabel)
        statusLabel.translatesAutoresizingMaskIntoConstraints = false
        statusLabel.numberOfLines = 1
        statusLabel.textColor = UIColor(hex: "#838383")
        statusLabel.font = statusFont

        NSLayoutConstraint.activate([
            statusLabel.centerYAnchor.constraint(equalTo: containerView.centerYAnchor),
            statusLabel.leftAnchor.constraint(equalTo: imageView.rightAnchor, constant: 10),
            statusLabel.rightAnchor.constraint(equalTo: containerView.rightAnchor, constant: 10)])
    }

    public func setTitleLabel(status: String) {
        if status.count > 0 {
            statusLabel.text = status
            let widthLabel = statusLabel.text?.width(withConstrainedHeight: 21, font: statusFont)
            containerView.widthAnchor.constraint(equalToConstant: widthLabel! + 36).isActive = true
            containerView.isHidden = false
            containerView.isUserInteractionEnabled = true
        } else {
            containerView.isHidden = true
        }
    }

    @objc func containerClicked() {
        print("container clicked")
    }
}
...