NSTimer的暂停与恢复
2013-01-21 09:59
155 查看
#import <Foundation/Foundation.h>
@interface NSTimer (Pausing)
- (NSMutableDictionary *)pauseDictionary;
- (void)pause;
- (void)resume;
@end
NSTimer+Pausing.m
#import "NSTimer+Pausing.h"
NSString *kIsPausedKey = @"IsPaused Key";
NSString *kRemainingTimeIntervalKey = @"RemainingTimeInterval Key";
@implementation NSTimer (Pausing)
- (NSMutableDictionary *)pauseDictionary {
static NSMutableDictionary *globalDictionary = nil;
if(!globalDictionary)
globalDictionary = [[NSMutableDictionary alloc] init];
if(![globalDictionary objectForKey:[NSNumber numberWithInt:(int)self]]) {
NSMutableDictionary *localDictionary = [[[NSMutableDictionary alloc] init] autorelease];
[globalDictionary setObject:localDictionary forKey:[NSNumber numberWithInt:(int)self]];
}
return [globalDictionary objectForKey:[NSNumber numberWithInt:(int)self]];
}
- (void)pause {
if(![self isValid])
return;
NSNumber *isPausedNumber = [[self pauseDictionary] objectForKey:kIsPausedKey];
if(isPausedNumber && YES == [isPausedNumber boolValue])
return;
NSDate *now = [NSDate date];
NSDate *then = [self fireDate];
NSTimeInterval remainingTimeInterval = [then timeIntervalSinceDate:now];
[[self pauseDictionary] setObject:[NSNumber numberWithDouble:remainingTimeInterval] forKey:kRemainingTimeIntervalKey];
[self setFireDate:[NSDate distantFuture]];
[[self pauseDictionary] setObject:[NSNumber numberWithBool:YES] forKey:kIsPausedKey];
}
- (void)resume {
if(![self isValid])
return;
NSNumber *isPausedNumber = [[self pauseDictionary] objectForKey:kIsPausedKey];
if(!isPausedNumber || NO == [isPausedNumber boolValue])
return;
NSTimeInterval remainingTimeInterval = [[[self pauseDictionary] objectForKey:kRemainingTimeIntervalKey] doubleValue];
NSDate *fireDate = [NSDate dateWithTimeIntervalSinceNow:remainingTimeInterval];
[self setFireDate:fireDate];
[[self pauseDictionary] setObject:[NSNumber numberWithBool:NO] forKey:kIsPausedKey];
}
@end
#import <Foundation/Foundation.h> @interface NSTimer (Pausing) - (NSMutableDictionary *)pauseDictionary; - (void)pause; - (void)resume; @end
NSTimer+Pausing.m
#import "NSTimer+Pausing.h"
NSString *kIsPausedKey = @"IsPaused Key";
NSString *kRemainingTimeIntervalKey = @"RemainingTimeInterval Key";
@implementation NSTimer (Pausing)
- (NSMutableDictionary *)pauseDictionary {
static NSMutableDictionary *globalDictionary = nil;
if(!globalDictionary)
globalDictionary = [[NSMutableDictionary alloc] init];
if(![globalDictionary objectForKey:[NSNumber numberWithInt:(int)self]]) {
NSMutableDictionary *localDictionary = [[[NSMutableDictionary alloc] init] autorelease];
[globalDictionary setObject:localDictionary forKey:[NSNumber numberWithInt:(int)self]];
}
return [globalDictionary objectForKey:[NSNumber numberWithInt:(int)self]];
}
- (void)pause {
if(![self isValid])
return;
NSNumber *isPausedNumber = [[self pauseDictionary] objectForKey:kIsPausedKey];
if(isPausedNumber && YES == [isPausedNumber boolValue])
return;
NSDate *now = [NSDate date];
NSDate *then = [self fireDate];
NSTimeInterval remainingTimeInterval = [then timeIntervalSinceDate:now];
[[self pauseDictionary] setObject:[NSNumber numberWithDouble:remainingTimeInterval] forKey:kRemainingTimeIntervalKey];
[self setFireDate:[NSDate distantFuture]];
[[self pauseDictionary] setObject:[NSNumber numberWithBool:YES] forKey:kIsPausedKey];
}
- (void)resume {
if(![self isValid])
return;
NSNumber *isPausedNumber = [[self pauseDictionary] objectForKey:kIsPausedKey];
if(!isPausedNumber || NO == [isPausedNumber boolValue])
return;
NSTimeInterval remainingTimeInterval = [[[self pauseDictionary] objectForKey:kRemainingTimeIntervalKey] doubleValue];
NSDate *fireDate = [NSDate dateWithTimeIntervalSinceNow:remainingTimeInterval];
[self setFireDate:fireDate];
[[self pauseDictionary] setObject:[NSNumber numberWithBool:NO] forKey:kIsPausedKey];
}
@end
相关文章推荐
- NSTimer 的暂停与恢复运行。
- NSTimer的暂停与恢复
- NSTimer 的暂停与恢复运行。
- NSTimer 的暂停与恢复运行。
- Android中的java层的线程暂停和恢复实现
- [iOS 多线程 & 网络 - 2.4] - 大文件下载 (边下边写/暂停恢复下载/压缩解压zip/多线程下载)
- 如何暂停和恢复CALayer上的动画
- java多线程被废除的暂停和恢复
- springmvc+easyui实现界面控制quartz的暂停、恢复、修改、添加
- java线程停止、暂停和恢复等状态的控制
- C# 线程的暂停和恢复的 实现
- C# winform 多线程异步操作线程启动暂停与恢复
- WPF在XAML中实现持续动画的暂停、恢复、停止
- 暂停线程suspend()和恢复线程resume()
- JAVA多线程机制之暂停、恢复和停止
- 5.cocos2dx中关于draw绘图,声音和音效,预加载,播放与停止Vs暂停和恢复,音量控制
- linux中线程的挂起与恢复(进程暂停)(2013-06-19 13:42:58)
- C# 线程的暂停与恢复
- Quartz的任务的临时启动和暂停和恢复
- NSTimer使用:暂停、继续、释放、解除循环引用