您的位置:首页 > 移动开发 > IOS开发

iOS NSTimer样例demo

2015-10-28 00:00 155 查看
摘要: 定时器NSTimer运用
新建一个空工程

所有代码如下:是一个画板三基颜色的随机变化

#import "AppDelegate.h"

@interface AppDelegate ()
{
NSTimer *_timer;//定时器
}
@end

@implementation AppDelegate
- (void)dealloc {
self.window =nil;
[super dealloc];
}
- (void)creatButton {
NSArray *arr = @[@"开始",@"暂停"];

for (NSInteger i = 0; i < arr.count; i++) {
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
//会算坐标
button.frame = CGRectMake([UIScreen mainScreen].bounds.size.width/2-50, 100+i*60, 100, 50);
//设置标题
[button setTitle:arr[i] forState:UIControlStateNormal];

//增加事件 self 执行带参数的btnClick:
//谁在增加的事件 那么参数传得就是谁
[button addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
//设置tag 值区分按钮
button.tag = 1+i;

//粘贴
[self.window addSubview:button];

}
}
-(void)btnClick:(UIButton *)btn{
switch (btn.tag) {
case 1:
{
//启动定时器
[_timer setFireDate:[NSDate distantPast]];
}
break;
case 2://定时器暂停
{
[_timer setFireDate:[NSDate distantFuture]];
}
break;

default:
break;
}

}
//button触发之后调用的方法

- (void)creatTimer {
/*
第一个参数:时间间隔 单位 s
2     : 目标对象地址 (任意) 一般写self
3     :目标对象的行为 选择器
4    : nil
5    : YES 是否重复
一旦用这个函数创建定时器 那么定时器就会立即启动
定时器启动之后 就会起一个子线程 每个一段时间 通知 UI主线程 使self 调用 timerClick 方法
*/
//实例化一个定时器对象

_timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerClick) userInfo:nil repeats:YES];
//默认创建 NSTimer 之后就会启动

//但是我们的需求 点击按钮才能启动
//现在我们应该先把定时器暂停了

//暂停定时器
[_timer setFireDate:[NSDate distantFuture]];

}
//定时器 触发的函数
- (void)timerClick {
//NSLog(@"%s",__func__);
//
//    CGFloat red = arc4random()%256/255.0;//随机产生 0--1
//    CGFloat green = arc4random()%256/255.0;
//    CGFloat blue = arc4random()%256/255.0;
//

//通过三基色进行配色
//0-1
self.window.backgroundColor = [UIColor colorWithRed:arc4random()%256/255.0 green:arc4random()%256/255.0 blue:arc4random()%256/255.0 alpha:1];
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];

[self creatButton];
//创建定时器
[self creatTimer];

self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}

- (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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: