iOS进度指示器——NSProgress

一、引言

在iOS7之前,系统一直没有提供一个完整的框架来描述任务进度相关的功能。这使得在开发中进行耗时任务进度的监听将什么麻烦,在iOS7之后,系统提供了NSProgress类来专门报告任务进度。

二、创建单任务进度监听器

单任务进度的监听是NSProgress最简单的一种运用场景,我们来用定时器模拟一个耗时任务,示例代码如下:

@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;
} }

上面的示例代码中,fractionCompleted属性为0-1之间的浮点值,为任务的完成比例。NSProgress对象中还有两个字符串类型的属性,这两个属性将进度信息转化成固定的格式:

//显示完后比例 如:10% completed
@property (null_resettable, copy) NSString *localizedDescription;
//完成数量 如:1 of 10
@property (null_resettable, copy) NSString *localizedAdditionalDescription;

三、创建多任务进度监听器

上面演示了只有一个任务时的进度监听方法,实际上,在开发中,一个任务中往往又有许多子任务,NSProgress是以树状的结构进行设计的,其支持子任务的嵌套,示例如下:

- (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);
}

NSProgress的这种树状设计模式乍看起来确实有些令人费解,有一点需要注意,becomeCurrentWithPendingUnitCount:方法的意义是将此NSProgress对象注册为当前线程任务的根进度管理对象,resignCurrent方法为取消注册,这两个方法必须成对出现,当一个NSProgress对象被注册为当前线程的根节点时,后面使用类方法 progressWithTotalUnitCount:创建的NSProgress对象都默认作为子节点添加。

四、iOS9之后进行多任务进度监听的新设计方法

正如上面的例子所演示,注册根节点的方式可读性很差,代码结构也不太清晰,可能Apple的工程师们也觉得如此,在iOS9之后,NSProgress类中又添加了一些方法,通过这些方法可以更加清晰的表达进度指示器之间的层级结构,示例代码如下:

- (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

六、关于NSProgress对象的用户配置字典

在NSProgress对象的用户字典中可以设置一些特定的键值来进行显示模式的设置,示例如下:

//设置剩余时间 会影响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];

iOS进度指示器——NSProgress的更多相关文章

  1. 转 UIActivityIndicatorView、UIProgressView 活动与进度指示器-IOS开发

    活动指示器(UIActivityIndicatorView)可以告知用户有一个操作正在进行中.进度指示器(UIProgressView )也具有同样功能,而且还可以告知用户离操作结束还多远. 这两个指 ...

  2. iOS-swift环形进度指示器+图片加载动画

    demo.gif 如图,这个动画的是如何做的呢? 分析: 1.环形进度指示器,根据下载进度来更新它 2.扩展环,向内向外扩展这个环,中间扩展的时候,去掉这个遮盖 一.环形进度指示器 1.自定义View ...

  3. Flutter 基础组件:进度指示器

    前言 Material 组件库中提供了两种进度指示器:LinearProgressIndicator和CircularProgressIndicator,它们都可以同时用于精确的进度指示和模糊的进度指 ...

  4. iOS空心圆下载进度指示器控件

    self.layer = [CAShapeLayer layer]; self.layer.frame = CGRectMake(, , , ); self.layer.position = self ...

  5. SVProgressHUD–比MBProgressHUD更好用的 iOS进度提示组件

    简介 SVProgressHUD是简单易用的显示器,用于指示一个持续进行的任务的进度. 项目主页: SVProgressHUD 最新示例: 点击下载 快速入门 安装 通过Cocoapods pod ' ...

  6. iOS进度条显示

    一.实现下载文件进度控制 1.代码示例 1 #import "YYViewController.h" 2 3 @interface YYViewController () 4 @p ...

  7. IOS 进度条与手势

    //进度条#import "ViewController.h" @interface ViewController () { UIImageView* _animaImageV; ...

  8. iOS:进度条控件的详细使用

    进度条控件:UIProcessView:UIView   功能:顾名思义,用来显示下载进度或者传输数据进度.   属性: @property(nonatomic) UIProgressViewStyl ...

  9. IOS - 网络指示器

    #pragma mark Activity methods - (void)openActivity { // 添加网络指示器 activityIV = [[UIActivityIndicatorVi ...

随机推荐

  1. sleep与wait的区别,详细解答(通过代码验证)

    package com.ysq.test; /** * sleep与wait的区别: * @author ysq * */ public class SleepAndWait { public sta ...

  2. Java反射的理解

    反射的作用:   1.运行时检查类的结构 2.运行时更改类的字段值 3.调用类的方法   准备知识:   Class类:虚拟机为每一个对象保存的一份对象所属类的清单: static Class for ...

  3. [Quote]Creating basic Excel workbook with Open XML

    Creating basic Excel workbook with Open XML [Quote from]http://www.codeproject.com/Articles/371203/C ...

  4. unity 基础之InputManager

    unity  基础之InputManager 说一下unity中的InputManager,先截个图 其中Axes指的是有几个轴向!Size指的是有几个轴,改变Size可以添加或者减少轴! Name指 ...

  5. 用PYTHON输入输出字符串

    这段好懂的,可以互动. import sys import re class BadEmployeeFormat(Exception): """Badly formatt ...

  6. 【UVALive - 3211】Now or later (二分+2-SAT)

    题意: 有n架飞机需要着陆.每架飞机有两种选择,早着陆或者晚着陆,二选其一.现在为了保证飞机的着陆安全,要求两架着陆的飞机的时间间隔的最小值达到最大. 分析: 最小值最大问题我们想到二分答案.对于猜测 ...

  7. 子元素div高度不确定时父div高度如何自适应

    粘自:http://www.jb51.net/css/110652.html 在最外层div加以下样式 height:100%; overflow:hidden; 其它方法: Div即父容器不根据内容 ...

  8. KeilC51高级编程

    第一节 绝对地址访问 C51提供了三种访问绝对地址的方法: 1. 绝对宏: 在程序中,用“#include<absacc.h>”即可使用其中定义的宏来访问绝对地址,包括: CBYTE.XB ...

  9. 【POJ】1056 IMMEDIATE DECODABILITY

    字典树水题. #include <cstdio> #include <cstring> #include <cstdlib> typedef struct Trie ...

  10. (转载)javascript客户端生成MD5值的函数代码

    (转载)http://www.jb51.net/article/26223.htm 用js实现的客户端即可实现md5值的代码,一般情况下都是后台语言才有的,客户端也有了,方便有需要的朋友了. 测试代码 ...