swift - 未从 @IBDesignable 类调用 UIView 的 addGestureRecognizer
问题描述
这是我的@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")
}
}
解决方案
推荐阅读
- function - 递归调用私有方法
- python - 为什么我在使用 selfbot 运行命令时收到错误“send() got an unexpected keyword argument”?
- z3 - z3py 有时会在 Solver.check() 上挂起它可以解决的公式
- javascript - 应用转换属性(纯 JavaScript)时,如何从 SVG 路径的属性“d”获取当前值?
- amazon-web-services - 关于使用 aws cli 将代码部署到 EC2 实例的问题
- javascript - 当数组中的数据发生变化时如何自动触发函数?
- sqlalchemy - 如何将具有计数结果的列添加到 sqlalchemy 查询并通过 pydantic 检查?
- ios - TableView 部分展开和折叠披露按钮需要 2 次点击以展开和折叠
- python - 如何获取索引位置 1 处列表中重复值的计数?
- google-analytics - 在网络和移动设备之间跟踪用户