我有一个带有两个 View Controller 的 SpriteKit 项目。一个是默认的 GameViewController,另一个是我添加的 TableViewController。 我想在 GameViewController 和 TableViewController 之间切换。它没有切换 View Controller 。
在 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");
}
最佳答案
我创建了一个示例项目,以便您了解我所说的使用滚动节点的含义。它本质上非常通用,您可以调整、修改和添加您自己的值、代码等...
我将用户的 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
关于ios - 在 Spritekit 上将一个 ViewController 导航到另一个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28791063/