touchesBegan没有在UIView子类中调用 [英] touchesBegan not called in UIView subclass
问题描述
我知道已经有一些问题,但我找不到解决方案。
有一个非常简单的代码可以在我的一个项目上正常工作但不能在另一个项目上工作:
I know there already are some questions about this but I couldn't find a solution. There is a very simple code that works just fine on one of my projects but not on an another one:
这是UIView子类的代码。
Here is the code for subclass of 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()
}
}
我确定解决方案非常简单,但我可以'找到它。
I'm sure the solution is quite simple but I can't find it.
推荐答案
你需要确保 userInteractionEnabled
是已启用视图的所有超级视图。如果任何超级视图将此属性设置为 false
,则不会为他们或他们的任何子视图处理触摸事件。
You need to make sure that userInteractionEnabled
is enabled on all of the superviews of your view. If any of the superviews have this property set to false
, the touch events won't be processed for them or any of their subviews.
这篇关于touchesBegan没有在UIView子类中调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!