iOS Thread practice Update progress bar

Source: Internet
Author: User

Similar to the progress bar when we load the page, let's see how they work.

#import "ViewController.h"


@interface viewcontroller ()

{

uiprogressview *_view;

}

@end


@implementation Viewcontroller


-(void) viewdidload {

[superviewdidload];

Set the progress bar

_view = [[uiprogressviewalloc] Init];

_view. Frame = CGRectMake ( 3);

_view. Progress = 0 ;

[self. View addsubview:_view];

UIButton *btn = [[UIButtonalloc] Init ];

btn. Frame = CGRectMake ( a);

[btn settitle:@ " click to update "forstate :uicontrolstatenormal];

btn. BackgroundColor = [uicolorredcolor];

[btn addTarget:selfAction:@selector (Btnclick:) forcontrolevents : UIControlEventTouchUpInside ];

[self. View addsubview: btn];

}


// update a progress bar in a child thread

-(void) Btnclick: ( UIButton *) btn

{

// show in the background

[selfperformselectorinbackground:@selector (Updateprogressview) Withobject : Nil ];

}



-(void) Updateprogressview

{

while (1) {

if (_view. Progress < 1 ) {

// 0.0001 value of each increment

nsnumber *number = [nsnumbernumberwithfloat: _view. Progress + 0.0001 ];

[selfperformselectoronmainthread:@selector(setprogre Ssviewprogress:)withobject: number Waituntildone:NO];

// delay 0.0005

[nsthreadsleepfortimeinterval:0.0005];

}

Else// End otherwise

{

break;

}

}

NSLog(@ "1111");

}



// Update progress bar ()

-(void) setprogressviewprogress: ( NSNumber *) number

{

// set a value for Progressview

[_viewsetprogress: [ number floatvalue]];

}


@end



Today 2 more, continue tomorrow. If you are hungry for knowledge, be foolish in modesty. These days to find a job to find a pretty hard, in the big Shanghai is not easy Ah, tomorrow is going to interview, an estimated day in the outside to run, only at night sometimes can write things to everyone, I will maintain, at the same time to ensure the quality, of course, if there are errors, you can clearly point out, I can modify. Hehe, I wish you all a good dream.


iOS Thread practice Update progress bar

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.