gpt4 book ai didi

ios - 尽管 userInteractionEnabled = true,但 UIView 中的 UIButton 不响应触摸事件

转载 作者:行者123 更新时间:2023-11-28 21:41:54 25 4
gpt4 key购买 nike

我很难解决这个问题。我有一个自定义 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
}

这些按钮被添加到另一个自定义 View ,该 View 被添加到自定义 View Controller 。将 PPButton 添加到自定义 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。所有这些都是以编程方式添加的。我确定我做错了什么小事。

此外,我还检查了所有 View ,从 ViewController 开始,哪些 View 的 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

最佳答案

所以看起来您的顶级 View 以及一些较低级别的 View 已将 userInteractionEnabled 设置为 false。这会向下级联,因此如果父 View 已关闭交互,则其 subview 将不会接收交互事件。

如果你想禁用某些 View 的交互,你可以重新排序你的层次结构,只需为这些父 View 打开交互或尝试这样的事情并传递事件:

How to get touches when parent view has userInteractionEnabled set to NO in iOS

关于ios - 尽管 userInteractionEnabled = true,但 UIView 中的 UIButton 不响应触摸事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31734619/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com