IOS动画之CAAnimation动画

 

CAAnimation采用了CAMediaTiming协议,可以调整时间,包括持续时间,速度,重复次数;采用了CAAction协议,可以通过响应动作的方式来显示动画.
CAAnimation的一些派生类:
CATransition 提供渐变效果:(推拉push效果,消退fade效果,揭开reveal效果)
CAAnimationGroup 允许多个动画同时播放
CABasicAnimation 提供了对单一动画的实现
CAKeyframeAnimation 关键桢动画,可以定义行动路线
CAConstraint 约束类,在布局管理器类中用它来设置属性
CAConstraintLayoutManager 约束布局管理器,是用来将多个CALayer进行布局的.各个CALayer是通过名称来区分,而布局属性是通过CAConstraint来设置的.
CATransaction 事务类,可以对多个layer的属性同时进行修改.它分隐式事务,和显式事务.

--------------------------CATransition动画---------------------------------

  CATransition *transition = [CATransition animation];
    transition.duration = 1.0f;  
/* 间隔时间*/
    transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]; /* 动画的开始与结束的快慢*/
    transition.type = @"rippleEffect"; 
/* 各种动画效果*/

//@"cube" @"moveIn" @"reveal" @"fade"(default) @"pageCurl" @"pageUnCurl" @"suckEffect" @"rippleEffect" @"oglFlip"

{这种动画不设置subtilte

  1. pageCurl   向上翻一页  
  2. pageUnCurl 向下翻一页  
  3. rippleEffect 滴水效果  
  4. suckEffect 收缩效果,如一块布被抽走  
  5. cube 立方体效果  
  6. oglFlip 上下翻转效果

}

transition.type = @"rippleEffect"; /* 各种动画效果*/

CATransition *animation=[CATransition animation];

 animation.delegate=self;
 animation.duration=1.0f;

animation.timingFunction=UIViewAnimationCurveEaseInOut;    

animation.type=kCATransitionMoveIn;

animation.subtype=kCATransitionFromTop;
 [myView.layer addAnimation:animation forKey:@"move in"];

--------------------------CABasicAnimation动画---------------------------------
{

 

 

transform.scale    缩放                        transform.scale.x                                  transform.scale.y          

transform.rotation.z---z轴旋转            transform.rotation.x---x轴旋转           transform.rotation.y---y轴旋转   

transform.translation.x---横向移动      transform.translation.y---纵向移动     

opacity---不透明度    cornerRadius 改变半径

 

 

 

+(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;

} 

//界限

CABasicAnimation *boundsAnimation = [CABasicAnimationanimationWithKeyPath:@"bounds"];
boundsAnimation.fromValue = [NSValue valueWithCGRect: logoLayer.bounds];
boundsAnimation.toValue = [NSValue valueWithCGRect:CGRectZero];


//透明度变化
CABasicAnimation *opacityAnimation = [CABasicAnimationanimationWithKeyPath:@"opacity"];
opacityAnimation.fromValue = [NSNumber numberWithFloat:1.0];
opacityAnimation.toValue = [NSNumber numberWithFloat:0.5];
//位置移动

CABasicAnimation *animation  = [CABasicAnimation animationWithKeyPath:@"position"];
animation.fromValue =  [NSValue valueWithCGPoint: logoLayer.position];
CGPoint toPoint = logoLayer.position;
toPoint.x += 180;
animation.toValue = [NSValue valueWithCGPoint:toPoint];
//旋转动画
CABasicAnimation* rotationAnimation =
       [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];//"z"还可以是“x”“y”,表示沿z轴旋转
rotationAnimation.toValue = [NSNumber numberWithFloat:(2 * M_PI) * 3]; 
rotationAnimation.duration = 2.0f;
rotationAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]; //缓入缓出


//缩放动画

CABasicAnimation *scaleAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
scaleAnimation.fromValue = [NSNumber numberWithFloat:0.0];
scaleAnimation.toValue = [NSNumber numberWithFloat:1.0];
scaleAnimation.duration = 2.0f;
scaleAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
 
--------------------------CAAnimationGroup组合动画--------------------------

