UIView touchesBegan 未被调用
UIView touchesBegan not being called
我创建了 UIView
的子类并通过引用 xib 文件添加到视图中。
我重写了 touchesBegan
和 touchesEnd
函数,以便获得 "color changes when user presses on this view" 的自定义效果。
下面是这个子类:
class CustomUIView: UIView {
@IBOutlet var contentView: UIView!
override init (frame: CGRect) {
super.init(frame: frame)
commonInit()
}
required init? (coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
commonInit()
}
private func commonInit () {
Bundle.main.loadNibNamed("CustomUIView", owner: self, options: nil)
addSubview(contentView)
contentView.frame = self.bounds
contentView.autoresizingMask = [.flexibleHeight, .flexibleWidth]
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
print("touch begin")
super.touchesBegan(touches, with: event)
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
print("touch ended")
super.touchesEnded(touches, with: event)
}
}
然而,touchesBegan
和 touchesEnd
从未被调用。
我用 google 和 Whosebug 进行了搜索,已经尝试了 3 个小时,但找不到解决这个简单问题的有效方法....
以下是我尝试过的:
- 确保所有超级视图都启用了 UserInteraction。我什至
捕获视图层次结构以检查这一点。
- 添加了一个
IBAction
的touchUpInside,看是否可以触发这个IBAction。答案是肯定的。
- 改为子类 UIControl 并覆盖
beginTracking
。但是也没有触发。
以下是我可以从该 CustomView 的 ViewHierarchy 中看到的属性:
- 已启用
- 启用用户交互
- 多次触发关闭
- 阿尔法 1
- 背景
<nil color>
(我也试过给它设置一个颜色。没用。)
- 不透明开启
- 隐藏关闭
- 清除图形上下文
- 剪辑到边界外
- 自动调整子视图大小
非常感谢帮助!
请试试这个,这是由于您的 CustomView
的框架
class CustomUIView: UIView {
var contentView: UIView!
override init (frame: CGRect) {
super.init(frame: frame)
commonInit()
}
required init? (coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
commonInit()
}
func loadFromNib() -> UIView {
let bundle = Bundle(for: type(of: self))
let nib = UINib(nibName: String(describing: type(of: self)), bundle: bundle)
let view = nib.instantiate(withOwner: self, options: nil).first as! UIView
return view
}
private func commonInit () {
contentView = self.loadFromNib()
addSubview(contentView)
contentView.frame = self.bounds
self .isUserInteractionEnabled = true
contentView.autoresizingMask = [.flexibleHeight, .flexibleWidth]
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
print("touch begin")
super.touchesBegan(touches, with: event)
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
print("touch ended")
super.touchesEnded(touches, with: event)
}
}
我可以在控制台中看到日志
在您的 ViewController 中拖动一个 UIView 并设置您的 customview class 作为 CustomUIView
感谢@Fahri Azimov 的评论,原因是 contentView
启用了用户交互。因此它在传递给 CustomUIView
之前消耗了所有触摸事件。我赞成他的评论。
教训是,xib 文件的 rootView 不是 CustomUIView
本身,而是它的子视图之一。
我创建了 UIView
的子类并通过引用 xib 文件添加到视图中。
我重写了 touchesBegan
和 touchesEnd
函数,以便获得 "color changes when user presses on this view" 的自定义效果。
下面是这个子类:
class CustomUIView: UIView {
@IBOutlet var contentView: UIView!
override init (frame: CGRect) {
super.init(frame: frame)
commonInit()
}
required init? (coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
commonInit()
}
private func commonInit () {
Bundle.main.loadNibNamed("CustomUIView", owner: self, options: nil)
addSubview(contentView)
contentView.frame = self.bounds
contentView.autoresizingMask = [.flexibleHeight, .flexibleWidth]
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
print("touch begin")
super.touchesBegan(touches, with: event)
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
print("touch ended")
super.touchesEnded(touches, with: event)
}
}
然而,touchesBegan
和 touchesEnd
从未被调用。
我用 google 和 Whosebug 进行了搜索,已经尝试了 3 个小时,但找不到解决这个简单问题的有效方法....
以下是我尝试过的:
- 确保所有超级视图都启用了 UserInteraction。我什至 捕获视图层次结构以检查这一点。
- 添加了一个
IBAction
的touchUpInside,看是否可以触发这个IBAction。答案是肯定的。 - 改为子类 UIControl 并覆盖
beginTracking
。但是也没有触发。
以下是我可以从该 CustomView 的 ViewHierarchy 中看到的属性:
- 已启用
- 启用用户交互
- 多次触发关闭
- 阿尔法 1
- 背景
<nil color>
(我也试过给它设置一个颜色。没用。) - 不透明开启
- 隐藏关闭
- 清除图形上下文
- 剪辑到边界外
- 自动调整子视图大小
非常感谢帮助!
请试试这个,这是由于您的 CustomView
的框架class CustomUIView: UIView {
var contentView: UIView!
override init (frame: CGRect) {
super.init(frame: frame)
commonInit()
}
required init? (coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
commonInit()
}
func loadFromNib() -> UIView {
let bundle = Bundle(for: type(of: self))
let nib = UINib(nibName: String(describing: type(of: self)), bundle: bundle)
let view = nib.instantiate(withOwner: self, options: nil).first as! UIView
return view
}
private func commonInit () {
contentView = self.loadFromNib()
addSubview(contentView)
contentView.frame = self.bounds
self .isUserInteractionEnabled = true
contentView.autoresizingMask = [.flexibleHeight, .flexibleWidth]
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
print("touch begin")
super.touchesBegan(touches, with: event)
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
print("touch ended")
super.touchesEnded(touches, with: event)
}
}
我可以在控制台中看到日志
在您的 ViewController 中拖动一个 UIView 并设置您的 customview class 作为 CustomUIView
感谢@Fahri Azimov 的评论,原因是 contentView
启用了用户交互。因此它在传递给 CustomUIView
之前消耗了所有触摸事件。我赞成他的评论。
教训是,xib 文件的 rootView 不是 CustomUIView
本身,而是它的子视图之一。