ios动画笔记(1)CABasicAnimation和CAKeyframeAnimation

来源:互联网 发布:网络红猫端午 编辑:程序博客网 时间:2024/06/08 00:01

1、CABasicAnimation

使用方法animationWithKeyPath:对 CABasicAnimation进行实例化,并指定Layer的属性作为关键路径进行注册。

//围绕y轴旋转CABasicAnimation *transformAnima = [CABasicAnimation animationWithKeyPath:@"transform.rotation.y"];

设定动画

设定动画的属性和说明

属性说明duration动画的时长repeatCount重复的次数。不停重复设置为 HUGE_VALFrepeatDuration设置动画的时间。在该时间内动画一直执行,不计次数。beginTime指定动画开始的时间。从开始延迟几秒的话,设置为【CACurrentMediaTime() + 秒数】 的方式timingFunction设置动画的速度变化autoreverses动画结束时是否执行逆动画fromValue所改变属性的起始值toValue所改变属性的结束时的值byValue所改变属性相同起始值的改变量

一些常用的animationWithKeyPath值的总结

值说明使用形式transform.scale比例转化@(0.8)transform.scale.x宽的比例@(0.8)transform.scale.y高的比例@(0.8)transform.rotation.x围绕x轴旋转@(M_PI)transform.rotation.y围绕y轴旋转@(M_PI)transform.rotation.z围绕z轴旋转@(M_PI)cornerRadius圆角的设置@(50)backgroundColor背景颜色的变化(id)[UIColor purpleColor].CGColorbounds大小,中心不变[NSValue valueWithCGRect:CGRectMake(0, 0, 200, 200)];position位置(中心点的改变)[NSValue valueWithCGPoint:CGPointMake(300, 300)];contents内容,比如UIImageView的图片imageAnima.toValue = (id)[UIImage imageNamed:@"to"].CGImage;opacity透明度@(0.7)contentsRect.size.width横向拉伸缩放@(0.4)最好是0~1之间的

+(void)basceAnimal1:(UIView *)view{    CABasicAnimation *positionAnima = [CABasicAnimation animationWithKeyPath:@"position.y"];    positionAnima.fromValue = @(view.center.y);    positionAnima.toValue = @(view.center.y + 200);    positionAnima.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];        CABasicAnimation *transformAnima = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];    transformAnima.fromValue = @(0);    transformAnima.toValue = @(13 * M_PI);//    transformAnima.beginTime    transformAnima.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];        //    CABasicAnimation *transformAnima = [CABasicAnimation animationWithKeyPath:@"transform.rotation.y"];//    transformAnima.fromValue = @(0);//    transformAnima.toValue = @(13 * M_PI);//    transformAnima.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];        CAAnimationGroup *animaGroup = [CAAnimationGroup animation];    animaGroup.duration = 1.0f;    animaGroup.fillMode = kCAFillModeForwards;    animaGroup.removedOnCompletion = NO;    animaGroup.autoreverses = YES;    animaGroup.animations = @[positionAnima,transformAnima];    animaGroup.repeatCount = HUGE_VALF;        [view.layer addAnimation:animaGroup forKey:@"Animation"];}

详细参考:http://www.jianshu.com/p/02c341c748f9


2、CAKeyframeAnimation

   CABasicAnimation的区别是:CABasicAnimation只能从一个数值(fromValue)变到另一个数值(toValue),而CAKeyframeAnimation会使用一个NSArray保存这些数值

- values:就是上述的NSArray对象。里面的元素称为”关键帧”(keyframe)。动画对象会在指定的时间(duration)内,依次显示values数组中的每一个关键帧

- path:可以设置一个CGPathRef\CGMutablePathRef,让层跟着路径移动。path只对CALayer的anchorPoint和position起作用。如果你设置了path,那么values将被忽略,还能用UIBezierPath方便绘制路径

- keyTimes:可以为对应的关键帧指定对应的时间点,其取值范围为0到1.0,keyTimes中的每一个时间值都对应values中的每一帧.当keyTimes没有设置的时候,各个关键帧的时间是平分的,keytimes指定的是动画持续时间内的每一帧的时间点,时间点是从0%-100%,时间不可能回退。。。

- keyPath可以使用的key

- #define angle2Radian(angle) ((angle)/180.0*M_PI)

