1、ios动画介绍
ios动画实现有三种方式:
(1)、UIView动画,最基本的动画使用方式,通过改变UIView的属性达到动画效果(渐隐/渐现)
(2)、CATransition动画,用于两个视图过渡切换的动画,系统内置了集中动画效果,可以直接使用
(3)、CAAnimation动画,ios核心动画,结合绘图可以实现多变的动画效果
2、UIView基本动画
1 //标记动画块开始,设置动画id和context 2 [UIView beginAnimations:@"testAnimation"context:@"test"]; 3 //以秒为单位设置动画持续时间 4 [UIView setAnimationDuration:0.5]; 5 //定义动画加速和减速方式 6 [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut]; 7 //设置动画代 8 [UIView setAnimationDelegate:self]; 9 //动画结束后回调方法 10 [UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context)]; 11 12 //视图变化动作 13 CGRect frame = self.myView.frame; 14 frame.origin.y = 400; 15 self.myView.frame = frame; 16 //动画结束 17 [UIView commitAnimations];
block使用动画:
[UIView animateWithDuration:0.7 animations:^{ [UIView setAnimationRepeatCount:1000]; self.myView.alpha = 0; } completion:^(BOOL finished){ if (finished) { } }];
动画类型:
(1)、淡进淡出动画: 设置view.alpha为0.0 或1.0
(2)、位置变换动画:更改view.frame实现
(3)、缩放动画:
CGAffineTransform transform = view.transform; //设置动画开始尺寸 view.transform = CGAffineTransform(transfrom,0.01,0.01); [UIView beginAnimations:nil context:nil]; [UIView setAnimationDuration:0.5]; //还原为原来的尺寸 view.transform = CGAffineTransform(transfrom,0.01,0.01);
//此行代码可以代替上面的代码,效果一样
self.myView.transform =
CGAffineTransformIdentity;
[UIView commitAnimations];
(4)、旋转动画:
view.transform = CGAffineTransformMakeRotation(3.14/2.0);
(5)、设置代理,动画结束后调用相应的方法
[UIView setAnimationDelegate:self]; [UIView setAnimationDidStopSelector:@selector(animationStop)]; [UIVeiw commitAnimations];
//动画结束后调用 - (void)animationStop { [UIView beginAnimations:nil context:nil]; [UIView setAnimationDuration:0.5]; CGRect frame = self.myView.frame; frame.origin.y = 54; self.myView.frame = frame; [UIView commitAnimations]; }
(6)、视图切换的过渡动画
//注意动画是作用在父视图上的 [UIView beginAnimations:nil context:nil]; [UIView setAnimationDuration:0.5]; [UIView setAnimationTransition:UIViewAnimationTransitionCurlDown forView:parentView cache:YES]; [UIView commitAnimations]; //交换视图0和视图1的位置 [parentView exchangeSubviewAtIndex:0 withSubviewAtIndex:1];
(7)、自定义两个控制器之间的切换动画
[UIView beginAnimations:nil context:nil]; [UIView setAnimationDuration:0.5]; //视图切换动画作用于导航控制器的view上 [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:self.navigationController.view cache:YES]; [UIView commitAnimations]; UIViewController *viewContr = [[UIViewController alloc] init]; [self.navigationController pushViewController:viewContr animated:NO];
(8)、过渡动画的block使用方法
[UIView transitionWithView:self.parentView duration:0.5 options:UIViewAnimationOptionTransitionFlipFromLeft animations:^{ [self.parentView exchangeSubviewAtIndex:0 withSubviewAtIndex:1]; } completion:NULL];
options类型:
2、CATransition动画的使用
CATransition是对UIView进行更低层次的绘制,作用于UIView的Layer层,主要用于两个视图切换过渡时的动画
(2.1)、导航控制器切换动画
CATransition *animation = [CATransition animation]; animation.duration = 0.6; animation.timingFunction = [CAMediaTimingFunction functionWithName:@"easeInEaseOut"]; //动画类型 animation.type = kCATransitionPush; //动画子类型 animation.subtype = kCATransitionFromLeft; [self.navigationController.view.layer addAnimation:animation forKey:@"test"]; UIViewController *viewCtrl = [[UIViewController alloc] init]; [self.navigationController pushViewController:viewCtrl animated:NO];
动画类型:
3、UIImageView播放连续动画
imageView.animationImages = images; imageView.animationDuration = 0.5f; [imageView startAnimating];
4、开源动画库HMGLTransitions的使用
基于openGL封装的开源动画库,使用时必须导入QuartzCore.framework和OpenGLES.framework
动画效果继承于HMGLTransition:
五种动画效果:
//开门效果 DoorsTransition *animation = [[DoorsTransition alloc] init]; animation.transitionType = DoorsTransitionTypeClose; //3D效果 Switch3DTransition *switch3D = [[Switch3DTransition alloc] init]; switch3D.transitionType = Switch3DTransitionLeft; // ClothTransition *cloth = [[ClothTransition alloc] init]; //翻页效果 FlipTransition *flip = [[FlipTransition alloc] init]; flip.transitionType = FlipTransitionLeft; //上下翻转效果 RotateTransition *rotate = [[RotateTransition alloc] init];
(1)、视图切换
//创建动画类型对象 Switch3DTransition *animation = [[Switch3DTransition alloc] init]; //设置动画类型对象 [[HMGLTransitionManager sharedTransitionManager] setTransition:animation]; //设置动画需要添加的视图 [[HMGLTransitionManager sharedTransitionManager] beginTransition:parentView]; [[HMGLTransitionManager sharedTransitionManager] commitTransition]; //切换视图 [parentVIew exchangeSubviewAtIndex:0 withSubviewAtIndex:1];
(2)、模态视图弹出动画效果
//定义动画对象
DoorsTransition *animation = [[DoorsTransition alloc] init]; [[HMGLTransitionManager sharedTransitionManager] setTransition:animation]; ModalViewController *viewCtrl = [[ModalViewController alloc] init]; //打开模态视图 [[HMGLTransitionManager sharedTransitionManager] presentModalViewController:viewCtrl onViewController:self];
(3)、模态视图关闭的动画效果:
[[HMGLTransitionManager sharedTransitionManager] setTransition:animation];
[[HMGLTransitionManager sharedTransitionManager] dismissModalViewController:self];