Objective-C中的定时器功能
来源:互联网 发布:怎么开启内网端口 编辑:程序博客网 时间:2024/06/05 08:34
文章参考地址:http://doandroid.info/object-c中的定时器功能/
在Object-C中,有三种方法可以实现定时器的功能。
1 使用NSObject对象的performSelector:withObject:afterDelay:的方法。
2 使用GCD的Block Objects方法。
3 使用GCD的C Functions方法。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
- (void) printString:(NSString *)paramString
{
NSLog(@"%@", paramString);
}
- (BOOL) application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[self performSelector:@selector(printString:) withObject:@"Grand Central Dispatch" afterDelay:3.0];
self.window = [[UIWindow alloc] initWithFrame: [[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
{
NSLog(@"%@", paramString);
}
- (BOOL) application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[self performSelector:@selector(printString:) withObject:@"Grand Central Dispatch" afterDelay:3.0];
self.window = [[UIWindow alloc] initWithFrame: [[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
使用GCD的dispatch_after方法和dispatch_after_f方法。
第一个方法三个参数:Delay in nanoseconds,Dispatch queue,Block object
第二个方法四个参数:Delay in nanoseconds,Dispatch queue,Context,C function
第一种方法的实现:
1
2
3
4
5
6
7
2
3
4
5
6
7
double delayInSeconds = 2.0;
dispatch_time_t delayInNanoSeconds = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
dispatch_queue_t concurrentQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_after(delayInNanoSeconds, concurrentQueue, ^(void)
{
/* Perform your operations here */
});
dispatch_time_t delayInNanoSeconds = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
dispatch_queue_t concurrentQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_after(delayInNanoSeconds, concurrentQueue, ^(void)
{
/* Perform your operations here */
});
第二种实现:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void processSomething(void *paramContext){
/* Do your processing here */
NSLog(@"Processing...");
}
- (BOOL) application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
double delayInSeconds = 2.0;
dispatch_time_t delayInNanoSeconds = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
dispatch_queue_t concurrentQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_after_f(delayInNanoSeconds, concurrentQueue,NULL, processSomething);
self.window = [[UIWindow alloc] initWithFrame: [[UIScreen mainScreen] bounds]];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
/* Do your processing here */
NSLog(@"Processing...");
}
- (BOOL) application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
double delayInSeconds = 2.0;
dispatch_time_t delayInNanoSeconds = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
dispatch_queue_t concurrentQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_after_f(delayInNanoSeconds, concurrentQueue,NULL, processSomething);
self.window = [[UIWindow alloc] initWithFrame: [[UIScreen mainScreen] bounds]];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
- Objective-C中的定时器功能
- Objective-C中的定时器功能
- Objective-c定时器
- objective-c NSTimer 定时器
- Objective-C定时器
- spring中的定时器功能
- spring中的定时器功能
- spring中的定时器功能
- ios 中的定时器功能
- objective-c中的NSBundle
- Objective-C 中的BOOL
- Objective-c中的委托
- Objective-C中的数据类型
- Objective-C中的委托
- Objective-C中的SEL
- Objective-C中的锁
- Objective-c中的 Protocol
- Objective-C中的Block
- Android 反编译,修改,重新打包,签名
- mac 10.8 montion lion 山狮系统上 安装 rails 和 oci8 连 oracle 数据库
- 安装NS2(xp+cygwin+ns2.28)
- 快速排序 有问题
- 温家宝:今后5年有3件事情重大而迫切
- Objective-C中的定时器功能
- 我的安卓学习之路--split,StringTokenizer
- 基于Hadoop平台的云存储应用实践
- 问题:MySQL doesn't yet support 'LIMIT & IN/ALL/AN Y/SOME subquery
- C++第11周项目2(8)参考——软件比拼
- 修改论文格式——word技巧大乱斗
- 教授如何骗自己
- 胡锦涛:扭转生态环境恶化趋势建设美丽中国
- 数组中最小没出现的整数