- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个类似的问题,就像这个帖子中的人 How to get from SKScene to UIViewController? 。首先我应该说我完全是一个初学者,这对我来说都是新的。
我的主菜单(我的应用程序加载时的第一个屏幕)在这里:
class MenuVC: UIViewController {
... }
我的应用程序(游戏)的交互可以在这里进行:
//
// GameScene.swift
// Doodle Pong
//
// Created by Daniel Kloe on 28.02.17.
// Copyright © 2017 Daniel Kloe. All rights reserved.
//
import SpriteKit
import GameplayKit
import Foundation
import UIKit
class GameScene: SKScene {
//let storyboard = UIStoryboard(name: "Main", bundle: nil)
//let MenuVC = storyboard.instantiateViewController(withIdentifier: "MenuVC")
//MenuVC.view.frame = (self.view?.frame)!
//MenuVC.view.layoutIfNeeded()
//UIView.transition(with: self.view!, duration: 0.3, options: .transitionFlipFromRight, animations:
//{
//self.view?.window?.rootViewController = vc
//}, completion: { completed in
//})
var ball = SKSpriteNode()
var enemy = SKSpriteNode()
var main = SKSpriteNode()
var score = [Int] ()
var topLbl = SKLabelNode()
var btmLbl = SKLabelNode()
override func didMove(to view: SKView) {
topLbl = self.childNode(withName: "topLabel") as! SKLabelNode
btmLbl = self.childNode(withName: "btmLabel") as! SKLabelNode
ball = self.childNode(withName: "ball") as! SKSpriteNode
enemy = self.childNode(withName: "enemy") as! SKSpriteNode
main = self.childNode(withName: "main") as! SKSpriteNode
let border = SKPhysicsBody(edgeLoopFrom: self.frame)
border.friction = 0
border.restitution = 1
self.physicsBody = border
startGame()
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in touches{
// let gameSceneTemp = GameScene(fileNamed: "GameScene")
// self.scene?.view?.presentScene(gameSceneTemp, transition: SKTransition.doorsCloseHorizontal(withDuration: 0.01))
let location = touch.location(in: self) //die location wird mit dem Berühren auf dem Bildschirm beschrieben
if currentGameType == .TwoPlayer{
if location.y > 0{
enemy.run(SKAction.moveTo(x: location.x, duration: 0.01)) //Der "main Balken" wird zu den location bewegt
}
if location.y < 0{
main.run(SKAction.moveTo(x: location.x, duration: 0.01)) //Der "main Balken" wird zu den location bewegt
}
}
else{
main.run(SKAction.moveTo(x: location.x, duration: 0.01)) //Der "main Balken" wird zu den location bewegt
}
}
}
func startGame(){
score = [0,0]
topLbl.text = "\(score[1])"
btmLbl.text = "\(score[0])"
//let delay = SKAction.wait(forDuration: 2000)
//self.run(delay) //evtl. Wartezeit 2s, funktioniert noch nicht richtig
ball.physicsBody?.applyImpulse(CGVector(dx: 20, dy: 20))
}
func addScore(playerWhoWon : SKSpriteNode){
ball.position = CGPoint(x: 0, y: 0)
ball.physicsBody?.velocity = CGVector(dx: 0, dy: 0)
if playerWhoWon == enemy{
score[1] += 1
ball.physicsBody?.applyImpulse(CGVector(dx: 32, dy: 27))
}
else if playerWhoWon == main{
score[0] += 1
ball.physicsBody?.applyImpulse(CGVector(dx: -32, dy: -27))
}
topLbl.text = "\(score[1])"
btmLbl.text = "\(score[0])"
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in touches{
let location = touch.location(in: self) //die location wird mit dem Berühren auf dem Bildschirm beschrieben
if currentGameType == .TwoPlayer{
if location.y > 0{
enemy.run(SKAction.moveTo(x: location.x, duration: 0.01)) //Der "main Balken" wird zu den location bewegt
}
if location.y < 0{
main.run(SKAction.moveTo(x: location.x, duration: 0.01)) //Der "main Balken" wird zu den location bewegt
}
}
else{
self.topLbl.zRotation = CGFloat(2 * M_PI)
// UIView.animate(withDuration: 0, animations: ({
// self.topLbl.zRotation = CGFloat(2 * M_PI) //CGFloat(.pi / 4.0)
// }))
main.run(SKAction.moveTo(x: location.x, duration: 0.01)) //Der "main Balken" wird zu den location bewegt
}
}
}
override func update(_ currentTime: TimeInterval) {
switch currentGameType{
case .Easy:
enemy.run(SKAction.moveTo(x: ball.position.x, duration: 1.0))
break
case .Medium:
enemy.run(SKAction.moveTo(x: ball.position.x, duration: 0.5))
break
case .Extreme:
enemy.run(SKAction.moveTo(x: ball.position.x, duration: 0.08))
break
case .TwoPlayer:
break
}
//Called before each frame is rendered
if ball.position.y <= main.position.y - 20{
addScore(playerWhoWon: enemy)
}
else if ball.position.y >= enemy.position.y + 20{
addScore(playerWhoWon: main)
}
}
@IBAction func backToMainMenu(_ sender: Any) {
//self.view?.window?.rootViewController
self.view?.window!.rootViewController?.dismiss(animated: false, completion: nil)
}
}
在我的函数“backToMainMenu”中,我尝试返回到 MenuVC,但收到警告“‘UIViewController’类型的表达式?”未使用”,如果我在模拟器中尝试它,模拟器将会崩溃。
我感谢各种帮助:)。
最佳答案
尝试:
self.view.window!.rootViewController?.dismiss(动画: false, 完成: nil)
这应该关闭 rootViewController 之上的所有 View
编辑:
这可能对您有用,因为我们定义了 ViewController,请确保在 Storyboard 中您的 MenuVC
有一个标识符:
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "MenuVC") // Make sure is the same identifier as in the storyboard.
vc.view.frame = (self.view?.frame)!
vc.view.layoutIfNeeded()
UIView.transition(with: self.view!, duration: 0.3, options: .transitionFlipFromRight, animations:
{
self.view?.window?.rootViewController = vc
}, completion: { completed in
})
}
编辑:
这似乎是最好的方法,是将 SKScene 类托管在另一个 View Controller 中。我建议在 SKScene 所在的位置创建另一个 ViewController,例如 SKViewController
。将您的 MenuVC
嵌入 navigationController
中,在 StoryBoard 中单击它,单击编辑器(从顶部菜单中)> 嵌入 > 导航 Controller 。编辑 MenuVC,这样您只需转到 SKViewController,然后打开场景,而不是打开场景。要做到这一点,只需在 Storyboard 中从 MenuVC
创建一个 segue,例如使用标识符“MenuToSKScene”,当您想要打开场景时,从 MenuVC 只需执行以下操作:
self.performsegue(with identifier: "MenuToSKScene", sender: self)
然后执行您当前在 SKViewController
的 MenuVC
中执行的操作来打开场景。
这会将您发送到 SKViewController,其中包含您的 SKScene 类。然后当你想返回菜单时,你所要做的就是:
self.dismiss(animated: true, completion: nil)
或者使用标识符“SKSceneToMenu”创建从 SKViewController 到 MenuVC 的 Segue 并调用:
self.performsegue(with identifier: "SKSceneToMenu", sender: self)
所以本质上,不要从 MenuVC
打开 SKScene
,而是有一个单独的 viewController SKViewController
,当您想要打开时打开它您的场景,以便您可以轻松跳回菜单。
关于ios - 如何从 SKScene 返回到 UIViewController (MenuVC)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55107785/
我希望能够在代码中布局我的 View Controller ,但可以看到界面构建器中显示的布局。 我知道我可以创建一个 UIView 子类,使其 IBDesignable,并将其分配给 View Co
我有一个父 UIViewController(MainVC)。从那里我有 2 个 segue 到 2 个 UIViewControllers:FirstVC(标识符:goFirstVC)和 Secon
我有一个导航 Controller 。第一个viewcontroller 是FirstViewController 的一种。当我在 FirstViewController 中点击一个按钮时,它会将第二
我有一个自定义的动画 UIViewController 过渡,似乎 iOS 中有一个错误会破坏横向布局。在主要动画方法中,我得到了横向和纵向 View 的混合。 (纵向 View 都是纵向的,所以没问
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 9
我有一个 UIViewController,里面有 UITabBar。我正在尝试模仿 UITabBarController。 My question is how do I set or a UIVi
是否有任何方便的方法来确定是否正在从处于后台模式的应用加载 View ? 在 3.X 中,我将依赖 viewDidLoad 进行一些初始化等操作,但是对于 4.X 而言情况并非如此,因为您不能依赖调用
我将主视图 Controller 作为带有 UICollectionView 的 View Controller ,并且填充了照片。 一旦一个单元格被点击,一个新的 View Controller 被
我有一个简单的 UIViewController(单点触控),在 UI 上有一个按钮。现在,我的用例很简单 - 我想在单击按钮时重新加载 UIViewController。在某些线程上,我读到我只需要
我在它上面有一个 UIViewController 1 UIButton(添加为 subview ),在我按下 Button(见下面的图 1)之后,在它上面添加了另一个 UIViewControlle
尝试为 iPad 制作基于 Storyboard的应用程序。在其中,我需要从开始屏幕 (UIViewController) 过渡到主屏幕 (UISplitViewController),然后再到全屏
我想调整 UIViewController 的大小在 Storyboard中,所以我可以使用它的 UIViewController作为弹出 View 。 在大多数网站上,我可以读到这些操作是: 拖一个
我正在尝试从 XIB 或 Storyboard加载 ViewModel 绑定(bind)的 ViewController(使用 MvvmCross 5.0.6)。我的应用程序的逻辑是,该 View C
我正在构建一个锻炼应用程序。我的 TableViewController 中有一系列练习,每个单元格显示不同的练习。单元格转到 UIViewController。在 UIViewController
我正在尝试从 XIB 或 Storyboard加载 ViewModel 绑定(bind)的 ViewController(使用 MvvmCross 5.0.6)。我的应用程序的逻辑是,该 View C
我有一个 UIViewController,它有一个模式窗口,我想在整个界面上展示它,包括 UITabBar。 我的应用程序层次结构是这样的: UITabBarController (A) ->
我想显示另一个 ViewController 的缩小“预览”在另一个 ViewController .这可能吗?不需要交互,因此它基本上可以是屏幕截图,但按比例缩小。我可以通过手动截屏并将其保存为图像
我正在开发一个应用程序,其中 UIViewController ( firstViewController ) 包含一些 UILabels , 一个 UIButton , 和 UIView ( sub
我的标签栏中有五个标签栏项目。第五项是弹出ViewController。因此,当我单击该按钮时,当前 Controller 中将显示一个弹出窗口。我使用 UIViewController 作为子类来实
我正在开发 iOS 应用程序。 我的应用程序有 6 个按钮,每个按钮都有每个标签栏控件。 so Main screen is [btn1] [btn2] [btn3] [btn4] [btn5] [b
我是一名优秀的程序员,十分优秀!