SpriteKit, Swift - ScrollView Not Showing Up

2019-09-12 10:46发布

Ok, so I implemented a scrollview by using this method:

How to create a vertical scrolling menu in spritekit?

Now when I launch the scene it doesn't display anything except a black/ grey background

Here is the code I have used in the menu scene:

import Foundation
import SpriteKit
import UIKit

//let kMargin: CGFloat = 40

//var backButton = SKSpriteNode()
//var selectButton = SKSpriteNode()
var moveableNode = SKNode()
var scrollView: CustomScrollView!
private var imageSize = CGSize.zero

var sprite = SKSpriteNode()

class Menu: SKScene {

func didMoveToView() {

    addChild(moveableNode)

    scrollView = CustomScrollView(frame: CGRect(x: 0, y: 0, width: self.frame.size.width, height: self.frame.size.height), scene: self, moveableNode: moveableNode)
    scrollView.contentSize = CGSizeMake(self.frame.size.width, self.frame.size.height * 2)
    view!.addSubview(scrollView)

    sprite =  SKSpriteNode(color: SKColor.redColor(), size: CGSize(width: 50, height: 44))
    sprite.position.y = CGRectGetMidY(self.frame) + self.frame.size.height
    moveableNode.addChild(sprite)

  }

}

It might be really obvious but I can't seem to see it.

2条回答
趁早两清
2楼-- · 2019-09-12 11:21

Try this for the sprite, I made a mistake in the other question, it should be - self.frame.size.height, not +.

 sprite = SKSpriteNode(color: SKColor.redColor(), size: CGSize(width: 50, height: 44))
 sprite.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame) - self.frame.size.height)
 moveableNode.addChild(sprite)

Check my gitHub project

https://github.com/crashoverride777/Swift-2-SpriteKit-UIScrollView-Helper

查看更多
等我变得足够好
3楼-- · 2019-09-12 11:28

The sprite position is not right, not sure where exactly you want it. I do the following and the sprite show up on the view.

sprite.position = CGPoint(x: 200, y: 200)

or

// To make it a center of the view
sprite.position = CGPointMake(self.frame.size.width / 2, self.frame.size.height / 2)
查看更多
登录 后发表回答