【发布时间】:2020-04-12 17:23:57
【问题描述】:
层次结构:
-
MainVC 调用
present(GameVC, animated: true, completion: nil)let vc = self.storyboard?.instantiateViewController(withIdentifier: "GameVC") as! GameVC self.present(vc, animated: true, completion: nil) GameVC具有覆盖整个 VC 的GameView(UIView的子类)
在GameView的初始化程序中,我有这段代码来配置滑动手势:
let leftGesture = UISwipeGestureRecognizer(target: self, action: #selector(leftSwipe))
leftGesture.direction = .left
self.addGestureRecognizer(leftGesture)
let rightGesture = UISwipeGestureRecognizer(target: self, action: #selector(rightSwipe))
rightGesture.direction = .right
self.addGestureRecognizer(rightGesture)
let downGesture = UISwipeGestureRecognizer(target: self, action: #selector(downSwipe))
downGesture.direction = .down
self.addGestureRecognizer(downGesture)
对应的选择器:
@objc func downSwipe() {
//code
}
@objc func leftSwipe() {
//code
}
@objc func rightSwipe() {
//code
}
选择器没有被调用。但是,当我将 GameVC 设置为正在显示的初始 VC 时(通过将情节提要箭头拖到 GameVC 上),手势按预期工作。这让我认为调用 present() 可能'已经搞乱了手势操作的层次结构,但我不太确定。
【问题讨论】:
-
什么是
self?它不可能是游戏视图控制器,因为 VC 没有addGestureRecognizer方法。present(GameVC, animated: true, completion: nil)不是真正的代码,因为GameVC似乎是一种类型。你能准确地展示你是如何展示这个 VC 的吗? “GameVC的初始化器”是指init,还是viewDidLoad? -
不能使用委托模式吗?函数在`GameVC`还是MainVC`?
-
你在哪个初始化方法中有代码?试着把它放在
viewDidLoad中 -
@Sweeper 我的错,我的意思是
GameView的初始化程序。我会更新我的问题。 -
@Sweeper 我没有详细说明我如何呈现
GameVC,因为它按预期工作,但如果有帮助,我会在我的问题中添加代码。
标签: ios swift selector uiswipegesturerecognizer presentviewcontroller