0

我目前正在使用 Swift 3、SpriteKit 和 Xcode 8 beta 开发游戏。我正在尝试通过 info.plist 从主屏幕实现静态 3D Touch 快速操作。目前,这些动作从主屏幕看起来很好,但不要转到正确的 SKScene - 转到初始场景或最后打开的场景(如果应用程序仍然打开),这意味着场景没有被更改。我尝试了各种在 switch 语句中设置场景的方法,但似乎没有一种方法可以正常显示 SKScene,因为该行window!.rootViewController?.present(gameViewController, animated: true, completion: nil) 仅适用于 UIViewController。

此代码的各个部分来自各种教程,但通过我的调查,我相当确定损坏的部分是正在呈现的场景(除非我错了),因为如果部分损坏,它甚至不应该加载任何场景.

有什么方法可以从 AppDelegate 呈现 SKScene 或根据 switch 语句设置开场场景?

应用委托

import UIKit
import SpriteKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.

        guard !handledShortcutItemPress(forLaunchOptions: launchOptions) else { return false } // ADD THIS LINE

        return true
    }
}

extension AppDelegate: ShortcutItem {

    /// Perform action for shortcut item. This gets called when app is active
    func application(_ application: UIApplication, performActionForShortcutItem shortcutItem: UIApplicationShortcutItem, completionHandler: (Bool) -> Void) {
        completionHandler(handledShortcutItemPress(forItem: shortcutItem))

    }
}

extension AppDelegate: ShortcutItemDelegate {