CAAnimationGroup *animationGroup = [CAAnimationGroup animation];
animationGroup.duration = 2.0f;
animationGroup.autoreverses = YES;   //是否重播,原动画的倒播
animationGroup.repeatCount = NSNotFound;//HUGE_VALF;     //HUGE_VALF,源自math.h
[animationGroup setAnimations:[NSArray arrayWithObjects:rotationAnimation, scaleAnimation, nil]];
//将上述两个动画编组
[logoLayer addAnimation:animationGroup forKey:@"animationGroup"];
}
//去掉所有动画
[logoLayer removeAllAnimations];
//去掉key动画
[logoLayer removeAnimationForKey:@"animationGroup"];  

+(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--------------------------

+(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, 00,direction);

    CABasicAnimation* animation;

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

   animation.toValue= [NSValue valueWithCATransform3D:rotationTransform];

    animation.duration= dur;

animation.autoreversesNO;

    animation.cumulativeYES;

    animation.removedOnCompletion=NO;

    animation.fillMode=kCAFillModeForwards;

    animation.repeatCount= repeatCount; 

animation.delegateself;

return animation;

}

 

-----------------------------------------------------2.CAAnimation---------------------------------------------------------------------------------------

需要添加库,和包含头文件

caanimation有多个子类

CABasicAnimation

CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"opacity"];
//@""里的字符串有多种,可以自己找相关资料,一定要填对,动画才会执行 opacity设置透明度 bounds.size设置大小
[animation setFromValue:[NSNumber numberWithFloat:1.0]]; //设置透明度从几开始
[animation setToValue:[NSNumber numberWithFloat:0.3]];//设置透明度到几结束
[animation setDuration:0.1]; //设置动画时间
[animation setRepeatCount:100000];//设置重复时间
[animation setRepeatDuration:4];  //会限制重复次数
[animation setAutoreverses:NO];//设置是否从1.0到0.3 再从0.3到1.0 为一次  如果设置为NO则 1.0到0.3为一次
[animation setRemovedOnCompletion:YES]; //完成时移出动画 默认也是
[view.layer addAnimation:animation forKey:@"abc"];//执行动画

CAKeyframeAnimation

CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"position"];//设置view从初始位置经过一系列点
NSArray *postionAraay = [NSArray arrayWithObjects:[NSValue valueWithCGPoint:CGPointMake(100, 20)], [NSValue valueWithCGPoint:CGPointMake(40, 80)],[NSValue valueWithCGPoint:CGPointMake(30, 60)],[NSValue valueWithCGPoint:CGPointMake(20, 40)],[NSValue valueWithCGPoint:CGPointMake(0, 100)],nil];//设置点
   
NSArray *times = [NSArray arrayWithObjects:[NSNumber numberWithFloat:0.3],[NSNumber numberWithFloat:0.5],[NSNumber numberWithFloat:0.6],[NSNumber numberWithFloat:0.1],[NSNumber numberWithFloat:1.0], nil];  //设置移动过程的时间

[animation setKeyTimes:times];
[animation setValues:postionAraay];
[animation setDuration:5]; //设置动画时间
[bigImage.layer addAnimation:animation forKey:@"dd"]; //执行动画

CATransition

CATransition *animation = [CATransition animation];
animation.duration = 0.5f;
animation.timingFunction = UIViewAnimationCurveEaseInOut;
animation.fillMode = kCAFillModeForwards;
    /*
     kCATransitionFade;
     kCATransitionMoveIn;
     kCATransitionPush;
     kCATransitionReveal;
     */
    /*
     kCATransitionFromRight;
     kCATransitionFromLeft;
     kCATransitionFromTop;
     kCATransitionFromBottom;
     */
animation.type = kCATransitionPush;
animation.subtype = kCATransitionFromBottom;
[view.layer addAnimation:animation forKey:animation];
type也可以直接用字符串
/*
     cube    suckEffect 卷走
     oglFlip    翻转
     rippleEffect  水波
     pageCurl   翻页
     pageUnCurl
     cameraIrisHollowOpen
     cameraIrisHollowClose
-----------------------------------------------------2.CAAnimation---------------------------------------------------------------------------------------

IOS动画之CAAnimation动画,,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。