UIView的动画是通过修改控件的属性来达到动画的效果,如:渐变, 移动.
废话不多说,直接上代码:
1 - (void)loadView{ 2 [super loadView]; 3 _leftView = [[UIView alloc]initWithFrame: CGRectMake(0, 0, 160, 560)]; 4 _rightView = [[UIView alloc]initWithFrame: CGRectMake(160, 0, 160, 560)]; 5 _leftView.backgroundColor = [UIColor grayColor]; 6 _rightView.backgroundColor = [UIColor grayColor]; 7 _btnStart = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 8 _btnStart.frame = CGRectMake(320 / 2 - 200 / 2, 400, 200, 50); 9 [_btnStart setTitle: @"startAnimation" forState:UIControlStateNormal]; 10 [_btnStart addTarget:self action:@selector(startAnimating)forControlEvents:UIControlEventTouchUpInside]; 11 12 [self.view addSubview: _leftView]; 13 [self.view addSubview: _rightView]; 14 [self.view addSubview: _btnStart]; 15 [_leftView release]; 16 [_rightView release]; 17 18 [self.navigationController setNavigationBarHidden: YES]; 19 }
1 - (void)startAnimating{ 2 _leftView.alpha = 1.0; 3 _rightView.alpha = 2.0; 4 5 _btnStart.alpha = 1.0; 6 _btnStart.transform = CGAffineTransformScale(_btnStart.transform, 1.0, 1.0); 7 8 [UIView beginAnimations: nil context: nil];//标记UIView动画开始,第一个参数是给动画的名称定义一个名字,在多个动画需要逻辑处理的时候定义,一般简单的动画可以忽略;第二个参数是上下文,暂且忽略; 9 [UIView setAnimationCurve: UIViewAnimationCurveEaseIn];//定义动画的曲线(动画的开始与结束的快慢),UIViewAnimationCurveEaseIn为默认类型; 10 [UIView setAnimationDuration: 1.5];//定义动画的持续时间,数值越大动画时间越长,也就是所谓的动画越慢; 11 12 [UIView setAnimationDelegate: self];//设置代理, 可以监听动画开始和结束时; 13 [UIView setAnimationDidStopSelector:@selector(animationDidStop)];//动画结束时调用animationDidStop方法; 14 /* 动画变化在这里修改 */ 15 CGRect lFrame = _leftView.frame; 16 CGRect rFrame = _rightView.frame; 17 18 lFrame.origin.x = -240;//通过改变x坐标达到做移动的效果 19 rFrame.origin.x = 400; 20 21 _leftView.frame = lFrame; 22 _rightView.frame = rFrame; 23 24 _leftView.alpha = 0.1;//通过改变透明度达到渐变的效果 25 _rightView.alpha = 0.1; 26 27 _btnStart.alpha = 0.0; 28 _btnStart.transform = CGAffineTransformScale(_btnStart.transform, 0.1, 0.1);//通过改变缩放比例达到动画效果 29 /* 动画变化这里结束 */ 30 [UIView commitAnimations];//确认提交动画 31 }
1 - (void)animationDidStop{ 2 3 CAAnimationViewController *caAnimationVC = [[CAAnimationViewController alloc]init];//创建一个新的ViewController命名为CAAnimationViewController,为了下一节的笔记; 4 [UIView beginAnimations: nil context:nil];//开始动画 5 [UIView setAnimationDuration:0.5];//动画持续时间 6 7 [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:self.navigationController.view cache: YES];//Transition 就是过渡的意思, 8 //过渡方法:第一个参数是过渡动画类型,这里是右转动,枚举类型; 9 //第二个参数是需要过渡的视图对象,也就是过渡页面的父类型,暂且这么记,下文有详细解释; 10 //第三个参数是是否缓存,如果是YES,那么在开始和结束图片视图渲染一次并在动画中创建帧;否则,视图将会在每一帧都渲染。例如缓存,你不需要在视图转变中不停的更新,你只需要等到转换完成再去更新视图。 11 [UIView commitAnimations];//提交动画 12 [self.navigationController pushViewController: caAnimationVC animated: NO];//push页面 13 [caAnimationVC release]; 14 15 }
关于setAnimationTransition第二个参数view,要传入过渡的父类, 做过渡的两个view是rootViewController和caAnimationVC,rootViewController的上一层(父类)是UINavigationContorller,所以传入的是self.navigationController.view.