SpriteKit中的水平滚动(与ViewController相对) [英] Horizontal scrolling in SpriteKit (vs. a viewcontroller)

查看:93
本文介绍了SpriteKit中的水平滚动(与ViewController相对)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对解决此问题的正确方法有些困惑:

I'm a little confused about the right way to approach this:

在我的游戏中,我允许玩家从5个区域中进行选择.一个区域只不过是屏幕上的矩形,该区域的完成百分比在该区域中.整个列表居中,看起来不错.

In my game, I allow the player to choose from 5 areas to play. An area is nothing more than a rectangle on a screen with the % of your completion in that area. The entire list is centered and looks good.

..最终会有更多区域,但我不希望它们位于多行中(我希望播放器滑动以滚动浏览它们).

..Eventually there will be more areas but I don't want them to be on multiple rows (I'd like the player to swipe to scroll through them).

迄今为止,我的游戏的所有 功能都使用SpriteKit进行了锥化(我只有一个视图控制器,除了展示所有我的SKScenes之外,我不使用它).

To date, all of the features of my game have been cone using SpriteKit (I only have one view controller, and I don't use it other than to present all my SKScenes in).

要实现上述所需功能,我现在需要引入带有UICollectionView的视图控制器吗?

To achieve the desired functionality above, do I need to now introduce a view controller with a UICollectionView?

我宁愿保持一致,并且只使用与SKView相关的代码,但是从本质上讲,我会通过允许滑动/滚动来重新创建UICollectionView吗?谁能推荐一种在SpriteKit中执行此操作的方法,或者您会说这通常是个坏主意?

I'd rather stay consistent and only use SKView-related code, but would I essentially be recreating a UICollectionView by allowing for swiping/scrolling? Could anyone recommend a way to do this in SpriteKit or would you say this is generally a bad idea?

非常感谢!

推荐答案

要纯粹在SpriteKit中执行此操作,您实质上是创建一个moveableNode并将所有菜单内容添加到该节点,然后再在touchesMoved中移动该节点.您将不得不跟踪其位置并以速度等方式摆弄.

To do this purely in SpriteKit you essentially create a moveableNode and add all your menu stuff to that node and than you move that node in touchesMoved. You would have to keep track of its position and fiddle with the speed etc.

https://codedump.io/share/etvt4SwQI6iR/1/如何创建一个垂直滚动菜单-spritekit

我认为这种菜单很少见,实际上最好在SpriteKit中使用UIKit,例如UIScrollViews或UICollectionViews.

I think this kind of menu is the rare occasion where it is actually better to use UIKit in SpriteKit, such as UIScrollViews or UICollectionViews.

尝试在SpriteKit中复制它们有些棘手,需要一些额外的代码,并且也不会给您带来很好的滚动/弹跳效果.

Trying to replicate them in SpriteKit is a bit tricky, requires some extra code and also doesnt give you the nice scrolling/bounce effect.

如果您要寻找的是,您可以在Spritekit中创建UIColletionView,您只需要子类化即可.我正在为自己的游戏使用一个作为关卡选择屏幕.

You can create a UIColletionView in Spritekit if thats what you are looking for, you just need to subclass. I am using one for my game I am working on as the level select screen.

创建一个新的swift文件

Create a new swift file

class CustomCollectionView: UICollectionView {

// MARK: - Init
init(frame: CGRect) {
    super.init(frame: frame)

    /// set up
    backgroundColor = UIColor.clearColor()
    #if os(iOS)
    pagingEnabled = true
    #endif
    self.frame = frame
    delegate = self
    dataSource = self
    indicatorStyle = .White
    scrollEnabled = true
    canCancelContentTouches = false

    registerClass... // register your cells
 }

// MARK: - Delegates
extension CustomCollectionView: UICollectionViewDataSource {

func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
    return 5
}

func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return 8
}

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
 .......
 }

然后在您的SKScenes中,您可以添加视图集合

Then in your SKScenes you can add the collection View

  weak var collectionView: CustomCollectionView!

  collectionView = CustomCollectionView(frame: view!.frame)
  view!.addSubview(collectionView)

您将必须阅读一些教程,以便熟悉UICollectionView,例如如何创建自定义单元格和常规setUp等.还必须确保在更改SKScenes时删除collectionView

You will have to read some tutorials so you can get comfortable with UICollectionView, like how to create custom cells and general setUp etc. You will also have to ensure that you remove the collectionView when changing SKScenes

 collectionView.removeFromSuperview()

这是您要问的吗?

这篇关于SpriteKit中的水平滚动(与ViewController相对)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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