ios 中的定时器功能
来源:互联网 发布:网络天才中文版下载app 编辑:程序博客网 时间:2024/06/03 23:05
在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;
}
0 0
- ios 中的定时器功能
- spring中的定时器功能
- spring中的定时器功能
- spring中的定时器功能
- iOS中的定时器
- iOS中的定时器
- Objective-C中的定时器功能
- Objective-C中的定时器功能
- iOS中的三大定时器
- iOS中的几种定时器
- boost中的asio实现定时器功能
- iOS中的小功能
- iOS 中的NSTimer定时器的作用
- iOS 中的NSTimer定时器的作用
- iOS中的三种定时器使用方法
- iOS 中的NSTimer定时器的作用
- iOS/OS X中的高精确度定时器
- iOS 中的重要功能--推送
- Windows下安装配置Solr (tomcat7.0)
- HDOJ 4533 威威猫系列故事——晒被子
- git - 简明指南
- Linux内存管理和分析vmalloc使用的地址范围
- 史上最强大ios app网络抓取 from http://www.2cto.com/net/201306/221430.html
- ios 中的定时器功能
- UIView视图层次操作
- 求A/B高精度值
- iOS总结_IOS开发部分语法的简化写法
- courses-machine learning-gradient descent
- AFNetwork 2.0在请求时报错code=-1016 和 3840
- kjdhkdflajsdjaldkja alkfjalskdjflakjsda falksdjfklsjd
- 重写java类的toString()方法, 安卓开发
- display:inline、block、inline-block的区别