#import "AppDelegate.h"
@interface AppDelegate ()
{
NSTimer *timer;
UILabel *label;
}
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//创建窗口
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[self.window setBackgroundColor:[UIColor whiteColor]];
[self.window makeKeyAndVisible];
//创建视图
UIView *view = [[UIView alloc] initWithFrame:self.window.frame];
view.backgroundColor = [UIColor orangeColor];
[self.window addSubview:view];
label = [[UILabel alloc] initWithFrame:CGRectMake(100, 100, 100, 50)];
label.text = @"100";
label.textAlignment = NSTextAlignmentCenter;
label.backgroundColor = [UIColor redColor];
[view addSubview:label];
UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeCustom];
[btn1 setFrame:CGRectMake(100, 200, 100, 50)];
[btn1 setTitle:@"开始计时" forState:UIControlStateNormal];
[btn1 addTarget:self action:@selector(btnAction1) forControlEvents:UIControlEventTouchUpInside];
[view addSubview:btn1];
UIButton *btn2 = [UIButton buttonWithType:UIButtonTypeCustom];
[btn2 setFrame:CGRectMake(100, 300, 100, 50)];
[btn2 setTitle:@"停止计时" forState:UIControlStateNormal];
[btn2 addTarget:self action:@selector(btnAction2) forControlEvents:UIControlEventTouchUpInside];
[view addSubview:btn2];
UIButton *btn3 = [UIButton buttonWithType:UIButtonTypeCustom];
[btn3 setFrame:CGRectMake(100, 400, 100, 50)];
[btn3 setTitle:@"计时复位" forState:UIControlStateNormal];
[btn3 addTarget:self action:@selector(btnAction3) forControlEvents:UIControlEventTouchUpInside];
[view addSubview:btn3];
return YES;
}
-(void)btnAction1{
timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerAction:) userInfo:nil repeats:YES];
}
-(void)btnAction2{
[timer invalidate];
timer = nil;
}
-(void)btnAction3{
label.text = @"100";
[self btnAction2];
[self btnAction1];
}
-(void)timerAction:(NSTimer *)timer{
NSString *timeStr = label.text;
NSInteger i = [timeStr integerValue] -1;
timeStr = [NSString stringWithFormat:@"%ld",i];
NSLog(@"%@",timeStr);
label.text = timeStr;
}
- (void)applicationWillResignActive:(UIApplication *)application {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}
- (void)applicationDidEnterBackground:(UIApplication *)application {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}
- (void)applicationWillEnterForeground:(UIApplication *)application {
// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}
- (void)applicationWillTerminate:(UIApplication *)application {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
@end