iOS中使用NSProgress類來建立UI進度條的方法詳解_IOS

來源:互聯網
上載者:User

一、引言

在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屬性設定為 NSProgressKindFileNSProgressFileOperationKindKey鍵對應的是提示文字類型 會影響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];

七、在UI中顯示進度步驟總結
以下有幾個在視圖或者視圖控制器中顯示進度的步驟:
1.在你調用一個長時間啟動並執行任務之前,藉助+progressWithTotalUnitCount:.方法建立一個NSProgress執行個體。 參數totalUnitCount將會包括“要完成的總工作單元的數量”。
 
有一點很重要,要從UI圖層的角度完全理解這個數值;你不會被要求猜測有多少個實際工作對象以及有多少種類的工作單元(位元組?像素?文字行數?)。如果你遍曆集合并且計劃為每一個集合元素調用該執行個體對象,該參數經常會是1或者也許是一個集合中的元素的數量 。
 
2.使用KVO註冊一個進度的fractionCompleted屬性的觀察者。類似於NSOperation,NSProgress被設計藉助KVO來使用。在MAC,這使得通過Cocoa Bindings綁定一個NSProgress執行個體到一個進度條或者標籤上變得非常容易。在iOS上,你將會在KVO observer handle中手動更新你的UI。
 
除了fractionCompleted, completedUnitCount和totalUnitCount屬性之外,NSProgress也有一個localizedDescription (@"50% completed"),並且還有一個localized Additional Description (@"3 of 6"),其能夠被綁定到文字標籤。KVO通知在改變NSProgress對象屬性值的線程中發送,因此確保在你的主線程中手動更新UI。
 
3.當前的進度對象通過調用-becomeCurrentWithPendingUnitCount:方法建立新的進度對象。在這裡,pendingUnitCount這個參數相當於“是要被接收者完成的總的工作單元的量要完成的工作的一部分”。你可以多次調用這個方法並且每次傳遞totalUnitCount(本次程式碼完成的佔比)的一部分。在集合元素的迭代樣本中,我們將會在每一次迭代中調用[progress becomeCurrentWithPendingUnitCount:1];
 
4.調用工作對象的方法。由於當前進度是一個局部線程概念,你必須在你調用becomeCurrentWithPendingUnitCount:的相同的線程中做這個事情。如果工作對象的API被設計成在主線程中調用,那這就不是一個問題,就像我對大部分API的看法那樣(Brent Simmons 也這麼認為)。
 
但是如果你的UI 層正在建立一個後台隊列並且調用工作對象來同步那個隊列,那要確保將 becomeCurrentWithPendingUnitCount:和resignCurrent放到相同的dispatch_async()塊中調用。
 
5.在你的進度對象中調用-resignCurrent。這個方法是和-becomeCurrentWith PendingUnitCount:相對應的,並且會調用相同的次數 。你可以在實際工作被完成以前調用resignCurrent,因此你不需要等待,直到你得到一個來自工作對象的完成通知。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.