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

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

问题描述

我是SpriteKit的新手。我需要通过UIImageView或SpriteNode显示图像(它是游戏的背景图像)。但是,我需要用户能够放大背景并平移。我在不使用SpriteKit的情况下轻松完成了这项工作,但无法弄清楚如何让节点接受缩放和平移。如果我在故事板中使用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年的WWDC视频中了解到有关将UIKit与OpenGL混合的信息。

Here is my sollution 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 the often the delegate method is called, as it somethimes feel a little laggy. If anyone manages to solve this issue, I would 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天全站免登陆