How do I add a scroll view to my gameScene.swift,

2019-07-17 09:53发布

问题:

So I've created a 3x10 grid of sprites, that go off the bottom edge of the screen. I've added a UIScrollView, and the scroll indicators appear, but no movement actually occurs. Here's the code:

import SpriteKit
var buyButton = SKSpriteNode()
var pic = SKTexture(imageNamed: "Button")
class GameScene: SKScene, UIScrollViewDelegate {
    var scrollView = UIScrollView()
    var scrollFrame = CGRect!()
    override func didMoveToView(view: SKView) {
        /* Setup your scene here */
        print(self.frame.size.width)
        scrollView = UIScrollView(frame: CGRect(origin: CGPointMake(0,0), size: CGSizeMake(self.frame.size.width, self.frame.size.height)))
        scrollView.contentSize = CGSizeMake(self.frame.size.width, self.frame.size.height*2)
        scrollView.bounces = false
        scrollView.pagingEnabled = true
        scrollView.showsHorizontalScrollIndicator = false
        scrollView.showsVerticalScrollIndicator = true
        scrollView.backgroundColor = UIColor(red: 1, green: 1, blue: 1, alpha: 0)

        var width = self.frame.size.width
        var height = self.frame.size.height
        for i in 1...10{
            for x in 1...3{
                buyButton = SKSpriteNode(texture: pic, size: CGSizeMake(width*13/CGFloat(45), 100))//(rectOfSize: CGSizeMake(width*13/CGFloat(45), 100))
                buyButton.position = CGPointMake(CGFloat(29.0)*CGFloat(x)*width/CGFloat(90.0)-width*CGFloat(13)/CGFloat(90), CGFloat(i)*0.15*height-width*CGFloat(13)/CGFloat(45)*9.0/16.0)
                print("\(x)\(i)")
                buyButton.size = CGSizeMake(width*CGFloat(13)/CGFloat(45), width*CGFloat(13)/CGFloat(45)*9.0/16.0)
                buyButton.name = "\(x)\(i)"
                self.addChild(buyButton)
                var label = SKLabelNode(text: "hi")
                label.fontSize = 40
                label.name = "Label\(x)\(i)"
                buyButton.addChild(label)
                print(CGFloat(29.0)*CGFloat(x)*width/CGFloat(90.0)-width*CGFloat(13)/CGFloat(90))
                label.fontColor = UIColor.blackColor()
                label.horizontalAlignmentMode = .Center
                label.verticalAlignmentMode = .Center
            }
         }
        self.scene?.view?.addSubview(scrollView)
    }

    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
       /* Called when a touch begins */

    }

    override func update(currentTime: CFTimeInterval) {
        /* Called before each frame is rendered */
    }
}

so I'm not entirely sure what I've done wrong.

I realise that this should not be in the GameScene.swift file, and should be separate, but this was just a testing project.

Thanks :-)

回答1:

You are running into a problem I struggled with for a while. The fundamental problem is that you cannot add SKNodes to a UIKit view, and you cannot add UIKit views to SKNodes. Views are not nodes, and nodes are not views, sadly this will not work. While you can add UIKit elements to the SKScene's view property, that is as far as you can take it. I struggled with this for quite a while before I finally got it.

I still mix UIKit and SpriteKit, btw, but I do so in a limited fashion and it never relies on adding nodes to views or vice versa.