如何向gameScene中添加滚动视图。斯威夫特,那将会通过一系列的精灵?

时间:2022-12-05 22:04:03

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:

我已经创建了一个3x10的精灵网格,它离开屏幕的底部边缘。我添加了一个UIScrollView,并显示了滚动指示器,但实际上没有发生任何移动。这是代码:

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.

我意识到这不应该出现在游戏中。swift文件,应该是分开的,但这只是一个测试项目。

Thanks :-)

谢谢:-)

1 个解决方案

#1


2  

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.

你遇到了一个问题,我为此挣扎了一会儿。最基本的问题是你不能向UIKit视图添加sknode,你也不能向sknode添加UIKit视图。视图不是节点,节点不是视图,不幸的是,这是行不通的。虽然您可以向SKScene的view属性添加UIKit元素,但这是您所能接受的。我挣扎了好长时间才最终得到它。

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.

顺便说一句,我仍然混合了UIKit和SpriteKit,但是我以一种有限的方式这样做,它从不依赖于向视图添加节点,反之亦然。

#1


2  

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.

你遇到了一个问题,我为此挣扎了一会儿。最基本的问题是你不能向UIKit视图添加sknode,你也不能向sknode添加UIKit视图。视图不是节点,节点不是视图,不幸的是,这是行不通的。虽然您可以向SKScene的view属性添加UIKit元素,但这是您所能接受的。我挣扎了好长时间才最终得到它。

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.

顺便说一句,我仍然混合了UIKit和SpriteKit,但是我以一种有限的方式这样做,它从不依赖于向视图添加节点,反之亦然。