SpriteKit,Swift 2.0 - ScrollView通过按钮从场景启动中丢失

时间:2016-01-02 09:35:37

标签: ios swift scrollview scene

我最近在我的GameViewController中实现了一个scrollView,它的效果非常好,但我看过的教程中只有一个场景是启动场景(GameScene),而不是一个单独的场景,我将会是打电话"菜单"所以我设法让它启动菜单场景,而不是常规" GameScene"但是当我使用我在GameScene中实现的按钮从GameScene进入菜单场景时,滚动视图不起作用,但它确实显示了图片,但我无法滚动它们。

我的问题是当我使用按钮(在GameScene中)转到菜单场景时,如何让scrollView工作?

这是按钮(在GameScene中)

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {

    let touch: UITouch = touches.first!
    let location: CGPoint = touch.locationInNode(self)
    let node: SKNode = self.nodeAtPoint(location)


    if (node == menubutton) {

        let MenuScene = Menu(size: self.size, viewController: viewController)
        let transition = SKTransition.flipVerticalWithDuration(0.5)
        MenuScene.scaleMode = SKSceneScaleMode.AspectFill
        self.scene!.view?.presentScene(MenuScene, transition: transition) 

}

这是我的菜单场景:

import Foundation
import SpriteKit


let kMargin: CGFloat = 40

var backButton = SKSpriteNode()
var selectButton = SKSpriteNode()


class Menu: SKScene {

    let world2 = SKSpriteNode()

private var imageSize = CGSize.zero

private weak var viewController: GameViewController?


init(size: CGSize, viewController: GameViewController?) {
    self.viewController = viewController
    super.init(size: size)
}

required init?(coder aDecoder: NSCoder) {
    assert(false, "Use init(size:viewController:)")

    super.init(coder: aDecoder)
}


override func didMoveToView(view: SKView) {
    physicsWorld.gravity = CGVector.zero

    imageSize = SKSpriteNode(imageNamed: "card_level01").size

    let initialMargin = size.width/2
    let marginPerImage = kMargin + imageSize.width

    world2.size = CGSize(width: initialMargin*2 + (marginPerImage * 7), height: size.height)
    addChild(world2)

    for i in 1...8 {
        let sprite = SKSpriteNode(imageNamed: String(format: "card_level%02d", i))
        sprite.position = CGPoint(x: initialMargin + (marginPerImage * (CGFloat(i) - 1)), y: size.height / 2)
        world2.addChild(sprite)

    }
}

override func update(currentTime: NSTimeInterval) {
    viewController?.applyScrollViewToSpriteKitMapping()
}

这是我的GameViewController

import UIKit
import SpriteKit

class GameViewController: UIViewController {

var scrollView: UIScrollView!
var contentView: UIView!

override func viewWillLayoutSubviews() {
    super.viewWillLayoutSubviews()

    let skView = view as! SKView

    if (skView.scene === Menu.self) {

        skView.showsFPS = true
        skView.showsNodeCount = true

        skView.ignoresSiblingOrder = true

        let scene = Menu(size: skView.bounds.size, viewController: self)
        scene.scaleMode = .AspectFill

        skView.presentScene(scene)

        scrollView = UIScrollView(frame: self.view.bounds)
        scrollView.delegate = self
        scrollView.contentSize = scene.world2.frame.size
        view.addSubview(scrollView)

        contentView = UIView(frame: CGRect(origin: CGPoint.zero, size: scene.world2.size))
        contentView.backgroundColor = UIColor.greenColor().colorWithAlphaComponent(0.2)
        scrollView.addSubview(contentView)

        applyScrollViewToSpriteKitMapping()

    }
}

func applyScrollViewToSpriteKitMapping() {

    let origin = contentView.frame.origin

    let skPosition = CGPoint(x: -scrollView.contentOffset.x + origin.x, y: -scrollView.contentSize.height + CGRectGetHeight(view.bounds) + scrollView.contentOffset.y - origin.y)


    let skView = view as! SKView
    if let scene = skView.scene as? Menu {
        scene.world2.position = skPosition

    }

}

override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
    if UIDevice.currentDevice().userInterfaceIdiom == .Phone {
        return UIInterfaceOrientationMask.Portrait
    } else {
        return UIInterfaceOrientationMask.All
    }
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Release any cached data, images, etc that aren't in use.
}

override func shouldAutorotate() -> Bool {
    return true
}

override func prefersStatusBarHidden() -> Bool {
    return true
 }

}

extension GameViewController: UIScrollViewDelegate {


}

1 个答案:

答案 0 :(得分:0)

我最近帮助其他成员提出了类似问题,请查看它可能对您有所帮助

How to create a vertical scrolling menu in spritekit?

以我的方式,我是子类化滚动视图,因此可以直接将其添加到SKScenes而不是视图控制器。

如果你想在不超过1个SKScene上使用scrollView,而不需要重复代码而不需要继承你的SKScnenes

 class BaseScene: SKScene ...
 // Add scroll view 

 class Menu: BaseScene...
 class GameScene: BaseScene ...

作为旁注,你不应该在你的SKScenes中真正引用你的viewController,他们不应该彼此了解。