在 SKSpriteKit 中创建可滚动表的正确方法是什么? [英] What is the right way to create scrollable table in SKSpriteKit?

查看:12
本文介绍了在 SKSpriteKit 中创建可滚动表的正确方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一种显示排行榜的方法 - 最佳用户分数(本地用户、本地分数).

I need a way to display a leaderboard - best user scores (local user, local scores).

我是否需要为这种事情创建我的自己的"节点,或者使用透明背景的 SKView 上方的 UITableView 更好?

Do I need to create my "own" node for this kind of thing, or its better to use UITableView above SKView with transparent background?

推荐答案

我使用的一种方法是:

  1. 在场景的 didMoveToView: 方法中,设置一个 UIScrollView 实例.设置您需要的所有属性,并确保还设置其委托,设置 hidden=YES 并将其 panGestureREcognizer 添加到视图手势识别器集合中.

  1. In the scene's didMoveToView: method, set up a UIScrollView instance. Set all the properties you need, and make sure to also set its delegate, set hidden=YES and add its panGestureREcognizer to the view gesture recognizer collection.

在 willMoveFromView 中:将其拆下并清理干净.

In willMoveFromView: tear it down and clean it up.

在scrollview委托中,实现scrollViewDidScroll:.这里可以使用滚动视图的 contentOffset 点来调整节点的位置.

In the scrollview delegate, implement scrollViewDidScroll:. Here you can use the scroll view's contentOffset point to adjust a node's position.

我已经实现了一个可偏移节点,我将它用作我需要滚动的东西的根节点.它所做的只是实现了一个 contentOffset 属性——你可以在这里看到它:CATOffsetNode Gist.

I have implemented an offsetable node that I use as a root node for things I need to scroll. All it does is implement a contentOffset property - you can see it here: CATOffsetNode Gist.

所以在scrollViewDidScroll:我需要做的就是:

So in scrollViewDidScroll: all I need to do is:

-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    // I don't need to flip scrollView.contentOffset.Y because my scrollview
    // Y coordinate is flipped. See below.
    self->_gameListNode.contentOffset = scrollView.contentOffset;
}    

然后我将所有需要滚动的项目添加为 _gameListNode 的子项.

Then I add all the items I need to scroll as children for _gameListNode.

结果是一个可滚动的游戏列表,提供原生滚动视图体验,包括弹力和滑动加速等.

The result is a scrollable game list, which offers a native scrollview experience, including bounciness and swipe acceleration etc.

您可以查看以下示例实现:GitHub 上的 ScrollKit.

Here is an example implementation you can check out: ScrollKit on GitHub.

关于scrollview contentOffset Y 方向的注释:

UIView 和 SpriteKit 的 Y 坐标方向相反(UIView 的原点在左上角,Y 向下增加),我更喜欢坚持一个坐标系.所以我制作了一个自定义滚动视图类,它在初始化程序中翻转 Y 方向.例如:

UIView and SpriteKit Y coordinates go in opposite directions (UIView's origin is top left, and Y increases downwards), and I prefer to stick to one coordinate system. So I've made a custom scrollview class that flips the Y direction in the initializer. E.g.:

-(id)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame])
    {
        CGAffineTransform verticalFlip = CGAffineTransformMakeScale(1,-1);
        self.transform = verticalFlip;
    }
    return self;
}

这不是必需的(您甚至不需要继承 UIScrollView 来实现这一点),但如果没有这个,您将不得不在滚动视图委托中手动翻转 contentOffset.Y.

This is not necessary (and you don't even need to subclass UIScrollView to achieve this) but without this you would have to manually flip contentOffset.Y in the scrollview delegate.

这篇关于在 SKSpriteKit 中创建可滚动表的正确方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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