多线程问题 ----- UI视图 & NSTimer 同时执行操作
2014-11-26 13:37
369 查看
// // RootViewController.m // 多线程- // // Created by zm on 14-11-26. // Copyright (c) 2014年 practice. All rights reserved. // #import "RootViewController.h" @interface RootViewController ()<NSURLConnectionDataDelegate> { UIProgressView *_progressView; } @end @implementation RootViewController - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { } return self; } /** 默认情况下: * scrollView 拖动时, NSTimer 暂停线程操作,停止计时 移开鼠标后,继续线程操作,继续计时 * 原因: 事件源有三种(界面触摸,网络,定时器),界面触摸优先级最高 界面触摸事件处理的优先级要高于定时器线程的优先级 * */ -(void)prepareScrollView { UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 100, 320, 100)]; [self.view addSubview:scrollView]; for (int i = 0; i < 3; i++) { float x = i*320; float y = 0; UIView *view = [[UIView alloc] initWithFrame:CGRectMake(x, y, 320, 40)]; float radomColor = arc4random()%255/255.0; UIColor *color = [UIColor colorWithRed:radomColor green:radomColor blue:radomColor alpha:1.0]; view.backgroundColor = color; [scrollView addSubview:view]; } scrollView.contentSize = CGSizeMake(320*3, 0); NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(testTimer) userInfo:nil repeats:YES]; [timer fire]; /** * scroollview NSTimer 共存的原理: 两个事件源共用一个 runLoop 修改timer runLoop 模式,则能共存,互不影响,同时进行 [主线程,UI线程的runloop 模式不可更改!] * NSRunLoop 主要有两种模式 NSDefaultRunLoopMode, */ [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes]; } -(void)testTimer { static int count = 1; NSLog(@"定时器运行:%d",count); } - (void)viewDidLoad { [super viewDidLoad]; [self prepareScrollView]; } @end
相关文章推荐
- 多线程问题 ----- UI视图 & 网络下载 同时执行操作
- SQLite并发操作下的分析与处理,解决database is locked,以及多线程下执行事务等问题
- 腾讯云图片鉴黄集成到C# SQL Server 怎么在分页获取数据的同时获取到总记录数 sqlserver 操作数据表语句模板 .NET MVC后台发送post请求 百度api查询多个地址的经纬度的问题 try{}里有一个 return 语句,那么紧跟在这个 try 后的 finally {}里的 code 会 不会被执行,什么时候被执行,在 return 前还是后? js获取某个日期
- 多线程问题,三个线程轮流进行,每个线程进行n次操作,共执行n*n*3次操作。本代码用三军攻击做演示,通俗易懂。
- 关于"试图执行系统不支持的操作"问题
- layerUI+Ajax打开页面执行操作后关闭问题
- QT类学习系列(6)- Qt多线程的简单实现以及不能同时处理UI的操作
- (委托事件处理)关于多线程执行显示进度条的实例(转)&&线程间操作无效: 从不是创建控件“rtxtEntryNO”的线程访问它。
- 关于多线程中涉及UI操作,导致UI线程阻塞问题
- Java Hashmap多线程同时操作导致的问题
- ORACLE中表,视图名变大写和BLOB操作的问题
- 解决大量TCPIP连接后出现“因为系统缺乏足够缓冲区空间或者因为队列已满无法执行套接字上操作”的问题
- 更新ADT遇到问题,requires plug-in "org.eclipse.wst.sse.ui"
- C# SQL语句执行时间过长在操作完成之前超时时间已过或服务器未响应问题的解决
- JavaScript脚本多线程执行问题(转帖)
- C++关于多个连续流插入符&lt;&lt;中执行的顺序问题
- Java:对于Sql语句执行方式不同而出现的问题!列名: 'false' 无效
- 集合已修改;枚举操作可能无法执行。使用foreach 循环删除DataRow的时候的问题! 该死的AcceptChanges() Row.elete() Remove(Row)使用不同。
- JavaScript脚本执行的多线程问题
- VC++执行MSSQL存储过程产生错误"对象关闭,无法操作该对象"