CABasicAnimation动画详解

https://www.jianshu.com/p/0e9af21c0696

该篇文章CABasicAnimation动画类的一些类型展示和使用说明。Talk is cheap.Show the code.

震动效果
-(void)shakeAnimationForView:(UIView *) view
{
    // 获取到当前的View
    CALayer *viewLayer = view.layer;
    // 获取当前View的位置
    CGPoint position = viewLayer.position;
    // 移动的两个终点位置
    CGPoint x = CGPointMake(position.x + 10, position.y);
    CGPoint y = CGPointMake(position.x - 10, position.y);
    // 设置动画
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position"];
    // 设置运动形式
    [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionDefault]];
    // 设置开始位置
    [animation setFromValue:[NSValue valueWithCGPoint:x]];
    // 设置结束位置
    [animation setToValue:[NSValue valueWithCGPoint:y]];
// 设置自动反转 [animation setAutoreverses:YES]; // 设置时间 [animation setDuration:.06]; // 设置次数 [animation setRepeatCount:5]; // 添加上动画 [viewLayer addAnimation:animation forKey:nil]; }
透明效果
-(void)opacityForView:(UIView*)view
{
    CALayer *viewLayer = view.layer;
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"opacity"];
    [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionDefault]];
    animation.fromValue=@0;
    animation.toValue=@1;
    [animation setAutoreverses:YES];
    [animation setDuration:1.0];
    [animation setRepeatCount:3];
    [viewLayer addAnimation:animation forKey:nil];
}
翻转效果
-(void)widthTranfromForView:(UIView*)view
 {
    CALayer *viewLayer = view.layer;
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.scale.x"];
    [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionDefault]];
    animation.fromValue=@0;
    animation.toValue=@1;
    [animation setAutoreverses:YES];
    [animation setDuration:1.0];
    [animation setRepeatCount:3];
    [viewLayer addAnimation:animation forKey:nil];
}
旋转效果
-(void)rotateForView:(UIView*)view
{
    CALayer *viewLayer = view.layer;
    CABasicAnimation*animation=[CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionDefault]];
    animation.fromValue=@0;
    animation.toValue=@(M_PI*2.0);
    animation.cumulative = YES;
    [animation setDuration:1.0];
    [animation setRepeatCount:1];
    [viewLayer addAnimation:animation forKey:nil];
}
大小变化效果
-(void)scaleForView:(UIView*)view
{
    CALayer *viewLayer = view.layer;
    CABasicAnimation*animation=[CABasicAnimation animationWithKeyPath:@"transform.scale"];
    [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionDefault]];
    animation.fromValue=@1;
    animation.toValue=@2;
//        [animation setAutoreverses:YES];
    animation.removedOnCompletion = NO;
    animation.fillMode = kCAFillModeForwards;
    [animation setDuration:1.0];
    [animation setRepeatCount:1];
    [viewLayer addAnimation:animation forKey:nil];
}

还有许多动画效果,这里就不一一例举了,CABasicAnimation的key如下,有兴趣的话,可以用不同的key试试

animationWithKeyPath的值:

transform.scale = 比例轉換

transform.scale.x = 闊的比例轉換

transform.scale.y = 高的比例轉換

transform.rotation.z = 平面圖的旋轉

opacity = 透明度

margin

zPosition

backgroundColor    背景颜色

cornerRadius    圆角

borderWidth

bounds

contents

contentsRect

cornerRadius

frame

hidden

mask

masksToBounds

opacity

position

shadowColor

shadowOffset

shadowOpacity

shadowRadius


作者:homosum_cn
链接:https://www.jianshu.com/p/0e9af21c0696
来源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。
 

CABasicAnimation 几个常用的动画效果

2014年08月21日 14:25:20 iOS探路者 阅读数:1019

 

+(CABasicAnimation *)opacityForever_Animation:(float)time //永久闪烁的动画

{

    CABasicAnimation *animation=[CABasicAnimation animationWithKeyPath:@"opacity"];

    animation.fromValue=[NSNumber numberWithFloat:1.0];

    animation.toValue=[NSNumber numberWithFloat:0.0];

    animation.autoreverses=YES;

    animation.duration=time;

    animation.repeatCount=FLT_MAX;

    animation.removedOnCompletion=NO;

    animation.fillMode=kCAFillModeForwards;

    return animation;

}

 

+(CABasicAnimation *)opacityTimes_Animation:(float)repeatTimes durTimes:(float)time; //有闪烁次数的动画

{

    CABasicAnimation *animation=[CABasicAnimation animationWithKeyPath:@"opacity"];

    animation.fromValue=[NSNumber numberWithFloat:1.0];

    animation.toValue=[NSNumber numberWithFloat:0.4];

    animation.repeatCount=repeatTimes;

    animation.duration=time;

    animation.removedOnCompletion=NO;

    animation.fillMode=kCAFillModeForwards;

    animation.timingFunction=[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];

    animation.autoreverses=YES;

    return  animation;

}

 

+(CABasicAnimation *)moveX:(float)time X:(NSNumber *)x //横向移动

