iPhone开发【十四】多线程开发之NSThread——子线程模拟耗时操作
ViewController.m如下:
2、运行效果如下:
发布日期:2021-09-28 18:46:29
浏览次数:14
分类:技术文章
本文共 3300 字,大约阅读时间需要 11 分钟。
转载请注明出处,原文网址: 作者:张燕广
实现的功能: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、运行效果如下:
转载地址:https://blog.csdn.net/h3c4lenovo/article/details/8563830 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
做的很好,不错不错
[***.243.131.199]2024年04月09日 10时27分26秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
Unity打开照相机与打开本地相册然后在Unity中显示照片(Android与iOS)
2021-06-30
无需接入SDK即可在Unity中获取经纬度(Android/iOS),告诉我你的坐标
2021-06-30
Unity获取系统信息SystemInfo(CPU、显卡、操作系统等信息)
2021-06-30
Unity中获取物体的尺寸(size)的三种方法
2021-06-30
Unity中的关节组件和绳子效果的实现
2021-06-30
Unity可视化编程插件: Bolt,可以像UE4的蓝图那样啦
2021-06-30
Android的.dex、.odex与.oat文件扫盲
2019-04-27
Unity移动应用如何在Bugly上查看崩溃堆栈
2019-04-27
unity3D 在屏幕边框创建碰撞框
2019-04-27
xml中常用的转义符
2019-04-27
关于MSDK的几个难点
2019-04-27
使用UnityEditor做工具
2019-04-27
Visual Studio我常用的快捷键
2019-04-27
写C# dll供Unity调用
2019-04-27
Linux制作run安装包
2019-04-27
一分钟学会C#解析XML
2019-04-27
unity AssetBundle的资源管理
2019-04-27
【转】Unity中HideInInspector和SerializeField一起使用
2019-04-27
单例模板类
2019-04-27