在SpriteKit中缩放和滚动

Jus*_*ore 13 pan image-zoom ios7 sprite-kit

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

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


编辑:

我对你的答案感到有点困惑,但这就是我的所作所为:

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

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

在我的场景.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;
}
Run Code Online (Sandbox Code Playgroud)

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

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

bob*_*off 15

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

  1. 将UIScrollViewDelegate方法添加到ViewController并将滚动视图委托设置为ViewController
  2. 将PanGestureRecognizer从UIScrollView添加/移动到SKView
  3. 使用scrollViewDidScroll callback-method控制用户滚动时所需的节点

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

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

  • 我走到我们的办公室,又拿起另一个.这是一个示例项目,其中scrollview已设置并正常工作:https://github.com/bobmoff/ScrollKit (5认同)

Pet*_*erK 1

您可以从 Ray W 教程SpriteKit 教程开始,了解如何拖放,其中包括您要求的一些内容,包括手势识别器。

这很好:Apple 手势识别器参考

Ray W 的另一个文章:uigesturerecognizer-tutorial-in-ios-5-pinches-pans-and-more

上面的内容是很好的开始。