NSTimer
2015-12-29 00:00
169 查看
摘要: 定时器运用 跑火车
#import "ViewController.h" #import <QuartzCore/QuartzCore.h> @interface ViewController () { NSTimer *tiemer; UIView *view; UIView *yellowView; } @end static BOOL left = NO; static BOOL right = NO; static BOOL top = NO; static BOOL bottom = NO; @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. self.view.backgroundColor = [UIColor whiteColor]; [self creatView]; } -(void)creatView { view = [[UIView alloc]init]; view.frame = CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y+64, 0.15*self.view.frame.size.height, 0.15*self.view.frame.size.width); [view setBackgroundColor:[UIColor clearColor]]; UIImage *image = [UIImage imageNamed:@"2"]; UIImageView *imageview = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 0.15*self.view.frame.size.height, 0.15*self.view.frame.size.width)]; [imageview setImage:image]; view.layer.masksToBounds = YES; view.layer.cornerRadius = 0.5; [view addSubview:imageview]; [self.view addSubview:view]; yellowView = [[UIView alloc]init]; yellowView.frame = CGRectMake(self.view.frame.origin.x+0.15*self.view.frame.size.height+10, self.view.frame.origin.y+64, 0.15*self.view.frame.size.width, 0.15*self.view.frame.size.height); [yellowView setBackgroundColor:[UIColor yellowColor]]; [self.view addSubview:yellowView]; UIView *topView = [[UIView alloc]init]; topView.frame = CGRectMake(0, 0, 70, 20); topView.backgroundColor = [UIColor blackColor]; [self.view addSubview:topView]; } -(void) updateTime { CGRect rect = view.frame; if (right ==NO && top == NO && bottom == NO) { rect.origin.x += 5; if (rect.origin.x+(0.15*self.view.frame.size.width) >= 320) { right = YES; } } if (bottom == YES && left == NO) { rect.origin.x -= 5; rect.origin.y = self.view.frame.size.height - 0.15*self.view.frame.size.height; if (rect.origin.x <= 0) { left = YES; } } else if (left == YES) { rect.origin.x = 0; rect.origin.y -= 5; } else if (right==YES) { rect.origin.y += 5; rect.origin.x = 320-(0.15*self.view.frame.size.width); if (rect.origin.y+0.15*self.view.frame.size.height >= self.view.frame.size.height) { bottom = YES; } } view.frame = rect; } -(void) touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { if (tiemer !=nil) { [tiemer invalidate]; tiemer = nil; } tiemer = [NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(updateTime) userInfo:nil repeats:YES];//重复运行 } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end
相关文章推荐
- maven打包跳过Test
- logrotate日志管家来切割日志(tomcat,nginx,httpd的日志)
- windows bat 常用命令
- 天下数据教你如何应付ARP病毒
- 关于为什么IE浏览器无法打开开发者模式的解决办法
- 使用git建立远程仓库,让别人git clone下来
- svn使用socks5代理 ProxyChains
- nginx 开发一个简单的 HTTP 模块
- MySQL新增字段、修改字段位置和查询存储过程的SQL代码
- Java 数组基础
- [实践] Android5.1.1源码 - 在Framework中添加自定义系统服务
- Android5.1.1源码 - 关键README路径
- Sqoop官方文档翻译1
- Sqoop官方文档翻译2
- ajax请求与传统http请求区分
- java按日期加1
- java比较日期
- Android最佳性能实践(四)——布局优化技巧
- VRRP协议
- LVS工作模式及原理