ios線程練習更新進度條

來源:互聯網
上載者:User

標籤:ios   ios開發   線程   多線程   編程   

類似於,我們載入網頁時候的進度條,我們來看看它們是怎麼工作的。

#import "ViewController.h"


@interface ViewController ()

{

    UIProgressView *_view;

}

@end


@implementation ViewController


- (void)viewDidLoad {

    [superviewDidLoad];

  

    

    //設定進度條

    _view = [[UIProgressViewalloc]init];

        _view.frame =CGRectMake(20,100, 300,3);

    _view.progress =0;

    [self.viewaddSubview:_view];

    

    

    

    UIButton *btn = [[UIButtonalloc]init];

    btn.frame =CGRectMake(60,150, 200,20);

    [btn setTitle:@"點擊更新"forState:UIControlStateNormal];

    btn.backgroundColor = [UIColorredColor];

    [btn addTarget:selfaction:@selector(btnClick:)forControlEvents:UIControlEventTouchUpInside];

    [self.viewaddSubview:btn];

    

    

    

}


//在子線程中更新進度條

-(void)btnClick:(UIButton *)btn

{

    

    //表現在幕後

    [selfperformSelectorInBackground:@selector(updateProgressView)withObject:nil];

    

}



-(void)updateProgressView

{

    

    while (1) {

        if (_view.progress<1) {

            //每次累加0.0001的值

            NSNumber *number = [NSNumbernumberWithFloat:_view.progress+0.0001];

            [selfperformSelectorOnMainThread:@selector(setProgressViewProgress:)withObject:number waitUntilDone:NO];

            

            //延遲0.0005

            [NSThreadsleepForTimeInterval:0.0005];

        }

        else//否則結束

        {

            

            break;

        }

    }

    

    NSLog(@"1111");

    

    

}



//更新進度條()

-(void)setProgressViewProgress:(NSNumber *)number

{

    //給progressview設定數值

    [_viewsetProgress:[number floatValue]];

}


@end



今天2更了,明天繼續。求知若饑,虛心若愚。這幾天找工作找的蠻辛苦的,在大上海不容易啊,明天就要去面試了,估計一天都在外面跑,只有晚上有時間能給大家寫東西,我會保持下去的,同時保證品質,當然如果有錯誤的話,大家可以明確指出來,我可以修改。嘻嘻,祝大家美夢,晚安。


ios線程練習更新進度條

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.