如何使用社交框架呈现SKscene的UIViewController? [英] How do I present UIViewController from SKscene with social framework?
问题描述
我正在制作像Flappy Bird这样的游戏。如何从SKScene呈现UIViewController?
首先,我告诉我的环境
- Mac OS X 10.9
- Xcode 5.0.2
- Sprite Kit(框架),social.framework(框架)添加到我的项目中
我的目标是在游戏结束时显示分享按钮。点击共享按钮图像应该呈现SLComposeViewController(Twitter共享)。场景的内容不应该改变。
我想解决下面的问题并将显示从GameOverScene更改为tweetSheet(显示)
由social.framework组成。
问题
[self presentViewController:tweetSheet animated:YES completion:nil] ;
//错误:'GameOverScene'没有可见的@interface声明选择器presentViewController:动画:完成:
我的编码文件如下(我提取了重要代码的部分内容)。
ViewController.h
import< UIKit / UIKit.h>
import< SpriteKit / SpriteKit.h>
import< iAd / iAd.h>
@interface ViewController:UIViewController< ADBannerViewDelegate>< br>
@end
GameOverScene.h
#import< SpriteKit / SpriteKit.h>
@class SpriteViewController;
@interface GameOverScene:SKScene {
}
@end
GameOverScene.m
#importGameOverScene.h
#importNewGameScene.h
#importMainScene.h
#import< Social / Social.h>
@implementation GameOverScene {
//推特按钮
SKSpriteNode * _twitterbutton;
}
- (id)initWithSize:(CGSize)size
{
if(self = [super initWithSize:size]){
//创建twitter按钮来自Images.xcassets的twitterbutton图片
_twitterbutton = [SKSpriteNode spriteNodeWithImageNamed:@twitterbutton];
[_twitterbutton setSize:CGSizeMake(50,50)];
[_twitterbutton setPosition:CGPointMake(self.size.width / 2,self.size.height / 5 + 50)];
//添加推特按钮
[self addChild:_twitterbutton];
//再次,这很重要,否则我们无法确定按下了什么按钮
_twitterbutton.name = @twitterbutton;
[_twitterbutton setPosition:CGPointMake(self.size.width / 2,self.size.height / 5 + 50)]
}
return self;
}
- (void)touchesBegan:(NSSet *)触及withEvent:(UIEvent *)事件
{
//与NewGameScene相同menu
UITouch * touch = [touches anyObject];
CGPoint location = [touch locationInNode:self];
SKNode * node = [self nodeAtPoint:location];
//是否触摸了twitter按钮?
if([node.name isEqualToString:@twitterbutton]){
if([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter]){
SLComposeViewController * tweetSheet = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeTwitter];
[tweetSheet setInitialText:@来自游戏的TestTweet !!];
[self presentViewController:tweetSheet animated:YES completion:nil];
** //错误:'GameOverScene'没有可见的@interface声明选择器presentViewController:动画:完成:**
}
}
ViewControlloer.m
#import ViewController.h
#importNewGameScene.h
@implementation ViewController
//将视图加载到我们的主类
- (void)loadView
{
self.view = [[SKView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
}
//视图完成加载时执行
- (void)viewWillLayoutSubviews
{
[super viewDidLoad];
//将调整大小模式设置为灵活宽度和高度
[self.view setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight];
//从原始视图创建我们的视图
//确保保留originalContentView,否则应用程序将崩溃
SKView * skView =(SKView *)self.originalContentView ;
//我们根据当前尺寸创建一个新的NewGameScene
SKScene * scene = [NewGameScene sceneWithSize:skView.bounds.size];
//创建一个动画类型为淡入淡出且持续时间为.4秒的过渡类
SKTransition * transition = [SKTransition fadeWithDuration:.4];
//使用淡入淡出转换显示菜单视图(NewGameScene)
[skView presentScene:场景转换:转换];
}
@end
您无法在SKScene中呈现viewController,因为它实际上只在SKView上呈现。您需要一种方法向viewController发送消息,而viewController又将呈现viewController。为此,您可以使用委派。
将以下协议定义添加到SKScene的.h文件中:
@protocol sceneDelegate< NSObject>
- (void)showShareScreen;
@end
并在界面中声明一个委托属性:
@property(弱,非原子)id< sceneDelegate>代表;
然后,在您想要显示共享屏幕的位置,而不是行:
[self presentViewController:tweetSheet animated:YES completion:nil];
使用此行:
[self.delegate showShareScreen];
现在,在你的viewController的.h文件中,实现协议:
@interface ViewController:UIViewController< sceneDelegate>
并且,在您的.m文件中,在呈现场景之前添加以下行:
scene.delegate = self;
然后在那里添加以下方法:
- (void)presentShareScreen
{
if([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter])
{
SLComposeViewController * tweetSheet = [SLComposeViewController
composeViewControllerForServiceType:SLServiceTypeTwitter];
[tweetSheet setInitialText:@来自游戏的TestTweet !!];
[self presentViewController:tweetSheet animated:YES completion:nil];
}
}
另一种方法是使用 NSNotificationCenter
保留 -presentShareScreen
方法,如上一个替代方案中所述。
将viewController添加为通知的监听器,其中 -viewDidLoad
方法:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(presentShareScreen)name:@showShareScreenobject:nil];
然后,在要显示此viewController的场景中,使用以下行: / p>
[[NSNotificationCenter defaultCenter] postNotificationName:@showShareScreen对象:nil];
I am making a game like Flappy Bird. How do I present a UIViewController from SKScene? First of all, I tell my environments
- Mac OS X 10.9
- Xcode 5.0.2
- Sprite Kit(framework), social.framework(framework) are added in my project
My goal is to display a "Share" button upon Game Over. Tapping the share button image should present a SLComposeViewController (Twitter Share). The contents of the scene should not change.
I'd like to solve bellow issue and change display from GameOverScene to tweetSheet(display)
composed with social.framework.
The issue
[self presentViewController:tweetSheet animated:YES completion:nil];
//Error:No visible @interface for 'GameOverScene' declares the selector "presentViewController":animated:completion:
My coding files are below(I extracted parts of important codes).
ViewController.h
import <UIKit/UIKit.h>
import <SpriteKit/SpriteKit.h>
import <iAd/iAd.h>
@interface ViewController : UIViewController<ADBannerViewDelegate><br>
@end
GameOverScene.h
#import <SpriteKit/SpriteKit.h>
@class SpriteViewController;
@interface GameOverScene : SKScene {
}
@end
GameOverScene.m
#import "GameOverScene.h"
#import "NewGameScene.h"
#import "MainScene.h"
#import <Social/Social.h>
@implementation GameOverScene {
//The twitter button
SKSpriteNode *_twitterbutton;
}
- (id)initWithSize:(CGSize)size
{
if (self = [super initWithSize:size]) {
//Creating the twitterbutton with the twitterbutton image from Images.xcassets
_twitterbutton = [SKSpriteNode spriteNodeWithImageNamed:@"twitterbutton"];
[_twitterbutton setSize:CGSizeMake(50, 50)];
[_twitterbutton setPosition:CGPointMake(self.size.width/2, self.size.height/5 + 50)];
//Adding the twitter button
[self addChild:_twitterbutton];
//Again, this is important, otherwise we can't identify what button is pressed
_twitterbutton.name = @"twitterbutton";
[_twitterbutton setPosition:CGPointMake(self.size.width/2, self.size.height/5 + 50)]
}
return self;
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
//Same as in NewGameScene menu
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInNode:self];
SKNode *node = [self nodeAtPoint:location];
//Is the twitter button touched?
if([node.name isEqualToString:@"twitterbutton"]){
if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter]){
SLComposeViewController *tweetSheet = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeTwitter];
[tweetSheet setInitialText:@"TestTweet from the Game !!"];
[self presentViewController:tweetSheet animated:YES completion:nil];
**//Error:No visible @interface for 'GameOverScene' declares the selector "presentViewController":animated:completion:**
}
}
ViewControlloer.m
#import "ViewController.h"
#import "NewGameScene.h"
@implementation ViewController
//Loads the view onto our main class
- (void)loadView
{
self.view = [[SKView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
}
//Executes when view finishes loading
- (void)viewWillLayoutSubviews
{
[super viewDidLoad];
//Set the resize mode to flexible width and height
[self.view setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight];
//Create our view from our original view
//Make sure to leave originalContentView in, otherwise the app will crash
SKView *skView = (SKView *)self.originalContentView;
//We create a new NewGameScene according to the current dimensions
SKScene *scene = [NewGameScene sceneWithSize:skView.bounds.size];
//Create a transition class with animation type fade and a duration of .4 seconds
SKTransition *transition = [SKTransition fadeWithDuration:.4];
//Present the menu view (NewGameScene) with our fade in transition
[skView presentScene:scene transition:transition];
}
@end
You cannot present a viewController from within a SKScene as it is actually only being rendered on a SKView. You need a way to send a message to the viewController, which in turn will present the viewController. For this, you can use delegation.
Add the following protocol definition to your SKScene's .h file:
@protocol sceneDelegate <NSObject>
-(void)showShareScreen;
@end
And declare a delegate property in the interface:
@property (weak, nonatomic) id <sceneDelegate> delegate;
Then, at the point where you want to present the share screen, instead of the line:
[self presentViewController:tweetSheet animated:YES completion:nil];
Use this line:
[self.delegate showShareScreen];
Now, in your viewController's .h file, implement the protocol:
@interface ViewController : UIViewController <sceneDelegate>
And, in your .m file, add the following line before you present the scene:
scene.delegate = self;
Then add the following method there:
-(void)presentShareScreen
{
if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter])
{
SLComposeViewController *tweetSheet = [SLComposeViewController
composeViewControllerForServiceType:SLServiceTypeTwitter];
[tweetSheet setInitialText:@"TestTweet from the Game !!"];
[self presentViewController:tweetSheet animated:YES completion:nil];
}
}
An alternate method would be to use NSNotificationCenter
Keep the -presentShareScreen
method as described in the previous alternative.
Add the viewController as a listener to the notification in it's -viewDidLoad
method:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(presentShareScreen) name:@"showShareScreen" object:nil];
Then, in the scene at the point where you want to show this viewController, use this line:
[[NSNotificationCenter defaultCenter] postNotificationName:@"showShareScreen" object:nil];
这篇关于如何使用社交框架呈现SKscene的UIViewController?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!