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