@interface ViewController ()
{
NSProgress * progress;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//这个方法将创建任务进度管理对象 UnitCount是一个基于UI上的完整任务的单元数
progress = [NSProgress progressWithTotalUnitCount:10];
NSTimer * timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(task) userInfo:nil repeats:YES];
//对任务进度对象的完成比例进行监听
[progress addObserver:self forKeyPath:@"fractionCompleted" options:NSKeyValueObservingOptionNew context:nil];
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
NSLog(@"进度= %f",progress.fractionCompleted);
}
-(void)task{
//完成任务单元数+1
if (progress.completedUnitCount<progress.totalUnitCount) {
progress.completedUnitCount +=1;
}
}
//显示完后比例 如:10% completed @property (null_resettable, copy) NSString *localizedDescription; //完成数量 如:1 of 10 @property (null_resettable, copy) NSString *localizedAdditionalDescription;
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//这个方法将创建任务进度管理对象 UnitCount是一个基于UI上的完整任务的单元数
progress = [NSProgress progressWithTotalUnitCount:10];
//对任务进度对象的完成比例进行监听
[progress addObserver:self forKeyPath:@"fractionCompleted" options:NSKeyValueObservingOptionNew context:nil];
//向下分支出一个子任务 子任务进度总数为5个单元 即当子任务完成时 父progerss对象进度走5个单元
[progress becomeCurrentWithPendingUnitCount:5];
[self subTaskOne];
[progress resignCurrent];
//向下分出第2个子任务
[progress becomeCurrentWithPendingUnitCount:5];
[self subTaskOne];
[progress resignCurrent];
}
-(void)subTaskOne{
//子任务总共有10个单元
NSProgress * sub =[NSProgress progressWithTotalUnitCount:10];
int i=0;
while (i<10) {
i++;
sub.completedUnitCount++;
}
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
NSLog(@"= %@",progress.localizedAdditionalDescription);
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//这个方法将创建任务进度管理对象 UnitCount是一个基于UI上的完整任务的单元数
progress = [NSProgress progressWithTotalUnitCount:10];
//对任务进度对象的完成比例进行监听
[progress addObserver:self forKeyPath:@"fractionCompleted" options:NSKeyValueObservingOptionNew context:nil];
//创建子节点
NSProgress * sub = [NSProgress progressWithTotalUnitCount:10 parent:progress pendingUnitCount:5];
NSProgress * sub2 = [NSProgress progressWithTotalUnitCount:10 parent:progress pendingUnitCount:5];
for (int i=0; i<10; i++) {
sub.completedUnitCount ++;
sub2.completedUnitCount ++;
}
}
//获取当前线程的进度管理对象根节点 //注意:当有NSProgress对象调用了becomeCurrentWithPendingUnitCount:方法后,这个方法才能获取到 + (nullable NSProgress *)currentProgress; //创建一个NSProgress对象,需要传入进度的单元数量 + (NSProgress *)progressWithTotalUnitCount:(int64_t)unitCount; //和上一个方法功能相似 iOS9之后的新方法 + (NSProgress *)discreteProgressWithTotalUnitCount:(int64_t)unitCount; //iOS9之后的新方法 创建某个进度指示器节点的子节点 + (NSProgress *)progressWithTotalUnitCount:(int64_t)unitCount parent:(NSProgress *)parent pendingUnitCount:(int64_t)portionOfParentTotalUnitCount; //NSProgress实例的初始化方法 自父节点参数可以为nil - (instancetype)initWithParent:(nullable NSProgress *)parentProgressOrNil userInfo:(nullable NSDictionary *)userInfoOrNil; //注册为当前线程根节点 - (void)becomeCurrentWithPendingUnitCount:(int64_t)unitCount; //取消注册 与注册方法必须同步出现 - (void)resignCurrent; //iOS9新方法 向一个节点中添加一个子节点 - (void)addChild:(NSProgress *)child withPendingUnitCount:(int64_t)inUnitCount; //进度单元总数 @property int64_t totalUnitCount; //已完成的进度单元数 @property int64_t completedUnitCount; //是否可取消 @property (getter=isCancellable) BOOL cancellable; //是否可暂停 @property (getter=isPausable) BOOL pausable; //进度比例 0-1之间 @property (readonly) double fractionCompleted; //取消 - (void)cancel; //暂停 - (void)pause; //恢复 - (void)resume
//设置剩余时间 会影响localizedAdditionalDescription的值 /* 例如:0 of 10 — About 10 seconds remaining */ [progress setUserInfoObject:@10 forKey:NSProgressEstimatedTimeRemainingKey]; //设置完成速度信息 会影响localizedAdditionalDescription的值 /* 例如:Zero KB of 10 bytes (15 bytes/sec) */ [progress setUserInfoObject:@15 forKey:NSProgressThroughputKey]; /* 下面这些键值的生效 必须将NSProgress对象的kind属性设置为 NSProgressKindFile NSProgressFileOperationKindKey键对应的是提示文字类型 会影响localizedDescription的值 NSProgressFileOperationKindKey可选的对应值如下: NSProgressFileOperationKindDownloading: 显示Downloading files… NSProgressFileOperationKindDecompressingAfterDownloading: 显示Decompressing files… NSProgressFileOperationKindReceiving: 显示Receiving files… NSProgressFileOperationKindCopying: 显示Copying files… */ [progress setUserInfoObject:NSProgressFileOperationKindDownloading forKey:NSProgressFileOperationKindKey]; /* NSProgressFileTotalCountKey键设置显示的文件总数 例如:Copying 100 files… */ [progress setUserInfoObject:@100 forKey:NSProgressFileTotalCountKey]; //设置已完成的数量 [progress setUserInfoObject:@1 forKey:NSProgressFileCompletedCountKey];
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有