pressesBegan 未在 Playground Book 中调用
pressesBegan not called in Playground Book
我需要响应在物理键盘上按下的键,但方法 pressesBegan
没有在任何 class 上调用。我试过将它放入我的 SKScene 和我的 ViewController,但它就是没有被识别
UPDATE 这是代码,我用 fatalError()
替换了逻辑,这样当 pressesBegan
被调用时这本书就会崩溃。因为它没有崩溃,所以 pressesBegan
没有被调用
其余代码与响应者无关
import Foundation
import SpriteKit
import SceneKit
import UIKit
public class GameScene: SKScene {
// ... some code
public override func pressesBegan(_ presses: Set<UIPress>, with event: UIPressesEvent?) {
fatalError()
}
/// Some more code...
}
class GameViewController: UIViewController {
override func loadView(){
self.view = sceneView
}
let sceneView = SKView(frame: CGRect(x:0 , y:0, width: 1920, height: 1080))
override func viewDidLoad() {
if let scene = GameScene(fileNamed: "GameScene") {
// Set the scale mode to scale to fit the window
scene.scaleMode = .aspectFill
// Present the scene
sceneView.presentScene(scene)
}
}
public override func pressesBegan(_ presses: Set<UIPress>, with event: UIPressesEvent?) {
fatalError()
}
}
let viewController = GameViewController()
viewController.view.becomeFirstResponder()
PlaygroundPage.current.liveView = viewController
PlaygroundPage.current.needsIndefiniteExecution = true
解决方法是将 override var canBecomeFirstResponder { true }
添加到 UIViewController
,在呈现控制器后调用 myViewController.becomeFirstResponder()
并在我不处理压力机时调用 super.pressesBegan
我需要响应在物理键盘上按下的键,但方法 pressesBegan
没有在任何 class 上调用。我试过将它放入我的 SKScene 和我的 ViewController,但它就是没有被识别
UPDATE 这是代码,我用 fatalError()
替换了逻辑,这样当 pressesBegan
被调用时这本书就会崩溃。因为它没有崩溃,所以 pressesBegan
没有被调用
其余代码与响应者无关
import Foundation
import SpriteKit
import SceneKit
import UIKit
public class GameScene: SKScene {
// ... some code
public override func pressesBegan(_ presses: Set<UIPress>, with event: UIPressesEvent?) {
fatalError()
}
/// Some more code...
}
class GameViewController: UIViewController {
override func loadView(){
self.view = sceneView
}
let sceneView = SKView(frame: CGRect(x:0 , y:0, width: 1920, height: 1080))
override func viewDidLoad() {
if let scene = GameScene(fileNamed: "GameScene") {
// Set the scale mode to scale to fit the window
scene.scaleMode = .aspectFill
// Present the scene
sceneView.presentScene(scene)
}
}
public override func pressesBegan(_ presses: Set<UIPress>, with event: UIPressesEvent?) {
fatalError()
}
}
let viewController = GameViewController()
viewController.view.becomeFirstResponder()
PlaygroundPage.current.liveView = viewController
PlaygroundPage.current.needsIndefiniteExecution = true
解决方法是将 override var canBecomeFirstResponder { true }
添加到 UIViewController
,在呈现控制器后调用 myViewController.becomeFirstResponder()
并在我不处理压力机时调用 super.pressesBegan