/*
做一个 100 秒倒计时的程序 , 注意考虑 , 当程序进入后台时的情况。
*/
做一个 100 秒倒计时的程序 , 注意考虑 , 当程序进入后台时的情况。
*/
AppDelegate.h文件中:
#import
<UIKit/UIKit.h>
@interface AppDelegate : UIResponder < UIApplicationDelegate >
{
UILabel *_label;
NSInteger _index;
NSTimer *_timer;
NSDate *_backgroundDate;
}
@property ( strong , nonatomic ) UIWindow *window;
@interface AppDelegate : UIResponder < UIApplicationDelegate >
{
UILabel *_label;
NSInteger _index;
NSTimer *_timer;
NSDate *_backgroundDate;
}
@property ( strong , nonatomic ) UIWindow *window;
@end
AppDelegate.m文件中:
#import
"AppDelegate.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- ( BOOL )application:( UIApplication *)application didFinishLaunchingWithOptions:( NSDictionary *)launchOptions {
// Override point for customization after application launch.
//******* 创建 window 窗口 *********************
// 拿到屏幕的大小
CGRect rect = [ UIScreen mainScreen ]. bounds ;
// 创建一个 window
self . window = [[ UIWindow alloc ] initWithFrame :rect];
// 设置窗口颜色
self . window . backgroundColor = [ UIColor greenColor ];
// 把当前的 window 作为程序的主 window 显示出来
[ self . window makeKeyAndVisible ];
_index = 100 ;
// 创建一个 label
_label = [[ UILabel alloc ] initWithFrame : CGRectMake ( 0 , 175 , 375 , 75 )];
// 设置 label 背景色
_label . backgroundColor = [ UIColor grayColor ];
_label . text = @"100" ;
// 设置文字颜色
_label . textColor = [ UIColor whiteColor ];
// 设置文字对齐方式
_label . textAlignment = NSTextAlignmentCenter ;
// 设置字体大小
_label . font = [ UIFont boldSystemFontOfSize : 24 ];
// 将 label 添加到 window 上
[ self . window addSubview : _label ];
_timer = [ NSTimer scheduledTimerWithTimeInterval : 1
target : self
selector : @selector (timeAction:)
userInfo : nil repeats : YES ];
return YES ;
}
- ( void )timeAction:( NSTimer *)timer{
_index --;
_label . text = [ NSString stringWithFormat : @"%ld" , _index ];;
if ( _index == 0 ) {
[timer invalidate ];
return ;
}
}
// 程序已经进入后台
- ( void )applicationDidEnterBackground:( UIApplication *)application {
[ _timer invalidate ]; // 停止计时器
_backgroundDate = [ NSDate date ]; // 获得程序进入前台时的时间
}
// 程序将要进入前台
- ( void )applicationWillEnterForeground:( UIApplication *)application {
NSDate *foregroundDate = [ NSDate date ]; // 获得程序即将进入活动状态时的时间
NSTimeInterval subTime = [foregroundDate timeIntervalSinceDate : _backgroundDate ]; // 获得程序待在后台的时间
_index -= subTime;
_label . text = [ NSString stringWithFormat : @"%ld" , _index ];
[ NSTimer scheduledTimerWithTimeInterval : 1
target : self
selector : @selector (timeAction:)
userInfo : nil
repeats : YES ];
}
@interface AppDelegate ()
@end
@implementation AppDelegate
- ( BOOL )application:( UIApplication *)application didFinishLaunchingWithOptions:( NSDictionary *)launchOptions {
// Override point for customization after application launch.
//******* 创建 window 窗口 *********************
// 拿到屏幕的大小
CGRect rect = [ UIScreen mainScreen ]. bounds ;
// 创建一个 window
self . window = [[ UIWindow alloc ] initWithFrame :rect];
// 设置窗口颜色
self . window . backgroundColor = [ UIColor greenColor ];
// 把当前的 window 作为程序的主 window 显示出来
[ self . window makeKeyAndVisible ];
_index = 100 ;
// 创建一个 label
_label = [[ UILabel alloc ] initWithFrame : CGRectMake ( 0 , 175 , 375 , 75 )];
// 设置 label 背景色
_label . backgroundColor = [ UIColor grayColor ];
_label . text = @"100" ;
// 设置文字颜色
_label . textColor = [ UIColor whiteColor ];
// 设置文字对齐方式
_label . textAlignment = NSTextAlignmentCenter ;
// 设置字体大小
_label . font = [ UIFont boldSystemFontOfSize : 24 ];
// 将 label 添加到 window 上
[ self . window addSubview : _label ];
_timer = [ NSTimer scheduledTimerWithTimeInterval : 1
target : self
selector : @selector (timeAction:)
userInfo : nil repeats : YES ];
return YES ;
}
- ( void )timeAction:( NSTimer *)timer{
_index --;
_label . text = [ NSString stringWithFormat : @"%ld" , _index ];;
if ( _index == 0 ) {
[timer invalidate ];
return ;
}
}
// 程序已经进入后台
- ( void )applicationDidEnterBackground:( UIApplication *)application {
[ _timer invalidate ]; // 停止计时器
_backgroundDate = [ NSDate date ]; // 获得程序进入前台时的时间
}
// 程序将要进入前台
- ( void )applicationWillEnterForeground:( UIApplication *)application {
NSDate *foregroundDate = [ NSDate date ]; // 获得程序即将进入活动状态时的时间
NSTimeInterval subTime = [foregroundDate timeIntervalSinceDate : _backgroundDate ]; // 获得程序待在后台的时间
_index -= subTime;
_label . text = [ NSString stringWithFormat : @"%ld" , _index ];
[ NSTimer scheduledTimerWithTimeInterval : 1
target : self
selector : @selector (timeAction:)
userInfo : nil
repeats : YES ];
}
@end