【问题标题】:Nevigating One ViewController to another on Spritekit在 Spritekit 上将一个 ViewController 导航到另一个 ViewController
【发布时间】:2015-03-03 08:34:11
【问题描述】:

我有一个带有两个视图控制器的 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");

}

【问题讨论】:

  • 您最好只使用滚动节点作为表格。
  • @sangony 怎么做,你能解释一下吗?
  • 为你添加了一些代码。

标签: ios objective-c sprite-kit


【解决方案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

【讨论】:

    【解决方案2】:

    如果您使用的是 xibs,您的方法应该可以工作,但在 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");
    
        }
    

    【讨论】:

    • 如果你使用的是tableview那么你应该使用tableviewdidselectforRowatIndexpath方法,请回复
    • 错误 1:重新定义 vc 然后我删除 UIStoryboard * 错误 2:原因:'Storyboard () 不包含带有标识符的视图控制器
    • 亲爱的,您必须创建一个要调用的视图控制器的实例。就像你说你有一个名为“MySecondViewController”的视图控制器,你创建了一个像 MySecondSecondViewController* 这样的实例第二件事你必须链接你的特定类并设置 StoryBoardID 否则它将无法正常工作
    • 当我点击好友按钮时它显示 FriendViewController 但是当我回到 GameViewController 时它什么也不做?
    • 因为我使用的是 push ,它应该转到上一个屏幕,或者你可以在你的后退按钮中弹出视图控制器,
    猜你喜欢
    • 1970-01-01
    • 2020-11-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-23
    • 1970-01-01
    相关资源
    最近更新 更多