{

    CABasicAnimation *animation=[CABasicAnimation animationWithKeyPath:@"transform.translation.x"];

    animation.toValue=x;

    animation.duration=time;

    animation.removedOnCompletion=NO;

    animation.fillMode=kCAFillModeForwards;

    return animation;

}

 

+(CABasicAnimation *)moveY:(float)time Y:(NSNumber *)y //纵向移动

{

    CABasicAnimation *animation=[CABasicAnimation animationWithKeyPath:@"transform.translation.y"];

    animation.toValue=y;

    animation.duration=time;

    animation.removedOnCompletion=NO;

    animation.fillMode=kCAFillModeForwards;

    return animation;

}

 

+(CABasicAnimation *)scale:(NSNumber *)Multiple orgin:(NSNumber *)orginMultiple durTimes:(float)time Rep:(float)repeatTimes //缩放

{

    CABasicAnimation *animation=[CABasicAnimation animationWithKeyPath:@"transform.scale"];

    animation.fromValue=orginMultiple;

    animation.toValue=Multiple;

    animation.duration=time;

    animation.autoreverses=YES;

    animation.repeatCount=repeatTimes;

    animation.removedOnCompletion=NO;

    animation.fillMode=kCAFillModeForwards;

    return animation;

}

 

+(CAAnimationGroup *)groupAnimation:(NSArray *)animationAry durTimes:(float)time Rep:(float)repeatTimes //组合动画

{

    CAAnimationGroup *animation=[CAAnimationGroup animation];

    animation.animations=animationAry;

    animation.duration=time;

    animation.repeatCount=repeatTimes;

    animation.removedOnCompletion=NO;

    animation.fillMode=kCAFillModeForwards;

    return animation;

}

 

+(CAKeyframeAnimation *)keyframeAniamtion:(CGMutablePathRef)path durTimes:(float)time Rep:(float)repeatTimes //路径动画

{

    CAKeyframeAnimation *animation=[CAKeyframeAnimation animationWithKeyPath:@"position"];

    animation.path=path;

    animation.removedOnCompletion=NO;

    animation.fillMode=kCAFillModeForwards;

    animation.timingFunction=[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];

    animation.autoreverses=NO;

    animation.duration=time;

    animation.repeatCount=repeatTimes;

    return animation;

}

 

+(CABasicAnimation *)movepoint:(CGPoint )point //点移动

{

    CABasicAnimation *animation=[CABasicAnimation animationWithKeyPath:@"transform.translation"];

    animation.toValue=[NSValue valueWithCGPoint:point];

    animation.removedOnCompletion=NO;

    animation.fillMode=kCAFillModeForwards;

    return animation;

}

 

+(CABasicAnimation *)rotation:(float)dur degree:(float)degree direction:(int)direction repeatCount:(int)repeatCount //旋转

{

    CATransform3D rotationTransform  = CATransform3DMakeRotation(degree, 0, 0,direction);

    CABasicAnimation* animation;

    animation = [CABasicAnimation animationWithKeyPath:@"transform"];

 

animation.toValue= [NSValue valueWithCATransform3D:rotationTransform];

    animation.duration= dur;

animation.autoreverses= NO;

    animation.cumulative= YES;

    animation.removedOnCompletion=NO;

    animation.fillMode=kCAFillModeForwards;

    animation.repeatCount= repeatCount; 

animation.delegate= self;

 

return animation;

}

 

实现view放大再缩小的效果

 

- (void)viewDidLoad {

    [super viewDidLoad];

    CALayer *layer = [CALayer layer];

    layer.frame = CGRectMake(50, 200, 50, 50);

    layer.backgroundColor = [UIColor orangeColor].CGColor;

    layer.cornerRadius = 8.0f;

    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.translation.y"];

    animation.duration = 4.0f;

    animation.autoreverses = NO;

    animation.repeatCount = 1;

    animation.toValue = [NSNumber numberWithInt:-10];

    animation.fromValue = [NSNumber numberWithInt:200];

    animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];

    CABasicAnimation *animationZoomIn = [CABasicAnimation animationWithKeyPath:@"transform.scale"];

    animationZoomIn.duration = 2.0f;

    animationZoomIn.autoreverses = NO;

    animationZoomIn.repeatCount = 1;

    animationZoomIn.toValue =[NSNumber numberWithFloat:1.56];

    animationZoomIn.timingFunction=[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];

    CABasicAnimation *animationZoomOut = [CABasicAnimation animationWithKeyPath:@"transform.scale"];

    animationZoomOut.beginTime = 2.0f;

    animationZoomOut.duration = 2.0f;

    animationZoomOut.autoreverses = NO;

    animationZoomOut.repeatCount = 1;

    animationZoomOut.toValue = [NSNumber numberWithFloat:.01];

    animationZoomOut.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];

    CAAnimationGroup *group = [CAAnimationGroup animation];

    group.duration=4.0f;

    group.animations = [NSArray arrayWithObjects: animation, animationZoomIn, animationZoomOut,nil];

    group.removedOnCompletion = NO;

    group.fillMode = kCAFillModeForwards;

    [layer addAnimation:group forKey:nil];

    [self.view.layer addSublayer:layer];

    //layer.hidden=YES;

    

 

}

 

 
 
posted @ 2019-03-21 16:39  sundaysios  阅读(703)  评论(0编辑  收藏  举报