主要是: remove, bring, exchange
//第一种方法: 将view2从window上移除
[_view2 removeFromSuperview];
//第二种方法: 将view1从屏幕的底层拿出来放在顶层
[self.window bringSubviewToFront:_view1];
//第三种方法: 交换位置
[self.window exchangeSubviewAtIndex:1 withSubviewAtIndex:0];
//定时器 NSTimer
//scheduled (安排) interval (间隔)
// target (目标 对象)
//selector 方法
//userinfo (用户信息) repeat 重复
//每间隔一定的时间 由 谁 是否重复性的 做 某事
//每隔1秒 就由self 重复执行 onTimer 方法
NSTimer *timer=[NSTimer scheduledTimerWithTimeInterval:1 target:self
selector:@selector(onTimer) userInfo:nil repeats:YES];
//立即开启 不再间隔1秒
[timer fire];
//定时器处理函数
- (void)onTimer {
static int count=0;
//运行[0, 81)共81次后关闭定时器, 计数器清零
if (count>80) {
count=0;
[_timer invalidate];
_timer=nil;
return;
}
count++;
}
//按键按下开启定时器
- (void)onClick {
//_timer被声明成全局变量NSTimer *_timer;
if (_timer==nil) {
_timer=[NSTimer scheduledTimerWithTimeInterval:0.1 target:self
selector:@selector(onClick) userInfo:nil repeats:YES];
[_timer fire];
}
}
注: 可以使用 userInfo
传递消息, 把 nil 改为传递的对象, 这时定时器处理函数为 - (void)onTimer:(NSTimer *)timer
, 而获取传递的对象, 可以 NSObject *obj = [NSTimer userInfo]
来获取.
如改变雪花图片视图的y坐标:
//a.先获取雪花这个矩形块
CGRect snowRect= snow.frame;
//b.改变矩形的y坐标
snowRect.origin.y+=1;
//c.把改变过后的矩形块 重新赋给视图的frame.
snow.frame=snowRect;
//数组:一组有序数据的集合
//特点:数据存放是有序的(按照从小到大的顺序排放,并且不能出现空缺),每一个元素都有自己的索引值(index),
// 索引值是按照从小到大顺序排放的.
//限制:存放的数据只能是 对象类型 ,不能存放基本数据类型,结构体
//分两在类: 不可变数组 NSArray ; 可变数组 NSMutableArray
//1.NSArray 创建并初始化的时候,内部存放的数据不能再改变
//a. 创建数组
NSArray *array=[[NSArray alloc]initWithObjects:@"张三",@"张六",@"23",@"李四", nil];
//b.拿到数组之后,如何获取数组中元素个数
unsigned long number = array.count;
// unsigned long number = [array count];
NSLog(@"%lu",number);
//c.拿到数组后,如何获取数组中每一个元素分别是什么,根据index获取数组中的每一个元素.
//for 循环遍历数组中的元素
//先初始化一个数值(一般从0开始),接着进行一个判断,看数组是否满足条件,如果满足条件 就执行大括号内的操作. 然后对数值进行++.....直到条件不满足条件 循环结束.
//查看数组中元素个数时 一旦索引值超出元素个数 程序就会崩溃.
for (int a=0; a < array.count; a++)
{
NSString *object=[array objectAtIndex:a ];
NSLog(@"%@",object);
}
//2.可变数组 NSMutableArray
//a. 创建
//初始化方法:(1)init (2)initWithObjects (3)initWithCapacity 容量
NSMutableArray *mutableArray=[[NSMutableArray alloc]initWithObjects:@"1",@"4",@"5",@"2",@"3",@"6", nil];
//b.增加
[mutableArray addObject:@"7"];
//大量增加有规律的数据
for (int i=8; i<20; i++)
{
NSString *str=[NSString stringWithFormat:@"%d",i];
[mutableArray addObject:str];
}
for (int b=0; b<mutableArray.count; b++)
{
NSString *OB =[mutableArray objectAtIndex:b ];
NSLog(@"%@",OB);
}
//添加10个元素到数组
//for (int i=0; i<10; i++)
//{
// UILabel *lab=[[UILabel alloc]init];
// [mutableArray addObject:lab];
//}
//c.删除元素
//根据元素删除
[mutableArray removeObject:@"2"];
//根据索引值删除
[mutableArray removeObjectAtIndex:15];
//d.替换元素
[mutableArray replaceObjectAtIndex:12 withObject:@"上课"];
//e.插入
[mutableArray insertObject:@"吃饭" atIndex:16];
//f.交换位置
[mutableArray exchangeObjectAtIndex:4 withObjectAtIndex:6];
//g.获取(查看数组中每一个元素,方式(方法)都与不可变数组一样
属于UIImageView, 区别于UIView动画
先建立一个元素为UIImage的数组, 放入图片, 然后开始帧动画.
//帧动画 系统提供的 基于UIImageView 的帧动画
//1.创建图片视图,作用:展示动画的每一张图片
UIImageView *eatImageView=[[UIImageView alloc]initWithFrame:self.window.frame];
[self.window addSubview:eatImageView];
//设置帧动画所需的三个属性(图片数组, 动作时间, 重复次数)和一个方法(开启动画)
//a.动画所需要的图片资源
//创建可变数组,使用for循环依次
NSMutableArray *imageArray=[[NSMutableArray alloc]init];
for (int i=0 ; i<40; i++)
{
NSString *name=[NSString stringWithFormat:@"eat_%d.jpg",i];
UIImage *eatImage=[UIImage imageNamed:name ];
[imageArray addObject:eatImage];
}
eatImageView.animationImages =imageArray;
//b.设置完成这组动作所需要的时间
//0.1*40
eatImageView.animationDuration=0.1*imageArray.count;
//c.设置动画重复次数 ( 0 langmax 代表无数次)
eatImageView.animationRepeatCount=0;
//d.开启动画
[eatImageView startAnimating];
判断动画是否正在进行:
if ([eatImageView isAnimating] == YES) {
}
tag是UIView的一个属性, 可以给基本所有的控件添加tag值.
下面是使用UIImageView设置tag值的例子:
UIImageView *tomImgView = [[UIImageView alloc]initWithFrame:self.window.frame];
tomImgView.image = [UIImage imageNamed:@"angry_00.jpg"];
tomImgView.tag = 100;
[self.window addSubview:tomImgView];
- (void)eatClick
{
UIImageView *tomImgView = (UIImageView *)[self.window viewWithTag:100];
//isAnimating:判断动画是否正在进行
// if ([tomImgView isAnimating] == YES)
// {
// return;
// }
[tomImgView startAnimating];
}
//判断两个矩形块 交叉的 函数,返回值类型:bool
//CGRectIntersectsRect(<#CGRect rect1#>, <#CGRect rect2#>)
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
//1.获取touch事件
UITouch *touch = [touches anyObject];
//2.获取屏幕中的点
CGPoint point = [touch locationInView:self.window];
//3.将鼠标拖动的 点的坐标 赋值给 图片的坐标
if (CGRectContainsPoint(_imageView.frame, point))
{
_imageView.center = point;
}
}
//LY3ViewController是自定义的viewController
LY3ViewController *ThirdVC=[[LY3ViewController alloc]init];
//找window window是appdelegate(入口类)的属性
//1.先找 应用程序类(单例类:整个程序中这个类 只有一个对象)的对象
UIApplication *app =[UIApplication sharedApplication];
//2.再找APPdelegate 类的对象
AppDelegate *appDelegate = app.delegate;
//3.获取window
UIWindow *window= appDelegate.window;
//以上3句可以合并为:
//UIWindow *window= [UIApplication sharedApplication].delegate.window;
window.rootViewController=ThirdVC;
//翻页动画
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:2];
//注意是window而不是view
//当vc1-->vc2 的时候,vc1.view从window上移除是瞬时性动作,所以对vc1.view做动画是没有效果的.
//在界面跳转的整个过程中,window始终存在,而且不会变动,所以可以对window做一个动画效果.
//总结:如果要实现两个界面的跳转,同时加上翻转动画,这个时候 forView:的参数值 为:当前view的父视图.
[UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:window cache:YES];
[UIView commitAnimations];
两个ViewController之间实现界面跳转的一种方式.
//点击按钮,实现模态弹出,让viewController2的view展示出来
- (IBAction)changeView:(id)sender {
//1.创建vc2
ViewController2 *vc2 = [[ViewController2 alloc]init];
//2.实现模态弹出 present:当前的
[self presentViewController:vc2 animated:YES completion:nil];
}
//点击按钮让vc2的view消失
- (IBAction)viewDismiss:(id)sender {
[self dismissViewControllerAnimated:YES completion:nil];
}
效果如下:
alert: 警告, 如我们删除一个文件时, 弹出是否删除的警告框.
/* delegate: 代理 protocol: 协议 optional:可选择的 required:必选的 (如果协议方法使用"required"描述,那么当设置完代理,就必须实现协议方法,如果不实现,程序就会崩溃) 代理-协议的使用步骤: 1.设置代理 delegate:self 2.粘贴协议方法 (到UIAlertView.h文件中) 3.实现协议方法 */
//点击按钮删除文件时, 弹出是否确定删除的警告框
- (IBAction)deleteFile:(UIButton *)sender {
//UIAlertView:提示对话框
//1.创建
UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"提示" message:@"是否确定删除" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
//2.展示
[alertView show];
//UIActionSheet
//UIAlertController
}
//当点击alertView警告框上的按钮时,(alertView:clickedButtonAtIndex:)这个方法会被调用
//参数1: 表示弹出的是哪一个alertView
//参数2: 表示点击的是alertView 警告框上的哪一个按钮(确定 ,还是取消. button 的 tag值)
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
// NSLog(@"buttonIndex == %d",buttonIndex);
if (buttonIndex == 0) {
NSLog(@"取消,不删除该文件");
}
if (buttonIndex == 1) {
NSLog(@"确定,删除选中的文件");
}
}
类似UIAlertView, 只不过不是在中间, 是在底部.
//按下删除按钮, 弹出ActionSheet
- (IBAction)deleteClick:(id)sender {
UIActionSheet *sheet = [[UIActionSheet alloc]initWithTitle:@"删除" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"销毁" otherButtonTitles:@"确定", nil];
[sheet showInView:self.view];
}
//ActionSheet上按钮的处理方法
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
NSLog(@"buttonIndex == %d",buttonIndex);
}
iOS 8???以后用来替代UIAlertView和UIActionSheet这两个类. (默认情况下Xcode 5是不能用的)
- (IBAction)deleteClick:(id)sender {
//UIAlertController 替代的UIAlertView和UIActionSheet这两个类
//1.创建控制器: UIAlertController
//参数1:控制器的标题
//参数2:提示信息
//参数3:样式,(对话框,列表)
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"删除" message:@"是否确定删除" preferredStyle:UIAlertControllerStyleActionSheet];
//2.通过实例化一个UIAlertAction的对象,给控制器添加动作按钮.
//a.实例化对象
//参数1:动作按钮的标题
//参数2:按钮的样式
//参数3:block块,指定点击动作按钮时,执行的事件
UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"点击取消按钮,什么也不干");
}];
//b.添加
[alertController addAction:action1];
//控制器中 只能 指定唯一 一个按钮为 取消按钮,如果出现多个,程序就会崩溃.
//错误:reason: 'UIAlertController can only have one action with a style of UIAlertActionStyleCancel'
UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"确定按钮,确定删除该文件");
}];
[alertController addAction:action2];
UIAlertAction *action3 = [UIAlertAction actionWithTitle:@"彻底删除" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"确定按钮,彻底删除该文件");
}];
[alertController addAction:action3];
//3.展示
[self presentViewController:alertController animated:YES completion:nil];
}