在Spritekit上将一个ViewController推向另一个

时间:2015-03-01 07:09:05

标签: ios objective-c sprite-kit

我有一个带有两个视图控制器的SpriteKit项目。一个是默认的GameViewController,另一个是我添加了一个TableViewController。 我想在GameViewController和TableViewController之间切换。它没有切换视图控制器。

在GameScene.m中

GameViewController *vc =(GameViewController*)self.view.window.rootViewController;
    [vc moveToFriendsViewController];
    NSLog(@"vc called from gamescene");

在GameViewController.h中

@protocol ViewControllerDelegate <NSObject>

-(void)moveToFriendsViewController;

@end

@interface GameViewController : UIViewController<ViewControllerDelegate>
@end

在GameViewController.m中

-(void)moveToFriendsViewController{
   FriendsTableViewController *vc =[[FriendsTableViewController alloc] init];

// do any setup you need for myNewVC

[self.navigationController pushViewController:vc animated:YES];
NSLog(@"vc called from viewcontroller");

}

2 个答案:

答案 0 :(得分:1)

我创建了一个示例项目,让您通过使用滚动节点来理解我的意思。它本质上非常通用,您可以调整,修改和添加自己的值,代码等......

我将用户的y位置存储在touchesBegan方法中。然后,我在touchesMoved方法中检查y的任何更改,并相应地移动menuNode。但是,还有其他方法可以做到这一点。例如,您可以添加“向上”和“向下”按钮,并根据触摸的菜单移动菜单。不同的方法,但结果相同。

要查看是否选择了菜单项,我将用户从touchesBegan方法的y位置触摸与touchesEnded方法中的y位置进行比较。如果没有变化,用户没有向上或向下滑动,我NSLog所选节点。您可以在此处添加几个点的容差,以防用户稍微移动一下。

同样,它是通用代码,有很多方法可以做你想做的事情,但这应该会给你一些想法。

#import "GameScene.h"

@implementation GameScene {
    // declare ivars
    SKSpriteNode *menuNode;
    float yTouch;
}

-(void)didMoveToView:(SKView *)view {
    // add menu background
    menuNode = [SKSpriteNode spriteNodeWithColor:[SKColor darkGrayColor] size:CGSizeMake(200, 1000)];
    menuNode.name = @"menuNode";
    menuNode.position = CGPointMake(100, 800);
    menuNode.zPosition = 10;
    [self addChild:menuNode];

    float yPos = -450;

    for (int i = 0; i < 23; i++) {
        SKLabelNode *menuItem = [SKLabelNode labelNodeWithFontNamed:@"HelveticaNeue"];
        menuItem.name = [NSString stringWithFormat:@"menuItem-%i",i];
        menuItem.text = [NSString stringWithFormat:@"menuItem-%i",i];
        menuItem.fontSize = 20;
        menuItem.fontColor = [SKColor redColor];
        menuItem.horizontalAlignmentMode = SKLabelHorizontalAlignmentModeCenter;
        menuItem.verticalAlignmentMode = SKLabelVerticalAlignmentModeCenter;
        menuItem.position = CGPointMake(0, yPos);
        menuItem.zPosition = 25;
        [menuNode addChild:menuItem];

        yPos += 40;
    }
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

    for (UITouch *touch in touches) {
        CGPoint touchLocation = [touch locationInNode:self];

        // get starting y position of touch
        yTouch = touchLocation.y;
    }
}

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {

    for (UITouch *touch in touches) {
        CGPoint touchLocation = [touch locationInNode:self];

        // check for changes in touched y position and menuNode limits
        if((touchLocation.y > yTouch) && (menuNode.position.y < 800)) {
            menuNode.position = CGPointMake(menuNode.position.x, menuNode.position.y+15);
        }
        if((touchLocation.y < yTouch) && (menuNode.position.y > 200))   {
            menuNode.position = CGPointMake(menuNode.position.x, menuNode.position.y-15);
        }
    }
}

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {

    UITouch *touch = [touches anyObject];
    CGPoint touchLocation = [touch locationInNode:self.scene];
    SKNode *node = [self nodeAtPoint:touchLocation];

    // if current touch position y is same as when touches began
    if(touchLocation.y == yTouch) {
        NSLog(@"%@",node);
    }
}

-(void)update:(CFTimeInterval)currentTime {

}

@end

答案 1 :(得分:0)

如果您使用的是xib,那么您的方法应该可行,但在Storyboard中,以下方法可以正常工作。

 -(void)moveToFriendsViewController{
       FriendsTableViewController *vc =[[FriendsTableViewController alloc] init];

     UIStoryboard * storyboard = [UIStoryboard storyboardWithName:@"Main" bundle: nil];
            UIViewController * vc = [storyboard instantiateViewControllerWithIdentifier:@"FriendsTableViewController"];
            [self.navigationController pushViewController:vc animated:YES];
    NSLog(@"vc called from viewcontroller");

    }