IOS开发之XCode学习010:定时器和视图对象
此文学习来源为:http://study.163.com/course/introduction/1002858003.htm
此工程文件实现功能:
1、通过点击“启动定时器”按钮,在定时器方法中每隔固定时长打印字符串,并实现view视图移动
2、通过点击“停止定时器”按钮,停止定时器方法的执行
3、以上操作可以重复执行
===========================ViewController.h脚本==============================
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
{
//定义一个定时器对象
//可以在每个固定时间发送一个消息
//通过此消息来调用相应的时间函数
//通过此函数可以在固定时间段来完成一个根据时间间隔的任务
NSTimer* _timerView;
}
//定时器的属性对象
@property (retain,nonatomic) NSTimer* timerView;
@end
===========================ViewController.m脚本==============================
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
//属性和成员变量的同步
@synthesize timerView = _timerView;
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIButton* btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn.frame = CGRectMake(100, 100, 80, 40);
[btn setTitle:@"启动定时器" forState:UIControlStateNormal];
[btn addTarget:self action:@selector(pressStart) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
UIButton* btnStop = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btnStop.frame = CGRectMake(100, 200, 80, 40);
[btnStop setTitle:@"停止定时器" forState:UIControlStateNormal];
[btnStop addTarget:self action:@selector(pressStop) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btnStop];
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
{
//NSTimer的类方法创建一个定时器并且启动这个定时器
//P1:每隔多长时间调用定时器函数,以秒为单位
//P2:表示实现定时器函数的对象(指针)
//P3:定时器函数对象
//P4:可以传入定时器函数中一个参数,无参数可以传nil
//P5:定时器是否重复操作 YES:重复 NO:只完成一次函数调用
//返回值为一个新建好的定时器对象
_timerView = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(updateTimer:) userInfo:@"小明" repeats:YES];
}
-(void) updateTimer:(NSTimer*) timer
{
NSLog(@"test!!!,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 (_timerView != nil) {
//停止定时器
[_timerView invalidate];
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
学习总结:
- 重点:定时器的创建使用
- 难点:定时器移动视图
源码链接地址:https://pan.baidu.com/s/1yrOLXZZeu9MiOWtMq5-EGA 密码:7t1l
IOS开发之XCode学习010:定时器和视图对象的更多相关文章
- IOS开发之XCode学习014:警告对话框和等待提示器
此文学习来源为:http://study.163.com/course/introduction/1002858003.htm 此工程文件实现功能: 1.警告对话框和等待提示器的概念 2.警告对话框 ...
- IOS开发之XCode学习011:UISwitch控件
此文学习来源为:http://study.163.com/course/introduction/1002858003.htm 此工程文件实现功能: 1.定义UIswitch控件,添加UIswitc ...
- IOS开发之XCode学习009:UIViewController使用
此文学习来源为:http://study.163.com/course/introduction/1002858003.htm 此工程文件实现功能: 通过点击屏幕事件,调用ViewController ...
- IOS开发之XCode学习008:UIViewController基础
此文学习来源为:http://study.163.com/course/introduction/1002858003.htm 红色框选部分用A代替,AppDelegate类在程序框架启动时,如果在i ...
- IOS开发之XCode学习007:UIWindow对象
此文学习来源为:http://study.163.com/course/introduction/1002858003.htm #import "AppDelegate.h" @i ...
- IOS开发之XCode学习012:Slider和ProgressView
此文学习来源为:http://study.163.com/course/introduction/1002858003.htm 此工程文件实现功能: 1.定义UISlider和UIProgressV ...
- IOS开发之XCode学习013:步进器和分栏控件
此文学习来源为:http://study.163.com/course/introduction/1002858003.htm 此工程文件实现功能: 1.定义UIStepper和UISegmente ...
- iOS开发之Xcode常用调试技巧总结
转载自:iOS开发之Xcode常用调试技巧总结 最近在面试,面试过程中问到了一些Xcode常用的调试技巧问题.平常开发过程中用的还挺顺手的,但你要突然让我说,确实一脸懵逼.Debug的技巧很多,比如最 ...
- 李洪强iOS开发之Xcode快捷键
14个Xcode中常用的快捷键操作 在Xcode 6中有许多快捷键的设定可以使得你的编程工作更为高效,对于在代码文件中快速导航.定位Bug以及新增应用特性都是极有效的. 当然,你戳进这篇文章的目的 ...
随机推荐
- 使用exe4j工具制作简单的java应用程序
首先需要下载exe4j工具并进行安装,下面是利用exe4j工具制作应用程序的步骤. 1.首先将工程导出为可运行的jar包,选择extract required libralies into gener ...
- POJ3155 Hard Life [最大密度子图]
题意:最大密度子图 #include<iostream> #include<cstdio> #include<cstring> #include<algo ...
- es6 Object.assign
ES6 Object.assign 一.基本用法 Object.assign方法用来将源对象(source)的所有可枚举属性,复制到目标对象(target).它至少需要两个对象作为参数,第一个参数是目 ...
- 六、BeautifulSoup4------自动登录网站(手动版)
每天一个小实例:(按照教学视频上自动登录的网站,很容易就成功了.自已练习登录别的网站,问题不断) 这个自己分析登录boss直聘.我用了一下午的时间,而且还是手动输入验证码,自动识别输入验证码的还没成功 ...
- 【汇总】Linux常用脚本shell
[crontab] #每天6:00 执行a.sh00 6 * * * /bin/sh /home/work/rxShell/a.sh #每天3:20 执行a1.sh20 3 * * * /bin/sh ...
- 图论算法-网络最大流【EK;Dinic】
图论算法-网络最大流模板[EK;Dinic] EK模板 每次找出增广后残量网络中的最小残量增加流量 const int inf=1e9; int n,m,s,t; struct node{int v, ...
- angular2 localStorage的使用
最近从ng1 转ng2 相信 使用ng1的同学都知道 ngStorage 这个插件吧, 存储 本地数据 下面介绍一下 ng2 使用 localStorage 参考 github https ...
- Inspinia_admin-V2.3原版(英文)
Inspinia_admin-V2.3原版(英文) Inspinia_admin-V2.3 BootStrap原版(英文) 原版是老外开发的,结果 国内某人翻译成中文版进行二次开发 卖998 演示地址 ...
- applicationContext.xml最基本配置文件
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...
- PHP动态编译出现Cannot find autoconf的解决方法
wget http://ftp.gnu.org/gnu/m4/m4-1.4.9.tar.gz tar -zvxf m4-.tar.gz cd m4-/ ./configure && m ...