    func shortcutItem1Pressed() {

        Timer.scheduledTimer(timeInterval: 0.5, target: self, selector: #selector(loadShopScene), userInfo: nil, repeats: false)
    }

    @objc private func loadShopScene() {
        let scene = ShopScene(size: CGSize(width: 768, height: 1024))
        loadScene(scene: scene, view: window?.rootViewController?.view)
    }

    func shortcutItem2Pressed() {
       Timer.scheduledTimer(timeInterval: 0.5, target: self, selector: #selector(loadGameScene), userInfo: nil, repeats: false)
    }

    @objc private func loadGameScene() {
        let scene = GameScene(size: CGSize(width: 768, height: 1024))
        loadScene(scene: scene, view: window?.rootViewController?.view)
    }

    func shortcutItem3Pressed() {
        // do something else
    }

    func shortcutItem4Pressed() {
        // do something else
    }

    func loadScene(scene: SKScene?, view: UIView?, scaleMode: SKSceneScaleMode = .aspectFill) {
        guard let scene = scene else { return }
        guard let skView = view as? SKView else { return }

        skView.ignoresSiblingOrder = true
        #if os(iOS)
            skView.isMultipleTouchEnabled = true
        #endif
        scene.scaleMode = scaleMode
        skView.presentScene(scene)
    }
}

3DTouchQuickActions.swift

import Foundation
import UIKit

/// Shortcut item delegate
protocol ShortcutItemDelegate: class {
    func shortcutItem1Pressed()
    func shortcutItem2Pressed()
    func shortcutItem3Pressed()
    func shortcutItem4Pressed()
}

/// Shortcut item identifier
enum ShortcutItemIdentifier: String {
    case first // I use swift 3 small letters so you have to change your spelling in the info.plist
    case second
    case third
    case fourth

     init?(fullType: String) {
        guard let last = fullType.components(separatedBy: ".").last else { return nil }
        self.init(rawValue: last)
    }

    public var type: String {
        return Bundle.main.bundleIdentifier! + ".\(self.rawValue)"
    }
}

/// Shortcut item protocol
protocol ShortcutItem { }
extension ShortcutItem {

    // MARK: - Properties

    /// Delegate
    private weak var delegate: ShortcutItemDelegate? {
        return self as? ShortcutItemDelegate
    }

    // MARK: - Methods

    /// Handled shortcut item press first app launch (needed to avoid double presses on first launch)
    /// Call this in app Delegate did launch with options and exit early (return false) in app delegate if this method returns true
    ///
    /// - parameter forLaunchOptions: The [NSObject: AnyObject]? launch options to pass in
    /// - returns: Bool
    func handledShortcutItemPress(forLaunchOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

        guard let launchOptions = launchOptions, let shortcutItem = launchOptions[UIApplicationLaunchOptionsKey.shortcutItem] as? UIApplicationShortcutItem else { return false }

        handledShortcutItemPress(forItem: shortcutItem)
        return true
    }

    /// Handle shortcut item press
    /// Call this in the completion handler in AppDelegate perform action for shortcut item method
    ///
    /// - parameter forItem: The UIApplicationShortcutItem the press was handled for.
    /// - returns: Bool
    func handledShortcutItemPress(forItem shortcutItem: UIApplicationShortcutItem) -> Bool {
        guard let _ = ShortcutItemIdentifier(fullType: shortcutItem.type) else { return false }
        guard let shortcutType = shortcutItem.type as String? else { return false }

        switch shortcutType {

        case ShortcutItemIdentifier.first.type:
            delegate?.shortcutItem1Pressed()

        case ShortcutItemIdentifier.second.type:
            delegate?.shortcutItem2Pressed()

        case ShortcutItemIdentifier.third.type:
            delegate?.shortcutItem3Pressed()

        case ShortcutItemIdentifier.fourth.type:
            delegate?.shortcutItem4Pressed()

        default:
            return false
        }

        return true
    }
}
4

1 回答 1

1

您的代码不起作用,因为在您的应用程序委托中,您创建了一个新的 GameViewController 实例,而不是引用当前的实例

let gameViewController = GameViewController() // creates new instance

在我的 2 款游戏中,我正在做的正是您尝试使用 3d touch 快速操作所做的事情。我直接从 appDelegate 加载场景,不要尝试为此更改 gameViewController 场景。

我为此使用了一个可重用的助手。假设您在 info.plist 中正确设置了所有内容。(我在枚举中使用小写字母,因此在 info.plist 中以 .first、.second 等结束您的项目),删除您之前为 3d touch 快速操作而拥有的所有应用程序委托代码。比在您的项目中创建一个新的 .swift 文件并添加此代码

这是 swift 3 代码。

import UIKit

/// Shortcut item delegate
protocol ShortcutItemDelegate: class {
    func shortcutItemDidPress(_ identifier: ShortcutItemIdentifier)   
}

 /// Shortcut item identifier
enum ShortcutItemIdentifier: String {
     case first // I use swift 3 small letters so you have to change your spelling in the info.plist 
     case second
     case third
     case fourth

     private init?(fullType: String) {
          guard let last = fullType.componentsSeparatedByString(".").last else { return nil }
          self.init(rawValue: last)
      }

      public var type: String {
           return (Bundle.main.bundleIdentifier ?? "NoBundleIDFound") + ".\(rawValue)"
      }
  }

  /// Shortcut item protocol
  protocol ShortcutItem { } 
  extension ShortcutItem {

  // MARK: - Properties

  /// Delegate
  private weak var delegate: ShortcutItemDelegate? {
        return self as? ShortcutItemDelegate
  }

  // MARK: - Methods

 func didPressShortcutItem(withOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    guard let shortcutItem = launchOptions?[.shortcutItem] as? UIApplicationShortcutItem else { return false }
    didPressShortcutItem(shortcutItem)
    return true
}


/// Handle item press
@discardableResult
func didPressShortcutItem(_ shortcutItem: UIApplicationShortcutItem) -> Bool {
    guard let _ = ShortcutItemIdentifier(fullType: shortcutItem.type) else { return false }

    switch shortcutItem.type {

    case ShortcutItemIdentifier.first.type:
        delegate?.shortcutItemDidPress(.first)

    case ShortcutItemIdentifier.second.type:
        delegate?.shortcutItemDidPress(.second)

    case ShortcutItemIdentifier.third.type:
        delegate?.shortcutItemDidPress(.third)

    case ShortcutItemIdentifier.fourth.type:
        delegate?.shortcutItemDidPress(.fourth)

    default:
        return false
    }

    return true
   }
}

比在您的应用程序委托中使用此方法创建扩展(您在代码中错过了这一点)

extension AppDelegate: ShortcutItem {

   /// Perform action for shortcut item. This gets called when app is active
   func application(_ application: UIApplication, performActionFor shortcutItem: UIApplicationShortcutItem, completionHandler: (Bool) -> Void) {
        completionHandler(didPressShortcutItem(shortcutItem))
   }

比您需要调整 AppDelegate 中的 didFinishLaunchingWithOptions 方法看起来像这样

  func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.

    ...

    return !didPressShortcutItem(withOptions: launchOptions)
}

最后创建另一个扩展来确认 ShortcutItem 委托

extension AppDelegate: ShortcutItemDelegate {

  func shortcutItemDidPress(_ identifier: ShortcutItemIdentifier) {

    switch identifier {
    case .first:
        let scene = GameScene(size: CGSize(width: 1024, height: 768))
        loadScene(scene, view: window?.rootViewController?.view)
    case .second:
         //
    case .third:
        //
    case .fourth:
       //
    }
 }

 func loadScene(scene: SKScene?, view: UIView?, scaleMode: SKSceneScaleMode = .aspectFill) {
    guard let scene = scene else { return }
    guard let skView = view as? SKView else { return }

    skView.ignoresSiblingOrder = true
    #if os(iOS)
        skView.isMultipleTouchEnabled = true
    #endif
    scene.scaleMode = scaleMode
    skView.presentScene(scene)
   }
}

我通常在另一个助手中使用的加载场景方法,这就是我将视图传递给 func 的原因。

希望这可以帮助。

于 2016-08-20T14:18:29.207 回答