就两种,UIView自带动画和核心动画(Core Animation)
一、UIView自带动画(UIKit框架中)
注意:只有当UIView的以下属性改变时才能产生动画效果,且这些动画效果都是平面性的(二维的),基于手机屏幕的平面,主要有“平移(位置改变,沿x轴、Y轴或同时改变)”、“缩放(view大小改变,宽高)”、“旋转(只有沿Z轴,即垂直于屏幕的轴,旋转平行于屏幕)”、“背景色的改变”、“透明度”:
- frame
- bounds
- center
- transform (变换【平移 + 缩放 + 旋转】)
- alpha
- backgroundColor
- contentStretch
重点介绍transform的使用:
eg:view.transform = CGAffineTransformMakeSca
le(0.5, 0.5);
CGAffineTransform结构体详解 CGAffineTransform.h:
CGAffineTransform CGAffineTransformIdentity 默认值,保持原样不变
CGAffineTransform CGAffineTransformMake(CGFloat a, CGFloat b,CGFloat c, CGFloat d, CGFloat tx, CGFloat ty) 直接变换三维矩阵,很少用吧
创建变换=============
//Translation(平移)创建一个平移变换,起始位置 x 会加上tx , y 会加上 ty
CGAffineTransform CGAffineTransformMakeTranslation(CGFloat tx,CGFloat ty)
//Scale(缩放) 宽度就会变为 width*sx ,对应高度变为 hight * sy
CGAffineTransform CGAffineTransformMakeScale(CGFloat sx, CGFloat sy)
//Rotation(旋转)将一个图片视图旋转了多少度,参数是弧度,先把度转化为弧度
CGAffineTransform CGAffineTransformMakeRotation(CGFloat angle) //90.0*(M_PI/180.0) 顺时针旋转90度,-90.0*(M_PI/180.0) 逆时针旋转90度,view的旋转方向仅以整个圆周最小的弧度,即270不会顺时针转3/4圈,而是逆时针转1/4圈,以180为界,大于等于180了就是逆时针了
增加变换===============
//为一个变换再加上平移变换
CGAffineTransform CGAffineTransformTranslate(CGAffineTransform t,CGFloat tx, CGFloat ty)
//为一个Transformation再加上缩放
CGAffineTransform CGAffineTransformScale(CGAffineTransform t,CGFloat sx, CGFloat sy)
//为一个Transformation再加上旋转
CGAffineTransform CGAffineTransformRotate(CGAffineTransform t,CGFloat angle)
其他====================
//判断一个变换是不是原生变换,即没有什么变换
bool CGAffineTransformIsIdentity(CGAffineTransform t)
//创建一个变换的反向效果
CGAffineTransform CGAffineTransformInvert(CGAffineTransform t)
//合并两个变换
CGAffineTransform CGAffineTransformConcat(CGAffineTransform t1,CGAffineTransform t2)
//判断两个变换是否一样
bool CGAffineTransformEqualToTransform(CGAffineTransform t1,CGAffineTransform t2)
(1).以begin commit的方式提交动画代码块 :
[UIView beginAnimations:@"View Flip" context:nil];
[UIView setAnimationDuration:2.25];
//动画持续时间,秒
[UIView setAnimationCurve:UIViewAnimationCurveLine
ar];//动画执行速度(UIViewAnimationCurveEase
InOut--动画开始和结束慢,中间快
UIViewAnimationCurveEase
In--开始慢,后面快
UIViewAnimationCurveEase
Out--前面快,结束慢
UIViewAnimationCurveLine
ar--匀速)
[UIView setAnimationDelegate:self];//一定要设置动画的委托
...............
.{改变View属性的代码块}
...................
.........
[UIView commitAnimations];
说明:以上代码块便可以执行一个动画,其中的“{...}”块又可以嵌套新的动画块,
里面的动画块只有在
最外层的动画提交之后才会一起执行
其中有一个特例,可以实现view的翻页、立体旋转等效果
:
+ (void)
setAnimationTransition
:(UIViewAnimationTransitio
n)transition forView:(UIView *)view cache:(BOOL)cache;
UIViewAnimationTransitio
n宏介绍如下:
UIViewAnimationTransitio
nNone,
UIViewAnimationTransitio
nFlipFromLeft,
UIViewAnimationTransitio
nFlipFromRight,
UIViewAnimationTransitio
nCurlUp,
UIViewAnimationTransitio
nCurlDown,
二、使用核心动画(Core Animation)[QuartzCore.framework中]
(1).UIView与CALayer的关系:
实质上UIView中元素的展示与渲染都是考CALayer来实现,每个UIView都包含有一个layer属性,view与layer操作上也是一样,既能addSubLayers(addSubView)也能获取父视图(父layer),创建layer对象时也需指定大小(frame)、内容(content)、背景色等属性,唯一不同的时UIView还继承自UIResponder,便能响应用户事件,而CALayer直接继承自NSObject,不能响应
第一点中的直接操作UIView的某些属性便能产生动画,其实质就是操作改UIView对象中的Layer对象的属性
(2).
QuartzCore框架中动画产生的原理
该框架中有三种类型的动画:
CABasicAnimation
CAKeyframeAnimation
CATransition
只要将创建好这三种动画对象add到layer上便能产生动画效果,不再需要使用第一点中UIView得代码块提交了,他们的继承关系如下:
(3).CABasicAnimation和CAKeyframeAnimation是对图层中的不同属性进行动画的,即第一点所说的支持动画的那些属性
- (void)changeUIView2{
CATransition *transition = [CATransition animation];
transition.duration = 2.0f;
transition.type = kCATransitionPush;
transition.subtype = kCATransitionFromTop;
[self.view exchangeSubviewAtIndex:1 withSubviewAtIndex:0];
[self.view.layer addAnimation:transition forKey:@"animation"];
}
transition.type 的类型可以有
淡化、推挤、揭开、覆盖
NSString * const kCATransitionFade;
NSString * const kCATransitionMoveIn;
NSString * const kCATransitionPush;
NSString * const kCATransitionReveal;
transition.subtype
也有四种
NSString * const kCATransitionFromRight;
NSString * const kCATransitionFromLeft;
NSString * const kCATransitionFromTop;
NSString * const kCATransitionFromBottom;
私有的类型的动画类型:
立方体、吸收、翻转、波纹、翻页、反翻页、镜头开、镜头关
[cpp]
1. animation.type = @"cube"
2. animation.type = @"suckEffect";
3. animation.type = @"oglFlip";//不管subType is "fromLeft" or "fromRight",official只有一种效果
4. animation.type = @"rippleEffect";
5. animation.type = @"pageCurl";
6. animation.type = @"pageUnCurl"
7. animation.type = @"cameraIrisHollowOpen ";
8. animation.type = @"cameraIrisHollowClose ";
下图是第一个cube立方体的效果:
CATransition的 startProgress endProgress属性
这两个属性是float类型的。 可以控制动画进行的过程,可以让动画停留在某个动画点上,值在0.0到1.0之间。endProgress要大于等于startProgress。
比如上面的立方体转到,可以设置endProgress= 0.5,让动画停留在转动一般的位置。
上面这些私有的动画效果,在实际应用中要谨慎使用。因为在app store审核时可能会以为这些动画效果而拒绝通过。
// 动画终了后不返回初始状态
animation.removedOnCompletion= NO;
animation.fillMode= kCAFillModeForwards;
(6). CABasicAnimation正在进行动画的时候,点击了Home按钮后再回到app的时候,动画会被清空。