- transform.rotation.x 围绕x轴翻转 参数:角度 angle2Radian(4)

transform.rotation.y 围绕y轴翻转 参数:同上

transform.rotation.z 围绕z轴翻转 参数:同上

transform.rotation 默认围绕z轴

transform.scale.x x方向缩放 参数:缩放比例 1.5

transform.scale.y y方向缩放 参数:同上

transform.scale.z z方向缩放 参数:同上

transform.scale 所有方向缩放 参数:同上

transform.translation.x x方向移动 参数:x轴上的坐标 100

transform.translation.y x方向移动 参数:y轴上的坐标

transform.translation.z x方向移动 参数:z轴上的坐标

transform.translation 移动 参数:移动到的点 (100,100)

opacity 透明度 参数:透明度 0.5

backgroundColor 背景颜色 参数:颜色 (id)[[UIColor redColor] CGColor]

cornerRadius 圆角 参数:圆角半径 5

borderWidth 边框宽度 参数:边框宽度 5

bounds 大小 参数:CGRect

contents 内容 参数:CGImage

contentsRect 可视内容 参数:CGRect 值是0~1之间的小数

hidden 是否隐藏

position

shadowColor

shadowOffset

shadowOpacity

shadowRadius

/**    15  *  将某个view或者layer从起点抛到终点    16  *    17  *  @param obj    被抛的物体    18  *  @param start  起点坐标    19  *  @param end    终点坐标    20  *  @param height 高度,抛物线最高点比起点/终点y坐标最低(即高度最高)所超出的高度    21  */- (void)throwObject:(UIView *)obj from:(CGPoint)start to:(CGPoint)end              height:(CGFloat)height duration:(CGFloat)duration{//     self.showingView = obj;     //初始化抛物线path     CGMutablePathRef path = CGPathCreateMutable();     CGFloat cpx = (start.x + end.x) / 2;     CGFloat cpy = -height;    NSLog(@"cpy = %f",cpy);     CGPathMoveToPoint(path, NULL, start.x, start.y);//    CGPathMoveToPoint(path, NULL, mScreenWidth/2, 0);     CGPathAddQuadCurveToPoint(path, NULL, cpx, cpy, end.x, end.y);    CGPathAddQuadCurveToPoint(path, NULL, mScreenWidth/2, mScreenHeight/2, height, mScreenHeight - height);     CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"position"];     animation.path = path;    animation.fillMode=kCAFillModeForwards;     CFRelease(path);         CABasicAnimation *scaleAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];     scaleAnimation.autoreverses = YES;     scaleAnimation.toValue = [NSNumber numberWithFloat:(CGFloat)((arc4random() % 4) + 4) / 10.0];    scaleAnimation.fillMode=kCAFillModeForwards;//    scaleAnimation.autoreverses = NO;     CAAnimationGroup *groupAnimation = [CAAnimationGroup animation];     groupAnimation.delegate = self;     groupAnimation.repeatCount = 1;     groupAnimation.duration = duration;     groupAnimation.removedOnCompletion = NO;//    groupAnimation.autoreverses = NO;         groupAnimation.animations = @[scaleAnimation, animation];     [obj.layer addAnimation:groupAnimation forKey:@"position scale"];}
+(void)basceAnimal4:(UIView *)view{    CAKeyframeAnimation *animation = [CAKeyframeAnimation animation];    animation.keyPath = @"position";    NSValue *value1=[NSValue valueWithCGPoint:CGPointMake(100, 100)];    NSValue *value2=[NSValue valueWithCGPoint:CGPointMake(200, 100)];    NSValue *value3=[NSValue valueWithCGPoint:CGPointMake(200, 200)];    NSValue *value4=[NSValue valueWithCGPoint:CGPointMake(100, 200)];    NSValue *value5=[NSValue valueWithCGPoint:CGPointMake(100, 100)];        animation.values=@[value1,value2,value3,value4,value5];    animation.repeatCount=MAXFLOAT;    animation.keyTimes = @[@0.0,@0.5,@0.7,@0.75,@0.95];    animation.removedOnCompletion = NO;    animation.fillMode = kCAFillModeForwards;        animation.duration = 4.0f;        animation.timingFunction=[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];        animation.delegate=self;        [view.layer addAnimation:animation forKey:nil];}









0 0
原创粉丝点击