zl程序教程

您现在的位置是:首页 >  Python

当前栏目

iOS进度管理NSProgress类简单实用实例

2023-03-15 23:21:30 时间

引言

iOS 7 之后苹果推出了一个新的类 NSProgress 专门用来管理进度,比如在下载数据或者执行任务的时候要显示进度,那么这个类就可以很方便地对进度进行管理,供开发者拿去控制表示进度的UI界面。

这个类的用法其实很简单,总结起来就是下面几个步骤:

  1. 创建实例,同时设定表示任务要完成的数量的总值(这个总值只是一个用来计算比例的概念);
  2. 配置KVO观察其进度的变化,从而对UI做出更新(NSProgress 类就是搭配 KVO 来操作的,关于 KVO 是什么可以看我这篇博客:iOS键值观察KVO实例详解);
  3. 执行任务,在执行过程中递增表示已完成任务量的属性(这个属性用来判断任务完成情况,供计算比例以及是否完成任务);
  4. 在 KVO 的响应方法中对UI进行更新操作,改变UI显示的当前进度值。

就这么多了,具体的用法我们举三个例子:单任务的执行、多任务的执行、以及 iOS 9 下新的多任务执行方法。

单任务执行

直接上代码:

@interface ViewController ()

@property (nonatomic, strong) NSProgress *progress;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor lightGrayColor];
    
    // 创建实例
    self.progress = [NSProgress progressWithTotalUnitCount:10];
    // 配置KVO观察
    [self.progress addObserver:self forKeyPath:@"fractionCompleted" options:NSKeyValueObservingOptionNew context:nil];

    // 每秒执行一次任务
    NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(doTask) userInfo:nil repeats:YES];
    
}

- (void)doTask {
    if (self.progress.completedUnitCount < self.progress.totalUnitCount) {
        self.progress.completedUnitCount ++;
    }
}

#pragma mark - KVO
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context {
    if ([keyPath isEqualToString:@"fractionCompleted"]) {
        NSLog(@"进度表示形式1:%f", self.progress.fractionCompleted);
        NSLog(@"进度表示形式2:%@", self.progress.localizedDescription);
        NSLog(@"进度表示形式3:%@", self.progress.localizedAdditionalDescription);
        NSLog(@"=============================");
    }
}

代码还是比较明了,先创建实例,设定总任务量为10,,然后配置KVO观察,使用NSTimer模拟了一个每秒执行一次任务的情况,在任务方法中,只要没做完任务就增加 completedUnitCount 属性,这个属性就表示当前已完成的任务量的。

在KVO的响应方法中,列出了三种进度表示方式,这都是原生支持的,分别对应三种显示效果,如下图:

多任务执行

所谓的多任务执行,是指 NSProgress 类可以添加多个子任务,分别占据主任务的一定比例,当然这个比例加起来是实例化时的总量,先看代码:

@interface ViewController ()

@property (nonatomic, strong) NSProgress *progress;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor lightGrayColor];
    
    self.progress = [NSProgress progressWithTotalUnitCount:10];
    [self.progress addObserver:self forKeyPath:@"fractionCompleted" options:NSKeyValueObservingOptionNew context:nil];

    // 第一个子任务
    [self.progress becomeCurrentWithPendingUnitCount:5];
    [self subTask];
    [self.progress resignCurrent];// 必须与 becomeCurrentWithPendingUnitCount: 方法成对
    
    // 第二个子任务
    [self.progress becomeCurrentWithPendingUnitCount:5];
    [self subTask];
    [self.progress resignCurrent];// 必须与 becomeCurrentWithPendingUnitCount: 方法成对
    
}

- (void)subTask {
    NSProgress *subProgress = [NSProgress progressWithTotalUnitCount:10];
    while (subProgress.completedUnitCount < subProgress.totalUnitCount) {
        subProgress.completedUnitCount ++;
    }
}

#pragma mark - KVO
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context {
    if ([keyPath isEqualToString:@"fractionCompleted"]) {
        NSLog(@"进度表示形式1:%f", self.progress.fractionCompleted);
        NSLog(@"进度表示形式2:%@", self.progress.localizedDescription);
        NSLog(@"进度表示形式3:%@", self.progress.localizedAdditionalDescription);
        NSLog(@"=============================");
    }
}

这里我们安排了两个子任务,所占的比例是五五开,要注意的是 becomeCurrentWithPendingUnitCount: 方法和 resignCurrent 必须成对出现,也就是说每个子任务都必须有,一个是创建子任务,一个是取消注册。在中间执行子任务,从子任务的方法中也可以看出,子任务其实是另外一个新的 NSProgress 实例,在进行自己的任务,当然也可以对其进行 KVO 观察,如有需要的话可以显示子任务的进度,不过这里我们就只要完成子任务就行了。任务进行的情况如下图,可以看到由于每个子任务都又安排了10的总量,而子任务占总任务的5/10,所以每次的进度是0.05:

iOS 9 多任务的新方式

其实上面这种多任务的开发方法很不方便,而且也不好理解,因此 iOS 9 提供了一种更加直观、简单的操作方式来进行多任务,代码如下:

@interface ViewController ()

@property (nonatomic, strong) NSProgress *progress;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor lightGrayColor];
    
    self.progress = [NSProgress progressWithTotalUnitCount:10];
    [self.progress addObserver:self forKeyPath:@"fractionCompleted" options:NSKeyValueObservingOptionNew context:nil];

    // 两个子任务
    NSProgress *sub1 = [NSProgress progressWithTotalUnitCount:10 parent:self.progress pendingUnitCount:4];
    NSProgress *sub2 = [NSProgress progressWithTotalUnitCount:10 parent:self.progress pendingUnitCount:6];
    for (int i = 0; i < 10; i++) {
        sub1.completedUnitCount ++;
        sub2.completedUnitCount ++;
    }
    
}

#pragma mark - KVO
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context {
    if ([keyPath isEqualToString:@"fractionCompleted"]) {
        NSLog(@"进度表示形式1:%f", self.progress.fractionCompleted);
        NSLog(@"进度表示形式2:%@", self.progress.localizedDescription);
        NSLog(@"进度表示形式3:%@", self.progress.localizedAdditionalDescription);
        NSLog(@"=============================");
    }
}

可以看到这种方法直观地多,只要直接创建子任务,设置 parent ,也就是父任务是谁,然后设置占任务的比例,就可以直接进行子任务了,这里的比例是四六开,同时子任务的总量也设为10,,因此两个子任务应该分别占父任务的0.04和0.06,而因为两个任务是同时进行的,因此效果如下:

以上,就是简单的三个实例了,其实用起来还是蛮方便的,比自己去创建变量来记录和计算要好多了,而且其用法还有很多,比如取消任务、暂停任务、继续任务、添加子节点、设置进度显示模式等等,有待在使用中慢慢摸索。


示例工程:https://github.com/Cloudox/NSProgressDemo