iOS Progress indicator--nsprogress

Source: Internet
Author: User

iOS progress indicator--nsprogress One, introduction

Prior to IOS7, the system had not provided a complete framework to describe the task's progress-related functions. This makes it cumbersome to listen for time-consuming tasks in development, and after iOS7, the system provides a nsprogress class to specifically report on the progress of the task.

Second, create a single task Progress listener

Single-Task Progress monitoring is the simplest application scenario for nsprogress, and we use timers to simulate a time-consuming task with the following example code:

@interface Viewcontroller () {nsprogress * progress;} @end @implementation viewcontroller-(void) Viewdidload {[Super viewdidload];Additional setup after loading the view, typically from a nib.This method creates the task progress management Object Unitcount is a unit number based on the full task on the UI progress = [Nsprogress Progresswithtotalunitcount:10]; Nstimer * timer = [Nstimer scheduledtimerwithtimeinterval:1 target:self selector: @selector ( Task) Userinfo:nil Repeats:yes]; //the completion ratio of the task progress object [Progress Addobserver:self forkeypath:@void) Observevalueforkeypath: (NSString *) KeyPath Ofobject: (ID) object change: ( Nsdictionary *) Change context: (void *) context{NSLog (@ "progress =%f" , progress.fractioncompleted);} -(void) task{//complete task Unit number +1 if ( Progress.completedunitcount<progress.totalunitcount) {Progress.completedunitcount + = 1;} }

In the example code above, the Fractioncompleted property is a floating-point value between 0-1 and the completion scale for the task. There are also two string-type properties in the Nsprogress object that convert progress information to a fixed format:

//显示完后比例 如:10% completed@property (null_resettable, copy) NSString *localizedDescription;//完成数量 如:1 of 10@property (null_resettable, copy) NSString *localizedAdditionalDescription;
Iii. creating a multi-tasking progress listener

The above shows a progress monitoring method with only one task, in fact, in development, a task often has many subtasks, nsprogress is designed in a tree-like structure, which supports nesting of subtasks, examples are as follows:

- (void) Viewdidload {[Super viewdidload];Additional setup after loading the view, typically from a nib.This method creates the task progress management Object Unitcount is a unit number based on the full task on the UI progress = [Nsprogress Progresswithtotalunitcount:10];Listen for the completion scale of the task progress object [Progress Addobserver:self forkeypath:@"Fractioncompleted" Options:nskeyvalueobservingoptionnew Context:nil];Pay down a subtask subtask progress totals 5 cells that is, when a subtask completes, the parent Progerss object progresses 5 units [Progress Becomecurrentwithpendingunitcount:5]; [Self subtaskone]; [Progress Resigncurrent]; //down 2nd sub-task [Progress Becomecurrentwithpendingunitcount:5] ; [Self subtaskone]; [Progress Resigncurrent];} -(void) subtaskone{//sub-task Total 10 units 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)}            

At first glance, this tree-like design of nsprogress seems a bit confusing, and one thing to be aware of, Becomecurrentwithpendingunitcount: The meaning of the method is to register this Nsprogress object as the root progress management object of the current thread task, and the Resigncurrent method is to unregister, the two methods must appear in pairs, and when a Nsprogress object is registered as the root node of the current thread, the class method is used later Progresswithtotalunitcount: The Nsprogress objects created are added by default as child nodes.

A new design method of multi-task progress monitoring after iOS9

As shown in the example above, registering the root node is poorly readable, the code structure is not very clear, and perhaps Apple's engineers also feel that, after IOS9, the Nsprogress class adds a few more ways to express the hierarchy between progress indicators more clearly. The sample code is as follows:

- (void) Viewdidload {[Super viewdidload];Additional setup after loading the view, typically from a nib.//This method creates the task progress management Object Unitcount is a unit number based on the full task on the UI progress = [nsprogress Progresswithtotalunitcount:10]; //monitor the completion ratio of the task progress object [Progress Addobserver:self forkeypath:@"fractioncompleted" options: Nskeyvalueobservingoptionnew Context:nil]; //Create child node nsprogress * Sub = [nsprogress progresswithtotalunitcount:parent:progress pendingunitcount:5]; nsprogress * Sub2 = [nsprogress progresswithtotalunitcount:parent:progress pendingunitcount:5]; for (int i=0; i<; i++) {sub.completedunitcount + +; Sub2.completedunitcount + +;}}   

As the code above shows, the code structure becomes clearer and more actionable.

51 Points Small Summary
Gets the progress management object root node of the current threadNote: This method can obtain the + (nullable nsprogress *) currentprogress when the Nsprogress object calls the Becomecurrentwithpendingunitcount: method;Create a Nsprogress object, the number of units that need to pass in progress + (nsprogress *)Progresswithtotalunitcount: (int64_t) Unitcount;And the previous method function similar to the new method after IOS9 + (nsprogress *)Discreteprogresswithtotalunitcount: (int64_t) Unitcount;New method After iOS9 creates a child node of a progress indicator node + (nsprogress *)Progresswithtotalunitcount: (int64_t) UnitcountParent: (nsprogress *) ParentPendingunitcount: (int64_t) Portionofparenttotalunitcount;Nsprogress instance initialization method from parent node parameter can be nil-(instancetype)Initwithparent: (Nullable nsprogress *) ParentprogressornilUserInfo: (Nullable nsdictionary *) Userinfoornil;Register as the current line Chenggen node-(voidBecomecurrentwithpendingunitcount: (int64_t) Unitcount;Cancellation and registration methods must appear synchronously-(void) resigncurrent;IOS9 A new method adds a child node to a node-(voidAddChild: (nsprogress *) Childwithpendingunitcount: (int64_t) Inunitcount; //progress Unit total  @property int64_t Totalunitcount;//The number of progress units completed  @property int64_t completedunitcount; Can I cancel  @property (getter=iscancellable) BOOL cancellable; Can I pause  @property (getter=ispausable) BOOL pausable; Progress ratio 0-1  @property (readonly) double fractioncompleted; //Cancel-(void) cancel; Pause-(void) pause; Resume-(void) resume          
Vi. User Configuration dictionaries for nsprogress objects

In the user dictionary of the Nsprogress object, you can set some specific key values for the display mode setting, as shown in the following example:

Setting the remaining time affects the value of localizedadditionaldescription/* For example: 0 of 10-about seconds remaining*/[progresssetuserinfoobject:@10Forkey:nsprogressestimatedtimeremainingkey];Setting the completion speed information affects the value of the Localizedadditionaldescription/* For example: Zero KB of ten bytes (bytes/sec) */[progresssetuserinfoobject:@15Forkey:nsprogressthroughputkey];/* The following key values must be in effect to set the kind property of the Nsprogress object to the Nsprogresskindfilensprogressfileoperationkindkey key corresponding to the prompt text type The value that affects localizeddescription Nsprogressfileoperationkindkey optional corresponding value is as follows: nsprogressfileoperationkinddownloading: Show downloading Files ... Nsprogressfileoperationkinddecompressingafterdownloading: Show decompressing files ... Nsprogressfileoperationkindreceiving: Show receiving files ... Nsprogressfileoperationkindcopying: Show copying files...*/[Progresssetuserinfoobject:nsprogressfileoperationkinddownloading Forkey:nsprogressfileoperationkindkey]; /*nsprogressfiletotalcountkey Key Sets the total number of files displayed for example: Copying files...*/[Progress setuserinfoobject:@  Forkey:nsprogressfiletotalcountkey]; //Set the number of completed [Progress setuserinfoobject:@1 forkey:nsprogressfilecompletedcountkey];    

iOS Progress indicator--nsprogress

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.