iPhone开发【十四】多线程开发之NSThread——子线程模拟耗时操作
来源:互联网 发布:七天网络阅卷成绩查询 编辑:程序博客网 时间:2024/05/21 08:54
转载请注明出处,原文网址:http://blog.csdn.net/m_changgong/article/details/8213964 作者:张燕广
实现的功能:1)演示多线程开发。2)子线程中模拟耗时操作,然后通知主线程更新进度条。
关键词:多线程 NSThread 定时器
1、新建视图控制器ViewController.m(不带xib),作为根视图控制器,通过ViewController的-(void)loadView方法构建UI,ViewController.h如下:
- #import <UIKit/UIKit.h>
- @interface ViewController : UIViewController{
- CGFloat progressValue; //进度条的值
- }
- @property(strong,nonatomic)UIProgressView *progress;
- @property(strong,nonatomic)UILabel *showValue;
- @property(strong,nonatomic)UIButton *startThread;
- @end
ViewController.m如下:
- #import "ViewController.h"
- @implementation ViewController
- @synthesize progress;
- @synthesize showValue;
- @synthesize startThread;
- -(void)loadView{
- CGRect appFrame = [UIScreen mainScreen].applicationFrame;
- UIView *view = [[UIView alloc]initWithFrame:appFrame];
- self.view = view;
- //设置背景颜色
- UIColor *bgcolor = [UIColor grayColor];
- [self.view setBackgroundColor:bgcolor];
- [self initViews];
- }
- //初始化视图组件
- -(void)initViews{
- //初始化progress
- CGRect frame = CGRectMake(50, 50, 200, 30);
- progress = [[UIProgressView alloc]initWithFrame:frame];
- [self.view addSubview:progress];
- //初始化showValue
- showValue = [[UILabel alloc]init];
- frame = showValue.frame;
- frame.origin.x = CGRectGetMaxX(progress.frame)+10;
- frame.origin.y = CGRectGetMinY(progress.frame);
- frame.size.width = 35;
- frame.size.height = 15;
- showValue.frame = frame;
- showValue.backgroundColor = [UIColor redColor];
- showValue.text = @"0.0";
- [self.view addSubview:showValue];
- //初始化startThread
- startThread = [[UIButton alloc]init];
- frame = startThread.frame;
- frame.origin.x = 110;
- frame.origin.y = 80;
- frame.size.width = 100;
- frame.size.height = 50;
- startThread.frame = frame;
- UIImage *img = [UIImage imageNamed:@"btn.png"];
- [startThread setBackgroundImage:img forState:UIControlStateNormal];
- [startThread setTitleColor:[UIColor colorWithRed:1.0 green:0 blue:0 alpha:1.0] forState:UIControlStateNormal];
- [startThread setTitle:@"开启子线程" forState:UIControlStateNormal];
- //设置事件
- [startThread addTarget:self action:@selector(startThreadButtonPressed) forControlEvents:UIControlEventTouchUpInside];
- [self.view addSubview:startThread];
- }
- //开启子线程
- -(void)startThreadButtonPressed{
- progress.progress = 0.0;
- showValue.text = @"0.0";
- startThread.hidden = YES;
- //该语句会让主线程堵塞2秒,这就是为什么要将耗时操作放在子线程的原因之一
- //[NSThread sleepForTimeInterval:2];
- //开启一个新线程
- [NSThread detachNewThreadSelector:@selector(doJobInBackground) toTarget:self withObject:nil];
- }
- //子线程,后台执行工作
- -(void)doJobInBackground{
- //睡眠,模拟子线程中耗时操作
- [NSThread sleepForTimeInterval:2];
- //通知主线程执行更新操作
- [self performSelectorOnMainThread:@selector(invalidateProgress) withObject:nil waitUntilDone:NO];
- }
- //更新进度
- -(void)invalidateProgress{
- progressValue = [progress progress];
- showValue.text = [NSString stringWithFormat:@"%.2f",progressValue];
- if(progressValue < 1.0){
- progress.progress = progressValue+0.02;
- //启动定时器
- [NSTimer scheduledTimerWithTimeInterval:0.2 target:self selector:@selector(invalidateProgress) userInfo:nil repeats:NO];
- }else{
- progress.progress = 1.0;
- showValue.text = @"1.0";
- startThread.hidden = NO;
- }
- }
- - (void)viewDidUnload
- {
- [super viewDidUnload];
- // Release any retained subviews of the main view.
- progress = nil;
- showValue = nil;
- startThread = nil;
- }
- - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
- {
- return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
- }
- @end
2、运行效果如下:
- iPhone开发【十四】多线程开发之NSThread——子线程模拟耗时操作
- iPhone开发【十四】多线程开发之NSThread——子线程模拟耗时操作
- 多线程开发之NSThread——子线程模拟耗时操作(★★★虽是转载,但是附上了个人理解,firecat推荐★★★)
- iPhone开发【十五】多线程开发之NSThread——异步下载图片
- iPhone开发【十五】多线程开发之NSThread——异步下载图片
- iOS开发——多线程、NSThread、gcd、线程间通信
- iOS开发多线程篇—NSThread及线程的状态
- iOS 开发 多线程详解之NSThread线程通信的操作与控制
- iphone开发中的多线程:NSThread、NSOperation、GCD
- iphone开发之多线程NSThread和NSInvocationOperation
- iphone开发之多线程NSThread和NSInvocationOperation
- iphone开发之多线程NSThread和NSInvocationOperation
- iphone开发之多线程NSThread和NSInvocationOperation
- iphone开发之多线程NSThread和NSInvocationOperation
- iphone开发之多线程NSThread和NSInvocationOperation
- iphone开发之多线程NSThread和NSInvocationOperation
- iphone开发之多线程NSThread和NSInvocationOperation
- iphone开发之多线程NSThread和NSInvocationOperation
- CCS编译链接库文件与头文件
- the operation could't be completed(webkitErrorDomain error 10
- 上百个Android开源项目分享
- Java中replace()、replaceFirst()和replaceAll()区别
- ThinkPHP学习笔记(十四)上传文件
- iPhone开发【十四】多线程开发之NSThread——子线程模拟耗时操作
- dlmalloc解析连载(2)
- linux 分析 ptrace()
- hibernate 配置连接oracle 11g数据库时Dialect的选择问题求教
- 前端开发工程师如何在2013年提升自己
- -[UIViewController _loadViewFromNibNamed:bundle:]
- Eclipse使用过程中出现java.lang.NoClassDefFoundError的解决方案
- Android APK反编译详解(附图)
- php中empty()和isset()、echo()和print()、require()和include()等易混淆函数的区别