iOS开发从入门到精通--定时器UITimer和视图对象移动
来源:互联网 发布:淘宝有发票是正品么 编辑:程序博客网 时间:2024/05/21 09:43
定时器UITimer和视图对象移动
在ViewController.h
#import <UIKit/UIKit.h>@interface ViewController : UIViewController{ //定义一个定时器对象 //可以在每隔固定的时间发送一个消息 //通过消息来调用相应的事件函数 //通过此函数可在固定时间段来完成一个根据时间间隔的任务 NSTimer * _timeView;}//定时器的属性对象@property(retain,nonatomic) NSTimer * timeView;@end
在ViewController.m
#import "ViewController.h"@interface ViewController ()@end@implementation ViewController//属性和成员变量的同步@synthesize timeView =_timeView;- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. //启动定时器按钮 UIButton * btn =[UIButton buttonWithType:UIButtonTypeRoundedRect]; //目前我们使用UIButtonTypeRoundedRect设置圆角已经不可以了,需要加上下面2句设置四周的圆角 [btn.layer setMasksToBounds:YES]; [btn.layer setCornerRadius:10.0];//设置矩形四个圆角半径 btn.frame=CGRectMake(100, 100, 100, 40); [btn setTitle:@"定时器按钮" forState:UIControlStateNormal]; btn.backgroundColor=[UIColor greenColor]; [btn addTarget:self action:@selector(pressStart) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:btn]; //停止定时器按钮 UIButton * btnStop =[UIButton buttonWithType:UIButtonTypeRoundedRect]; //目前我们使用UIButtonTypeRoundedRect设置圆角已经不可以了,需要加上下面2句设置四周的圆角 [btnStop.layer setMasksToBounds:YES]; [btnStop.layer setCornerRadius:10.0];//设置矩形四个圆角半径 btnStop.frame=CGRectMake(100, 200, 100, 40); [btnStop setTitle:@"停止定时器" forState:UIControlStateNormal]; btnStop.backgroundColor=[UIColor redColor]; [btnStop addTarget:self action:@selector(pressStop) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:btnStop]; //创建一个view视图 UIView * view =[[UIView alloc]init]; view.frame =CGRectMake(0, 0, 80, 80); view.backgroundColor=[UIColor orangeColor]; [self.view addSubview:view]; //设置view的标签值 //通过父亲视图对象以及view的标签值可以获得相应的视图对象 view.tag=101;}//按下开始按钮函数- (void) pressStart{ //NSTime的类方法创建一个定时器并且启动这个定时器 //p1:每个多长时间调用定时器函数,以秒为单位 //p2:表示实现这个定时器函数的对象 //p3:定时器函数对象 //p4:可以传入定时器函数中一个参数,无参数传入nil //p5:定时器是否重复操作YES表示重复,NO表示只完成一次函数调用// _timeView = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(updateTime) userInfo:nil repeats:YES]; _timeView = [NSTimer scheduledTimerWithTimeInterval:0.03 target:self selector:@selector(updateTime:) userInfo:@"苹果" repeats:YES];}//定时器函数//- (void) updateTime{// NSLog(@"test11");//}//将定时器本身作为参数传入- (void) updateTime:(NSTimer* )timer{ NSLog(@"test11 name=%@",timer.userInfo); //最好tag从100开始,避免冲突 UIView * view =[self.view viewWithTag:101]; view.frame=CGRectMake(view.frame.origin.x+1,view.frame.origin.y+1, 80, 80);}//按下停止按钮函数- (void) pressStop{ //停止定时器 if(_timeView!=nil){ [_timeView invalidate]; }}- (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.}@end
0 0
- iOS开发从入门到精通--定时器UITimer和视图对象移动
- iOS开发从入门到精通-- 滚动视图UIScrollView
- iOS开发从入门到精通--手动布局子视图
- iOS开发从入门到精通--自动布局子视图
- 《iOS移动开发从入门到精通》图书连载11:字符和字符串
- 《iOS移动开发从入门到精通》图书连载17:函数的定义和调用
- 《iOS移动开发从入门到精通》图书连载18:函数作为参数和返回类型
- 《iOS移动开发从入门到精通》图书连载2:如何成为一名iOS开发者
- 《iOS移动开发从入门到精通》图书连载4:iOS应用的生命周期
- 《iOS移动开发从入门到精通》图书连载7:iOS模拟器的使用
- iOS开发从入门到精通-- 滚动视图UIScrollView高级,协议。
- 《iOS移动开发从入门到精通》图书连载5:Xcode 8的使用(上)
- 《iOS移动开发从入门到精通》图书连载8:Swift的基本概念(上)
- 《iOS移动开发从入门到精通》图书连载9:Swift的基本概念(下)
- 《iOS移动开发从入门到精通》图书连载15:数组
- 《iOS移动开发从入门到精通》图书连载16:字典
- 《iOS移动开发从入门到精通》图书连载19:函数的嵌套
- 《iOS移动开发从入门到精通》图书连载20:枚举
- Uva 11324 The Largest Clique
- 37、面向对象-继承
- python核心编程学习笔记-2016-07-18-01-print
- 几种获取进程列表的方法
- Java Web 自定义MVC框架
- iOS开发从入门到精通--定时器UITimer和视图对象移动
- luogu1328[NOIP2014 T1]生活大爆炸版石头剪刀布
- js中函数参数传递
- PipeValid.js 2.0 数据验证
- StringUtils中 isNotEmpty 和isNotBlank的区别
- MongoDB学习笔记(索引)
- discuz常用 save_syscache函数说明
- js表单克隆
- gdb core的使用