仅在按下按钮后才允许触摸
Allow touch only after button is pressed
在我的上一个问题中,我学会了在游戏中放置一个计时器,以便在单击开始按钮后,用户必须等待几秒钟才能触摸屏幕。该代码如下所示:
self.view?.isUserInteractionEnabled = false
DispatchQueue.main.asyncAfter(deadline: .now() + 4.0) {
self.view?.isUserInteractionEnabled = true
}
我保留了最后一个 post 以供其他人学习。这次我想知道您将如何在按下按钮之前完全不允许触摸!我将上面的 true 语句切换为“false”,因此用户在第一次进入游戏时无法触摸。但是,我希望用户能够单击开始按钮,然后在游戏过程中允许所有其他触摸。谢谢!
一个非常简单的方法就是创建一个 UIView 的子类来覆盖 func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView?
.
然后在将事件转发到其他子视图之前,您只需检查按钮是否已被按下。
class View: UIView {
private var hasTappedButton = false
let button = UIButton()
override init(frame: CGRect) {
super.init(frame: frame)
configureButton()
}
required init?(coder: NSCoder) {
super.init(coder: coder)
configureButton()
}
private func configureButton() {
button.addTarget(self, action: #selector(tappedButton), for: .touchUpInside)
}
override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
let view = super.hitTest(point, with: event)
guard !hasTappedButton else { return view }
return view == button ? button : nil
}
@objc private func tappedButton() {
hasTappedButton = true
}
}
I want the user to be able to click the start button to then allow all other touch during the game
我的解决方案是在整个界面前面放置一个不可见的视图并使用命中测试修改,以便它后面只有一个视图(self.passthruView
,您的按钮)是可触摸的:
class MyView: UIView {
weak var passthruView : UIView?
override func hitTest(_ point: CGPoint, with e: UIEvent?) -> UIView? {
if let pv = self.passthruView {
let pt = pv.convert(point, from: self)
if pv.point(inside: pt, with: e) {
return nil
}
}
return super.hitTest(point, with: e)
}
}
所以只要 MyView 位于(不可见的)整个界面的前面,只能 可以触摸按钮。然后你拿走MyView,整个界面又变成可触摸了。
在我的上一个问题中,我学会了在游戏中放置一个计时器,以便在单击开始按钮后,用户必须等待几秒钟才能触摸屏幕。该代码如下所示:
self.view?.isUserInteractionEnabled = false
DispatchQueue.main.asyncAfter(deadline: .now() + 4.0) {
self.view?.isUserInteractionEnabled = true
}
我保留了最后一个 post 以供其他人学习。这次我想知道您将如何在按下按钮之前完全不允许触摸!我将上面的 true 语句切换为“false”,因此用户在第一次进入游戏时无法触摸。但是,我希望用户能够单击开始按钮,然后在游戏过程中允许所有其他触摸。谢谢!
一个非常简单的方法就是创建一个 UIView 的子类来覆盖 func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView?
.
然后在将事件转发到其他子视图之前,您只需检查按钮是否已被按下。
class View: UIView {
private var hasTappedButton = false
let button = UIButton()
override init(frame: CGRect) {
super.init(frame: frame)
configureButton()
}
required init?(coder: NSCoder) {
super.init(coder: coder)
configureButton()
}
private func configureButton() {
button.addTarget(self, action: #selector(tappedButton), for: .touchUpInside)
}
override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
let view = super.hitTest(point, with: event)
guard !hasTappedButton else { return view }
return view == button ? button : nil
}
@objc private func tappedButton() {
hasTappedButton = true
}
}
I want the user to be able to click the start button to then allow all other touch during the game
我的解决方案是在整个界面前面放置一个不可见的视图并使用命中测试修改,以便它后面只有一个视图(self.passthruView
,您的按钮)是可触摸的:
class MyView: UIView {
weak var passthruView : UIView?
override func hitTest(_ point: CGPoint, with e: UIEvent?) -> UIView? {
if let pv = self.passthruView {
let pt = pv.convert(point, from: self)
if pv.point(inside: pt, with: e) {
return nil
}
}
return super.hitTest(point, with: e)
}
}
所以只要 MyView 位于(不可见的)整个界面的前面,只能 可以触摸按钮。然后你拿走MyView,整个界面又变成可触摸了。