从 xcode 和目标 c 的 appdelegate 暂停计时器
问题说明
我正在为 iPhone 制作游戏,并且希望能够在用户的游戏中断时(例如他们按下主页按钮时)暂停计时器.我知道在应用程序委托中有一个当应用程序离开前台时调用的方法:
I am making a game for iPhone and want to be able to pause a timer when a user's game is interrupted like when they hit the home button. I know that in the app delegate there is a method when the app leaves the foreground called:
- (void)applicationWillResignActive:(UIApplication *)application
我正在努力解决的是如何暂停计时器.我的视图控制器中有一个名为 pauseGame 的函数,用于当用户想要暂停游戏时.我认为使用这种方法暂停游戏是最简单的.但是,我无法理解如何调用此方法.有任何想法吗?很抱歉初学者的问题.
What I am struggling with is how to pause the timer. I have a function in my view controller that's called pauseGame and is used for when the user wants to pause the game. I was thinking that it would be easiest to pause the game by using this method. I cannot however understand how to call this method. Any ideas? And sorry for the beginner question.
正确答案
最短的方法是使用通知:
The shortest way is to Use Notifications:
1. 在您的应用程序委托(或其他任何地方...)定义自定义通知
1. define a custom notification, at your application delegate (or anywhere else...)
#define kApplicationWillResignActiveNotification
@"kApplicationWillResignActiveNotification"
2. 在 applicationWillResignActive: 方法被调用时发送通知
2. dispatch the notification when the applicationWillResignActive: method is called
[[NSNotificationCenter defaultCenter] postNotificationName:
kApplicationWillResignActive object:nil];
3. 在项目中的任何位置收听该通知(* 在您@定义通知的位置导入头文件)
3. listen to that notification where ever you want in your project (* import the header file where you @defined the notification)
[[NSNotificationCenter defaultCenter] addObserver:self
selector: @selector(appResigned:)
name:kApplicationWillResignActiveNotification object: nil];
4.如果将 NSNotification 对象添加到选择器中,您可以获得它
4. you can get the NSNotification Object if you add it to your selector
-(void)appResigned:(NSNotification *)notification;
这篇好文章是转载于:学新通技术网
- 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
- 本站站名: 学新通技术网
- 本文地址: /reply/detail/tanhcfjjee
-
YouTube API 不能在 iOS (iPhone/iPad) 工作,但在桌面浏览器工作正常?
it1352 07-30 -
iPhone,一张图像叠加到另一张图像上以创建要保存的新图像?(水印)
it1352 07-17 -
保持在后台运行的 iPhone 应用程序完全可操作
it1352 07-25 -
使用 iPhone 进行移动设备管理
it1352 07-23 -
在android同时打开手电筒和前置摄像头
it1352 09-28 -
扫描 NFC 标签时是否可以启动应用程序?
it1352 08-02 -
检查邮件是否发送成功
it1352 07-25 -
Android微调工具-删除当前选择
it1352 06-20 -
希伯来语的空格句子标记化错误
it1352 06-22 -
Android App 和三星 Galaxy S4 不兼容
it1352 07-20