我在这个问题上遇到了困难。我有一个自定义UIView,其中包含一个UIImageView和一个UIButton。自定义UIView称为PPButton,添加这些控件的代码如下。需要注意的一件事是,我正在重写drawRect,这不应影响此处发生的情况。
private func addSubviews() {
let buttonFontMultipllier : CGFloat = 4.5 / 10.0
let buttonFontSize = (self.subRect!.height) * buttonFontMultipllier
self.ppButton = UIButton.buttonWithType(UIButtonType.Custom) as? UIButton
self.ppButton!.setTitle(self.ppButtonTitle!, forState: .Normal)
self.ppButton!.setTitleColor(UIColor.whiteColor(), forState: .Normal)
self.ppButton!.titleLabel!.font = UIFont(name: "Formata-BoldCondensed", size: buttonFontSize)
self.ppButton!.contentHorizontalAlignment = .Left
self.ppButton!.addTarget(self, action:"buttonWasTapped:", forControlEvents: UIControlEvents.TouchUpInside)
self.ppButton!.userInteractionEnabled = true
self.ppButton!.backgroundColor = UIColor.greenColor()
self.ppButton!.layer.borderColor = UIColor.greenColor().CGColor
self.ppButton!.layer.borderWidth = 1.0
self.ppImage = UIImageView()
self.ppImage!.contentMode = UIViewContentMode.ScaleAspectFit
self.ppImage!.userInteractionEnabled = false
self.ppImage!.layer.borderColor = UIColor.greenColor().CGColor
self.ppImage!.layer.borderWidth = 1.0
self.ppButtonView!.addSubview(self.ppButton!)
self.ppButtonView!.addSubview(self.ppImage!)
self.ppButtonView!.bringSubviewToFront(self.ppButton!)
self.ppButtonView!.sendSubviewToBack(self.ppImage!)
self.ppButtonView!.userInteractionEnabled = false;
self.addSubview(self.ppButtonView!)
superview!.layer.backgroundColor = UIColor.yellowColor().CGColor
}
这些按钮将添加到另一个自定义视图中,该视图将被添加到自定义视图控制器中。下面是用于将PPButtons添加到自定义HomeView的代码。
func addSubviews() {
self.homeLogo = UIImageView()
self.homeLogo!.contentMode = UIViewContentMode.ScaleAspectFit
self.addSubview(self.homeLogo!)
self.orderBtn = PPButton(title: "Order")
self.orderBtn!.delegate = self
self.orderBtn!.userInteractionEnabled = false
self.addSubview(self.orderBtn!)
self.findUsBtn = PPButton(title: "Find Us")
self.findUsBtn!.delegate = self
self.orderBtn!.userInteractionEnabled = false
self.addSubview(self.findUsBtn!)
self.menuBtn = PPButton(title: "Menu")
self.menuBtn!.delegate = self
self.orderBtn!.userInteractionEnabled = false
self.addSubview(self.menuBtn!)
self.clubPatronBtn = PPButton(title: "Club Patron")
self.clubPatronBtn!.delegate = self
self.orderBtn!.userInteractionEnabled = false
self.addSubview(self.clubPatronBtn!)
self.loginButton = UIButton()
self.loginButton!.setTitle(String("Login or Create an Account").uppercaseString, forState: .Normal)
self.loginButton!.setTitleColor(UIColor.whiteColor(), forState: .Normal)
self.loginButton!.titleLabel?.font = UIFont(name: "Formata-BoldCondensed", size: 18.0)
self.loginButton!.userInteractionEnabled = true
self.loginButton!.addTarget(self, action: Selector("testFunc"), forControlEvents: UIControlEvents.TouchUpInside)
self.addSubview(self.loginButton!)
}
当我尝试单击按钮时,没有为PPButton触发目标。此外,他们并没有为loginButton开火,后者只是一个简单的UIButton。所有这些都以编程方式添加。我确定我做错了一些小事。
另外,我从ViewController开始检查所有视图,其中userInteractionEnabled设置为true。功能和输出如下。请帮忙!
func logViewHierarchy(view: UIView, num: Int) {
var index = num
for i in 0..<index {
print("\t")
}
index = index + 1
println("\(NSStringFromClass(view.dynamicType)) userInteractionEnabled: \(view.userInteractionEnabled)")
for subview in view.subviews {
self.logViewHierarchy(subview as! UIView, num: index)
}
}
输出值
UIView userInteractionEnabled: false
UIImageView userInteractionEnabled: false
_UILayoutGuide userInteractionEnabled: true
_UILayoutGuide userInteractionEnabled: true
Pizza_Patron.HomeView userInteractionEnabled: false
UIImageView userInteractionEnabled: false
Pizza_Patron.PPButton userInteractionEnabled: false
UIView userInteractionEnabled: false
UIImageView userInteractionEnabled: false
UIButton userInteractionEnabled: true
UIButtonLabel userInteractionEnabled: false
Pizza_Patron.PPButton userInteractionEnabled: true
UIView userInteractionEnabled: false
UIImageView userInteractionEnabled: false
UIButton userInteractionEnabled: true
UIButtonLabel userInteractionEnabled: false
Pizza_Patron.PPButton userInteractionEnabled: true
UIView userInteractionEnabled: false
UIImageView userInteractionEnabled: false
UIButton userInteractionEnabled: true
UIButtonLabel userInteractionEnabled: false
Pizza_Patron.PPButton userInteractionEnabled: true
UIView userInteractionEnabled: false
UIImageView userInteractionEnabled: false
UIButton userInteractionEnabled: true
UIButtonLabel userInteractionEnabled: false
UIButton userInteractionEnabled: true
UIButtonLabel userInteractionEnabled: false
最佳答案
因此,看起来您的顶层视图以及一些下层视图已将userInteractionEnabled
设置为false
。这会级联下来,因此如果一个超级视图关闭了交互,则其子视图将不会接收交互事件。
如果要禁用某些视图的交互,可以重新排列层次结构,只需为这些超级视图打开交互或尝试类似的操作并通过事件即可:
How to get touches when parent view has userInteractionEnabled set to NO in iOS