在 SpriteKit 中缩放和滚动 [英] Zooming and scrolling in SpriteKit

查看:15
本文介绍了在 SpriteKit 中缩放和滚动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 SpriteKit 的新手.我需要通过 UIImageView 或 SpriteNode 显示图像(它是游戏的背景图像).但是,我需要用户能够放大背景并进行平移.我在不使用 SpriteKit 的情况下轻松完成了这项工作,但无法弄清楚如何让节点接受缩放和平移.如果我在 storyboard 中使用 UIScrollView,我的场景中添加的所有精灵都不会成为孩子,也不会缩放或平移.

I am new to SpriteKit. I need to display an image via UIImageView OR SpriteNode (it is a background image for a game). However, I need the user to be able to zoom in to the background and pan around. I accomplished this easily without using SpriteKit but cannot figure out how to make a node accept zoom and pan. If I use UIScrollView in the storyboard, all the sprites added in my scene don't become children and will not zoom or pan.

有没有可能,你能指出我正确的方向吗?

Is it possible and can you point me in the right direction?

我对你的回答有点困惑,但我是这样做的:

I'm a little confused by your answer, but here's what I did:

在我的根视图控制器 .m 中:

- (void)viewDidLoad
{
    [super viewDidLoad];
    SKView * showDesigner = (SKView *)self.view;
    SKScene * scene = [iMarchMyScene sceneWithSize:showDesigner.bounds.size];
    scene.scaleMode = SKSceneScaleModeAspectFill;
    [showDesigner presentScene:scene];
}

在我的场景中.m:(您的步骤已注释)

-(id)initWithSize:(CGSize)size {
    if (self = [super initWithSize:size]) {

        //create SKView (step 1)
        SKView *skview_field = [[SKView alloc]initWithFrame:(CGRectMake(0, 0, size.width, size.height))];

        //create UIScrollView with same dimensions as your SpriteKit view (step 2)
        UIScrollView* uiscrollview_field = [[UIScrollView alloc]initWithFrame:skview_field.frame];

        //initialize the field (field is the game's background node)
        SKSpriteNode *field = [SKSpriteNode spriteNodeWithImageNamed:@"Field"];
        field.position = CGPointMake(CGRectGetMidX(self.frame), (CGRectGetMidY(self.frame)));
        field.size = CGSizeMake(self.size.width, self.size.height / 3);

        //create a UIView (called scrollViewContent) with dimensions of background node (step 3)
        UIView* scrollViewContent = [[UIView alloc]initWithFrame:field.frame];

        //set UIScrollView contentSize with same dimensions as your scrollViewContent and add a scrollContentview as your UISCrollView subview (step 4)
        [uiscrollview_field setContentSize:(CGSizeMake(scrollViewContent.frame.size.width, scrollViewContent.frame.size.height))];
        scrollViewContent.frame = field.frame;
        [uiscrollview_field addSubview:scrollViewContent];



        //[self addChild:field]; -- my original code
        //.....does a bunch of other inits

        return self;
}

这就是我迷路的地方:第 5 步:现在,在根视图中添加 SKView 和 UIScrollView 到 SKView 之上.

And here's where I get lost: Step 5: Now, to your root view add your SKView, and UIScrollView on top of SKView.

如果我理解正确,我需要转到 myRootViewController.m 并在 viewDidLoad 方法中,将 SKView 和 UIScrollView(在 myScene.m 中初始化)作为子视图添加到此处在 viewDidLoad 方法中初始化的 SKView?我不知道如何做那部分.

If I understood you correctly, I need to go to myRootViewController.m and in the viewDidLoad method, add the SKView and UIScrollView (which are initialized in myScene.m) as subviews to SKView initialized here in the viewDidLoad method? I don't see how to do that part.

推荐答案

这是我对滚动问题的解决方案.它围绕窃取" UIScrollView 的行为展开.我从 2012 年关于将 UIKit 与 OpenGL 混合的 WWDC 视频中了解到这一点.

Here is my solution to the scrolling problem. It revolves around "stealing" the behaviour from the UIScrollView. I learned this from a WWDC Video from 2012 about mixing UIKit with OpenGL.

  1. 将 UIScrollViewDelegate 方法添加到您的 ViewController 并将滚动视图委托设置为 ViewController
  2. 将 PanGestureRecognizer 从 UIScrollView 添加/移动到 SKView
  3. 当用户滚动时,使用 scrollViewDidScroll 回调方法来控制你想要的任何节点

示例项目:https://github.com/bobmoff/ScrollKit

就像我在上面的评论中提到的那样,我在运行该应用程序时大约有 30% 的时间遇到​​了一些微小的延迟.FPS 仍然是 60,但似乎有一些轻微的冲突或委托方法的调用频率,因为它有时会感觉有点滞后.如果有人设法解决这个问题,我很想听听.似乎只有当我手指向下时,减速发生时才不会滞后.

Like I mentioned in a comment above, I am experiencing some tiny lag about 30% of the times I am running the app. The FPS is still 60 but there seem to be some slight conflict or something with how often the delegate method is called, as it sometimes feel a little laggy. If anyone manages to solve this issue, I would love to hear about it. It seems to be only when I am holding my finger down, it never lags when the deceleration is happening.

这篇关于在 SpriteKit 中缩放和滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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