美文网首页
NSThread使用

NSThread使用

作者: cz3w | 来源:发表于2016-03-24 20:24 被阅读20次

- (void)viewDidLoad {

    [super viewDidLoad];

    self.view.backgroundColor = [UIColor blackColor];

    UIButton * btnStart = [[UIButton alloc]initWithFrame:CGRectMake(20, 20, 300, 40)];

    [btnStart setTitle:@"START" forState:UIControlStateNormal];

    [self.view addSubview:btnStart];

    self.actView = [[ UIActivityIndicatorView alloc]initWithFrame:

                                                                        CGRectMake(200, 100, 50, 50)];

   [self.view addSubview:self.actView];

   self.actView.activityIndicatorViewStyle = 

                                                                UIActivityIndicatorViewStyleWhiteLarge;

   self.actView.hidesWhenStopped = NO;

   [btnStart addTarget:self action:@selector(bigTaskAction) 

                          forControlEvents:UIControlEventTouchUpInside];

   self.aProgressView = [[UIProgressView alloc]initWithFrame:

                                                                          CGRectMake(20, 500, 300, 20)];

   [self.view addSubview:self.aProgressView];

}

-(void)bigTaskAction{

    [self.actView startAnimating];

    [NSThread detachNewThreadSelector:@selector(bigTask) toTarget:self withObject:nil];

//[self bigTask];

}

-(void)bigTask{

  @synchronized(self) {

    int updateUIWhen = 2000;

    for (int i=0; i<50000; i++) {

      NSString * newString =[ NSString stringWithFormat:@"i = %i",i];

      NSLog(@"%@",newString);

      if (i==updateUIWhen) {

          float f = (float)i/50000;

          NSNumber *percentDone = [NSNumber numberWithFloat:f];

          [self performSelectorOnMainThread:

                         @selector(updateProgressViewWithPercentage:) 

                         withObject:percentDone   waitUntilDone:YES];

          updateUIWhen = updateUIWhen +2000;

       }

}

      [self performSelectorOnMainThread:

                         @selector(updateProgressViewWithPercentage:) 

                         withObject:[NSNumber numberWithFloat:1.0] waitUntilDone:YES];

       [self.actView stopAnimating];

  }

}

-(void)updateProgressViewWithPercentage:(NSNumber *)percentDone {

      [self.aProgressView setProgress:[percentDone floatValue] animated:YES];

}

相关文章

  • iOS 多线程

    NSThread 使用NSThread对象建立一个线程非常方便,但要使用NSThread管理多个线程较困难,不推荐...

  • iOS_多线程

    NSThread NSThread是轻量级的多线程开发,使用起来也并不复杂,但是使用NSThread需要自己管理线...

  • iOS 多线程(一)---> NSThread

    NSThread的使用 No.1:NSThread创建线程 NSThread有三种创建方式: init方式 det...

  • iOS 多线程

    iOS使用线程的方式 pthread NSThread GCD NSOperation NSThread线程的创建...

  • 1.2、iOS面试题之多线程

    1.简述GCD,NSThread,NSOperation使用,各自优缺点 NSThread: –优点:NSThre...

  • NSThread使用

    - (void)viewDidLoad { [super viewDidLoad]; self.view....

  • IOS多线程的方式

    pthread的基本使用(需要包含头文件) 需要#import 3 NSThread (1)NSThread的基本...

  • 多线程之NSThread

    NSThread的使用: 全部API介绍

  • IOS面试集锦

    1、NSThread/NSOperation/GCD 三种多线程不同,分别使用场景? •NSThread: –优点...

  • iOS多线程与网络(1)--基本概念

    1 基本概念 2 pthread 3 NSThread (1)NSThread的基本使用 (2)设置线程的属性 (...

网友评论

      本文标题:NSThread使用

      本文链接:https://www.haomeiwen.com/subject/umvrlttx.html