一、userInfo
//userInfo:在timer里要对哪个对象操作,就传入哪个对象
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(drinkAnimation:) userInfo:drinkImgView repeats:YES];
二、项目:HmWork_PlayBall_Teacher0303
//定义好初始速度
offSet_x = 2;//x方向速度
offSet_y = 3;//y方向速度
//球移动的方法
- (void)ballMove:(NSTimer *)timer1
{
UIImageView *ballImgView = timer1.userInfo;
CGRect ballRect = ballImgView.frame;
//判断是否到达 左右边界
if (ballImgView.frame.origin.x > self.window.bounds.size.width - 60 || ballImgView.frame.origin.x < 0)
{
offSet_x = -offSet_x;
}
//判断是否到达 上下边界
if (ballImgView.frame.origin.y > self.window.bounds.size.height - 60 || ballImgView.frame.origin.y < 0)
{
offSet_y = -offSet_y;
}
ballRect.origin.x = ballRect.origin.x + offSet_x;
ballRect.origin.y = ballRect.origin.y + offSet_y;
ballImgView.frame = ballRect;
}
三、数组
项目:Array_Demo
//数组:一组有序数据的集合
//特点:数据存放是有序的,并且不允许出现空缺。
// 每个元素都有索引值,按照索引值从小到大顺序排列
//限制:只能存放对象类型,不能存放基本数据类型
//种类:1.不可变数组 NSArray 2.可变数组 NSMutableArray
//1.创建 NSArray**********************************************
//a.创建并初始化之后,不能修改数据
NSArray *array = [[NSArray alloc]initWithObjects:@"张三",@"李四",@"王五",@"小红",@"23",nil];
//b.拿到数组后,如何获取数组中元素的个数
int number = array.count;
//c.获取和查看组中的元素
NSString *object = [array objectAtIndex:0];
for (int a = 0; a < array.count; a++)
{
object = [array objectAtIndex:a];
NSLog(@"%@",object);
}
//2.创建 NSMutableArray**********************************************
//a. 初始化方法1.init 2.initWithObjects 3.initWithCapacity(容量)
NSMutableArray *mutableArray = [[NSMutableArray alloc]initWithObjects:@"1",@"2",@"3",@"4", nil];
//b.增加数据
[mutableArray addObject:@"5"];
//b.1 增加大量、有规律的数据
for (int i = 6; i < 21; i++)
{
NSString *str = [NSString stringWithFormat:@"%d",i];
[mutableArray addObject:str];
}
//c.删除数据
// 1.removeObjectAtIndex
// 2.removeObject
[mutableArray removeObject:@"20"];
//[mutableArray removeObjectAtIndex:19];
//d.替换数据
[mutableArray replaceObjectAtIndex:18 withObject:@"29"];
//e.插入操作
[mutableArray insertObject:@"小明" atIndex:19];
//f.交换
[mutableArray exchangeObjectAtIndex:0 withObjectAtIndex:15];
//g.获取数组中每一个元素,方式都与不可变数组一样
return YES;
四、帧动画
项目:Frame_Animation
//帧动画 系统提供的 基于 UIImageView 的帧动画
//1.创建图片视图,展示动画的每一张图片
_fireImgView = [[UIImageView alloc]initWithFrame:self.window.frame];
_fireImgView.image = [UIImage imageNamed:@"fire.png"];
[self.window addSubview:_fireImgView];
//2.设置:3个属性+1个方法
//即:animationImages、animationDuration、animationRepeatCount
//2.1 属性1 设置动画所需图片
NSMutableArray *imageArray = [[NSMutableArray alloc]init];
for (int i = 0; i < 17; i++)
{
NSString *name = [NSString stringWithFormat:@"Fire%d.gif",i+1];
UIImage *image = [UIImage imageNamed:name];
[imageArray addObject:image];
}
_fireImgView.animationImages = imageArray;
//2.2 属性2 设置动画时间间隔
_fireImgView.animationDuration = 0.01 * imageArray.count;
//2.3 属性3 是否重复
_fireImgView.animationRepeatCount = LONG_MAX;//0或者LONG_MAX
//3.添加开始/停止按钮
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(40, 40, 40, 40);
[button setTitle:@"开始/停止" forState:UIControlStateNormal];
[button setBackgroundImage:[UIImage imageNamed:@"play"] forState:UIControlStateNormal];
[button addTarget:self action:@selector(startAndStop:) forControlEvents:UIControlEventTouchUpInside];
[self.window addSubview:button];
return YES;