如何在我的gameScene.swift中添加滚动视图,以滚动显示一系列精灵? [英] How do I add a scroll view to my gameScene.swift, that will scroll through a series of sprites?

查看:62
本文介绍了如何在我的gameScene.swift中添加滚动视图,以滚动显示一系列精灵?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我创建了一个3x10的精灵网格,它们位于屏幕的底部边缘之外.我添加了一个UIScrollView,并且出现了滚动指示符,但实际上没有发生任何移动. 这是代码:

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.

我意识到这不应该在GameScene.swift文件中,并且应该是分开的,但这只是一个测试项目.

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

谢谢:-)

推荐答案

您遇到了我苦苦挣扎了一段时间的问题.根本问题是您不能将SKNode添加到UIKit视图,也不能将UIKit视图添加到SKNode.视图不是节点,节点也不是视图,可惜这是行不通的.您可以将UIKit元素添加到SKScene的view属性中,但这是可以做到的.我在这个问题上苦苦挣扎了好一阵子,才终于得到它.

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和SpriteKit,但是我这样做的方式很有限,它从不依赖于向视图添加节点,反之亦然.

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.

这篇关于如何在我的gameScene.swift中添加滚动视图,以滚动显示一系列精灵?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