简体   繁体   English

Peek和Pop函数SpriteKit

[英]Peek and Pop Function SpriteKit

I have a menu in an `SKScene'. 我有一个'SKScene'的菜单。 I'm trying to integrate a Peek and Pop function into it. 我正在尝试将Peek和Pop功能集成到其中。 I have multiple possible selections so my problem is how to identify which one is being force touched. 我有多种可能的选择,所以我的问题是如何识别哪一个被强制触摸。

This is my code: 这是我的代码:

import SpriteKit
import UIKit

class Menu: SKScene, UIViewControllerPreviewingDelegate {

//SETUP Menu


 func previewingContext(_ previewingContext: UIViewControllerPreviewing, viewControllerForLocation location: CGPoint) -> UIViewController? {
        return UIViewController()
    }

    //POP
    func previewingContext(_ previewingContext: UIViewControllerPreviewing, commit viewControllerToCommit: UIViewController) {
        return
    }

}

GameViewController.swift: GameViewController.swift:

import UIKit
import SpriteKit

class GameViewController: UIViewController {


    override func viewDidLoad() {
    super.viewDidLoad()

    if let scene = GameScene(fileNamed:"GameScene") {
       if self.traitCollection.forceTouchCapability == .available {
          registerForPreviewing(with: scene as! UIViewControllerPreviewingDelegate, sourceView: scene.view!)
          }
         }

       // Move to Menu

 }
}

I'm pretty sure that I should be passing the information in the sourceView field. 我很确定我应该在sourceView字段中传递信息。 But since this is in GameViewController I don't know what to do. 但由于这是在GameViewController中,我不知道该怎么做。 And I don't know how to do this inside the Menu, where would I put this code? 我不知道如何在菜单中执行此操作,我将在哪里放置此代码?

 if let scene = GameScene(fileNamed:"GameScene") {
            if self.traitCollection.forceTouchCapability == .available {
                registerForPreviewing(with: scene as! UIViewControllerPreviewingDelegate, sourceView: ButtonPressed)
            }
        }

You will not be able to use Peek and Pop in a SKScene or a SKSpriteNode menu like you are trying, I was trying to do the same in one of my games. 你将无法在SKScene或SKSpriteNode菜单中使用Peek和Pop,就像你正在尝试一样,我试图在我的一个游戏中做同样的事情。

Peek and Pop only works with UIKit, specifically it allows you to preview (Peek) and than open (Pop) UIViewControllers. Peek和Pop仅适用于UIKit,特别是它允许您预览(Peek)和打开(Pop)UIViewControllers。

In SpriteKit you normally only have 1 ViewController, GameViewController. 在SpriteKit中,您通常只有1个ViewController,GameViewController。 That GameViewController should only handle your SKScene presentation, your actual UI is/should be done using SpriteKit APIs such as SKLabelNodes, SKSpriteNodes etc directly in the relevant SKScenes. GameViewController应该只处理你的SKScene演示文稿,你的实际用户界面是/应该在相关的SKScenes中直接使用SpriteKit API,如SKLabelNodes,SKSpriteNodes等。 Therefore you dont really use UIKit and have not much you can preview with Peek and Pop. 因此,你真的没有使用UIKit,你可以用Peek和Pop预览。

There are some exceptions to this so it depends what kind of game you are making. 这有一些例外,所以它取决于你正在制作什么样的游戏。 For example in one of my games I am using a UICollectionViewController for my level select menu (40 levels) as they are perfect for that scenario (cell reuse, smooth scrolling etc). 例如,在我的一个游戏中,我使用UICollectionViewController作为我的级别选择菜单(40级),因为它们非常适合该场景(单元重用,平滑滚动等)。 Because that menu is made with UIKit I can register for peek and pop when you force touch on a cell. 因为该菜单是使用UIKit制作的,所以当您强行触摸单元格时,我可以注册窥视和弹出。 I than have another UIViewController with some UILabels, UIImageViews etc I specifically use for the Peeking (see picture) and when I pop I just start the actual level. 我有另一个UIViewController与一些UILabel,UIImageViews等我专门用于Peeking(见图片),当我弹出时我只是开始实际水平。

So in summary there is not many situations where you can use Peek and Pop in SpriteKit. 总而言之,在SpriteKit中可以使用Peek和Pop的情况并不多。 I also would not recommend to try to design your UI with UIKIt just so that you can use Peek and Pop. 我也不建议尝试使用UIKIt设计您的UI,以便您可以使用Peek和Pop。 Hopefully Apple will update the API one day so when can peek and pop SKNodes or SKScenes, that would be awesome. 希望Apple有一天会更新API,以便可以查看和播放SKNodes或SKScenes,这将是非常棒的。

Hope this helps 希望这可以帮助

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM