7.19
进程和线程.
//进程:一个运行中的应用程序,就一个进程,他是资源分配的单位,譬如地址空间,系统自动生成主线程
//线程:是cpu分配的基本单位,一个进程运行后系统自动分配一个主线程,你可以手动生成子线程,多个子线程运行在进程的地址空间中,一个线程必须依附于一个进程,多线程的好处,可以防止主界面卡死,还可以同时处理多个事情。
1.通过路径获取图片
//NSBundle应用程序束,其实就是一个目录,存放的是导入资源
//每个应用程序有一个主应用程序束,可以通过[NSBundle mainBundle]获取
NSBundle* bundle = [NSBundle mainBundle];
//通过应用程序束获取资源的路径
NSString* path = [bundle pathForResource:@"b2" ofType:@"png"];
NSLog(@"%@",path);
//通过路径,获取UIImage
UIImage* image = [UIImage imageWithContentsOfFile:path];
//通过[UIImage imageNamed]会缓存图片而通过路径不会缓存
UIImageView* bombImgView = [[UIImageView alloc]initWithImage:image];
============================================================================================================
2.线程和进程
- (void)viewDidLoad {
[super viewDidLoad];
UIButton* button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button1 setTitle:@"复杂运算" forState:UIControlStateNormal];
button1.frame = CGRectMake(10, 30, 60, 39);
[button1 addTarget:self action:@selector(bigCalucate) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button1];
UIButton* button2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button2 setTitle:@"normal" forState:UIControlStateNormal];
button2.frame = CGRectMake(10, 100, 60, 40);
[button2 addTarget:self action:@selector(normalButtonClick) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button2];
}
//进程:一个运行中的应用程序,就一个进程,他是资源分配的单位,譬如地址空间,系统自动生成主线程
//线程:是cpu分配的基本单位,一个进程运行后系统自动分配一个主线程,你可以手动生成子线程,多个子线程运行在进程的地址空间中,一个线程必须依附于一个进程,多线程的好处,可以防止主界面卡死,还可以同时处理多个事情。
-(void)bigCalucate
{
//while (1) {
// NSLog(@"fff");
// }
//Thread 线程
//通过 NSThread detachNewThreadSelector 生成一个线程
//线程函数:就是线程启动后运行的函数,这里就是caluateInThread
//detach 分离 New Thread
[NSThread detachNewThreadSelector:@selector(caluateInThread:) toTarget:self withObject:@"abc"];
}
-(void)normalButtonClick
{
NSLog(@"我要cpu,让我执行");
}
-(void)caluateInThread:(id)parameter
{
NSLog(@"%@",parameter);
while (1) {
//NSLog(@"大数据量运算");
}
}
============================================================================================================
3.线程实现椭圆运动,界面更新一般在主线程,在子线程里会发生未知情况
- (void)viewDidLoad {
[super viewDidLoad];
movedImageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"b2.png"]];
movedImageView.frame = CGRectMake(0,0,IMG_WIDHT,IMG_HEIGHT);
[self.view addSubview:movedImageView];
//NSThread* thread = [[NSThread alloc]initWithTarget:self selector:@selector(onThread) object:nil];
// [thread start];
TestObject* test = [[TestObject alloc]initWithName:@"小强"];
//withObject 出入一个对象,可以是数组,改对象可以在子线程中获取
[NSThread detachNewThreadSelector:@selector(onThread:) toTarget:self withObject:test];
//sleepForTimeInterval 让线程睡眠5秒
//NSLog(@"before-------------");
// [NSThread sleepForTimeInterval:5];
// NSLog(@"after------------");
}
-(void)onThread:(id)parameter
{
TestObject* test = (TestObject*)parameter;
NSLog(@"%@",test.strName);
while (1) {
angle += 5;
xPos = CENTER_X + LONG_AXIS*cos(ANGLE2RADIO(angle));
yPos = CENTER_Y + SHORT_AXIS*sin(ANGLE2RADIO(angle));
NSLog(@"(%d,%d)",xPos,yPos);
//更改界面的操作要放到主线程中
//perform 执行
//selector 方法
//OnMainThread 在主线程
//waitUntilDone 是否等到主线程中的操作执行完毕,即UpdateUI执行完毕
[self performSelectorOnMainThread:@selector(updateUI) withObject:nil waitUntilDone:YES];
//movedImageView.center = CGPointMake(xPos, yPos);
//让当前线程睡眠0.2秒
[NSThread sleepForTimeInterval:0.2];
}
}
-(void)updateUI
{
movedImageView.center = CGPointMake(xPos, yPos);
}
============================================================================================================
4.利用计时器实现View的椭圆运动
#define IMG_WIDTH 50
#define IMG_HEIGHT 50
#define CENTER_X 160
#define CENTER_Y 230
#define LONG_AXIS (160-25)
#define SHORT_AXIS 80
#define ANGLE2RADIO(x) (x*M_PI/180)
movedImgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"b2.png"]];
movedImgView.frame = CGRectMake(0, 0, IMG_WIDTH, IMG_HEIGHT);
[self.view addSubview:movedImgView];
//定时器需要等0.2秒后才触发
[NSTimer scheduledTimerWithTimeInterval:0.2 target:self selector:@selector(onTimer) userInfo:nil repeats:YES];
//使用fire 马上触发定时器
//NSTimer * timer = [NSTimer scheduledTimerWithTimeInterval:0.2 target:self selector:@selector(onTimer) userInfo:nil repeats:YES];
//[timer fire];
-(void)onTimer
{
angle += 5;
int xPos = CENTER_X + LONG_AXIS*cos(ANGLE2RADIO(angle));
int yPos = CENTER_Y + SHORT_AXIS*sin(ANGLE2RADIO(angle));
移动的X坐标等于 中心x坐标+ 横轴/2- W/2 *cos(移动坐标*M_PI/180);
移动的Y坐标等于 中心Y坐标+ 竖轴/2- H/2*sin(移动坐标*M_PI/180);
movedImgView.center = CGPointMake(xPos, yPos);
}
进程和线程.
//进程:一个运行中的应用程序,就一个进程,他是资源分配的单位,譬如地址空间,系统自动生成主线程
//线程:是cpu分配的基本单位,一个进程运行后系统自动分配一个主线程,你可以手动生成子线程,多个子线程运行在进程的地址空间中,一个线程必须依附于一个进程,多线程的好处,可以防止主界面卡死,还可以同时处理多个事情。
1.通过路径获取图片
//NSBundle应用程序束,其实就是一个目录,存放的是导入资源
//每个应用程序有一个主应用程序束,可以通过[NSBundle mainBundle]获取
NSBundle* bundle = [NSBundle mainBundle];
//通过应用程序束获取资源的路径
NSString* path = [bundle pathForResource:@"b2" ofType:@"png"];
NSLog(@"%@",path);
//通过路径,获取UIImage
UIImage* image = [UIImage imageWithContentsOfFile:path];
//通过[UIImage imageNamed]会缓存图片而通过路径不会缓存
UIImageView* bombImgView = [[UIImageView alloc]initWithImage:image];
============================================================================================================
2.线程和进程
- (void)viewDidLoad {
[super viewDidLoad];
UIButton* button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button1 setTitle:@"复杂运算" forState:UIControlStateNormal];
button1.frame = CGRectMake(10, 30, 60, 39);
[button1 addTarget:self action:@selector(bigCalucate) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button1];
UIButton* button2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button2 setTitle:@"normal" forState:UIControlStateNormal];
button2.frame = CGRectMake(10, 100, 60, 40);
[button2 addTarget:self action:@selector(normalButtonClick) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button2];
}
//进程:一个运行中的应用程序,就一个进程,他是资源分配的单位,譬如地址空间,系统自动生成主线程
//线程:是cpu分配的基本单位,一个进程运行后系统自动分配一个主线程,你可以手动生成子线程,多个子线程运行在进程的地址空间中,一个线程必须依附于一个进程,多线程的好处,可以防止主界面卡死,还可以同时处理多个事情。
-(void)bigCalucate
{
//while (1) {
// NSLog(@"fff");
// }
//Thread 线程
//通过 NSThread detachNewThreadSelector 生成一个线程
//线程函数:就是线程启动后运行的函数,这里就是caluateInThread
//detach 分离 New Thread
[NSThread detachNewThreadSelector:@selector(caluateInThread:) toTarget:self withObject:@"abc"];
}
-(void)normalButtonClick
{
NSLog(@"我要cpu,让我执行");
}
-(void)caluateInThread:(id)parameter
{
NSLog(@"%@",parameter);
while (1) {
//NSLog(@"大数据量运算");
}
}
============================================================================================================
3.线程实现椭圆运动,界面更新一般在主线程,在子线程里会发生未知情况
- (void)viewDidLoad {
[super viewDidLoad];
movedImageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"b2.png"]];
movedImageView.frame = CGRectMake(0,0,IMG_WIDHT,IMG_HEIGHT);
[self.view addSubview:movedImageView];
//NSThread* thread = [[NSThread alloc]initWithTarget:self selector:@selector(onThread) object:nil];
// [thread start];
TestObject* test = [[TestObject alloc]initWithName:@"小强"];
//withObject 出入一个对象,可以是数组,改对象可以在子线程中获取
[NSThread detachNewThreadSelector:@selector(onThread:) toTarget:self withObject:test];
//sleepForTimeInterval 让线程睡眠5秒
//NSLog(@"before-------------");
// [NSThread sleepForTimeInterval:5];
// NSLog(@"after------------");
}
-(void)onThread:(id)parameter
{
TestObject* test = (TestObject*)parameter;
NSLog(@"%@",test.strName);
while (1) {
angle += 5;
xPos = CENTER_X + LONG_AXIS*cos(ANGLE2RADIO(angle));
yPos = CENTER_Y + SHORT_AXIS*sin(ANGLE2RADIO(angle));
NSLog(@"(%d,%d)",xPos,yPos);
//更改界面的操作要放到主线程中
//perform 执行
//selector 方法
//OnMainThread 在主线程
//waitUntilDone 是否等到主线程中的操作执行完毕,即UpdateUI执行完毕
[self performSelectorOnMainThread:@selector(updateUI) withObject:nil waitUntilDone:YES];
//movedImageView.center = CGPointMake(xPos, yPos);
//让当前线程睡眠0.2秒
[NSThread sleepForTimeInterval:0.2];
}
}
-(void)updateUI
{
movedImageView.center = CGPointMake(xPos, yPos);
}
============================================================================================================
4.利用计时器实现View的椭圆运动
#define IMG_WIDTH 50
#define IMG_HEIGHT 50
#define CENTER_X 160
#define CENTER_Y 230
#define LONG_AXIS (160-25)
#define SHORT_AXIS 80
#define ANGLE2RADIO(x) (x*M_PI/180)
movedImgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"b2.png"]];
movedImgView.frame = CGRectMake(0, 0, IMG_WIDTH, IMG_HEIGHT);
[self.view addSubview:movedImgView];
//定时器需要等0.2秒后才触发
[NSTimer scheduledTimerWithTimeInterval:0.2 target:self selector:@selector(onTimer) userInfo:nil repeats:YES];
//使用fire 马上触发定时器
//NSTimer * timer = [NSTimer scheduledTimerWithTimeInterval:0.2 target:self selector:@selector(onTimer) userInfo:nil repeats:YES];
//[timer fire];
-(void)onTimer
{
angle += 5;
int xPos = CENTER_X + LONG_AXIS*cos(ANGLE2RADIO(angle));
int yPos = CENTER_Y + SHORT_AXIS*sin(ANGLE2RADIO(angle));
移动的X坐标等于 中心x坐标+ 横轴/2- W/2 *cos(移动坐标*M_PI/180);
移动的Y坐标等于 中心Y坐标+ 竖轴/2- H/2*sin(移动坐标*M_PI/180);
movedImgView.center = CGPointMake(xPos, yPos);
}