[英]Swift & SpriteKit - How to present alert view in GameScene
我需要帮助在游戏场景中显示警报视图。 我目前正在努力这样做,因为 GameScene.Swift 不是标准的 ViewController。 如果有帮助,我需要这样做,因为我需要用户输入一个值,该值用作我游戏中球 Sprite Kit 节点的坐标。 输入只是一个标准整数,所以这不是问题。 我也欢迎任何其他不是通过警报视图执行此操作的想法。
override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
let view = self.view as! SKView
if view.scene == nil {
view.showsFPS = false
view.showsNodeCount = false
let gameScene = GameScene(size: view.bounds.size)
gameScene.scaleMode = SKSceneScaleMode.AspectFill
view.presentScene(gameScene)
}
}
那是在 GameViewController 文件中
var vc : GameViewController!
override init(size: CGSize) {
super.init(size: size)
let alertController = UIAlertController(title: "Bll Starting Position", message: "Please Enter a X Coordinate Value IN Range 0 to 345 ", preferredStyle: .Alert)
alertController.addTextFieldWithConfigurationHandler { (textField) in
textField.placeholder = "Value Must Be In Range 0 To 345"
textField.autocapitalizationType = UITextAutocapitalizationType.None
textField.autocorrectionType = UITextAutocorrectionType.No
textField.clearsOnBeginEditing = true
textField.clearsOnInsertion = true
textField.clearButtonMode = UITextFieldViewMode.Always
let cancelBtn = UIAlertAction(title: "Cancel", style: .Cancel, handler: nil)
let confirmBtn = UIAlertAction(title: "Confirm", style: .Default, handler: { (confirmView) in
if let field = alertController.textFields![0] as? UITextField {
}
})
alertController.addAction(confirmBtn)
alertController.addAction(cancelBtn)
self.vc.presentViewController(alertController, animated: true, completion: nil)
}
谢谢
您可以直接从 SKScene 显示 UIAlertControllers,只需将它们显示在 rootViewController 上,这可能是显示它们的最佳位置。
view?.window?.rootViewController?.present...
一般来说,在 SKScenes 中引用 GameViewController 并不是最佳实践,而且我实际上从未达到过被迫这样做的程度。 NSNotificationCenter、委托或协议扩展是更好的方法。
我实际上使用了我使用 Swift 2 的协议扩展制作的警报助手。
只需创建一个新的 .swift 文件并添加此代码
import SpriteKit
protocol Alertable { }
extension Alertable where Self: SKScene {
func showAlert(withTitle title: String, message: String) {
let alertController = UIAlertController(title: title, message: message, preferredStyle: .alert)
let okAction = UIAlertAction(title: "OK", style: .cancel) { _ in }
alertController.addAction(okAction)
view?.window?.rootViewController?.present(alertController, animated: true)
}
func showAlertWithSettings(withTitle title: String, message: String) {
let alertController = UIAlertController(title: title, message: message, preferredStyle: .alert)
let okAction = UIAlertAction(title: "OK", style: .cancel) { _ in }
alertController.addAction(okAction)
let settingsAction = UIAlertAction(title: "Settings", style: .default) { _ in
guard let url = URL(string: UIApplicationOpenSettingsURLString) else { return }
if #available(iOS 10.0, *) {
UIApplication.shared.open(url)
} else {
UIApplication.shared.openURL(url)
}
}
alertController.addAction(settingsAction)
view?.window?.rootViewController?.present(alertController, animated: true)
}
}
现在在您的场景中,您需要显示警报,您只需遵守协议
class GameScene: SKScene, Alertable {
}
并调用类似的方法
showAlert(withTitle: "Alert title", message: "Alert message")
仿佛它们是场景本身的一部分。
希望这可以帮助
可能有以下选项:
1)快速解决。 不要使用UIAlertController
,使用UIAlertView
。 像那样:
alert.show()
但是, UIAlertView
已被弃用,因此依赖它并不十分安全。
2)更好的解决方案。 使您的SKScene
子类持有对用于呈现场景的视图控制器的引用,并在创建场景时为其分配视图控制器:
myScene.viewController = self
然后你就可以使用它了。
self.viewController.presentViewController(alertController, animated: true, completion: nil)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.