touchesBegan 未在 UIView 子类中调用
touchesBegan not called in UIView subclass
我知道已经有一些关于此的问题,但我找不到解决方案。
有一个非常简单的代码在我的一个项目上工作得很好但在另一个项目上却不行:
这是 UIView 子类的代码。
import UIKit
class test : UIView {
init(frame: CGRect, color: UIColor) {
super.init(frame: frame)
self.backgroundColor = color
self.userInteractionEnabled = true
}
required init(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
super.touchesBegan(touches, withEvent: event)
var alert = UIAlertView()
alert.title = "lol"
alert.message = "lol"
alert.addButtonWithTitle("Ok")
alert.show()
}
}
我确定解决方案很简单,但我找不到。
您需要确保在您视图的所有父视图上启用 userInteractionEnabled
。如果任何父视图将此 属性 设置为 false
,则不会为它们或它们的任何子视图处理触摸事件。
也许某些超级视图已将 userInteractionEnabled 设置为 "false"?
我知道已经有一些关于此的问题,但我找不到解决方案。 有一个非常简单的代码在我的一个项目上工作得很好但在另一个项目上却不行:
这是 UIView 子类的代码。
import UIKit
class test : UIView {
init(frame: CGRect, color: UIColor) {
super.init(frame: frame)
self.backgroundColor = color
self.userInteractionEnabled = true
}
required init(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
super.touchesBegan(touches, withEvent: event)
var alert = UIAlertView()
alert.title = "lol"
alert.message = "lol"
alert.addButtonWithTitle("Ok")
alert.show()
}
}
我确定解决方案很简单,但我找不到。
您需要确保在您视图的所有父视图上启用 userInteractionEnabled
。如果任何父视图将此 属性 设置为 false
,则不会为它们或它们的任何子视图处理触摸事件。
也许某些超级视图已将 userInteractionEnabled 设置为 "false"?