在SKSpriteKit中创建可滚动表的正确方法是什么? [英] What is the right way to create scrollable table in 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?
推荐答案
我使用的方法是:
-
在场景的didMoveToView:方法中,设置UIScrollView实例。设置所需的所有属性,并确保还设置其委托,设置hidden = YES并将其panGestureREcognizer添加到视图手势识别器集合。
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.
在滚动视图中委托,实现scrollViewDidScroll:。在这里你可以使用滚动视图的contentOffset点来调整节点的位置。
In the scrollview delegate, implement scrollViewDidScroll:. Here you can use the scroll view's contentOffset point to adjust a node's position.
我实现了一个offsetable节点,我用作我需要滚动的东西的根节点。它所做的只是实现一个contentOffset属性 - 你可以在这里看到它: CATOffsetNode要点。
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.
编辑:注意滚动视图内容偏移Y方向:
UIView和SpriteKit Y坐标向相反方向移动(UIView的原点是左上角, Y向下增加),我更喜欢坚持一个坐标系。所以我创建了一个自定义的scrollview类,它在初始化器中翻转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子类化为实现这一点)但没有这个你必须手动翻转scrollview委托中的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屋!