iOS动画---1

/**

iOS动画主要是指CoreAnimation框架。CoreAnimation是iOS和macOS平台上负责图形渲染与动画的基础框架。CoreAnimation可以作用于动画视图或者其他可视元素,为你完成了动画所需的大部分绘帧工作。只需配置少量的动画参数,如开始点的位置和结束点的位置即可使用CoreAnimation的动画效果。CoreAnimation将大部分实际的绘图任务交给了图形硬件来处理,图形硬件会加速图形渲染的速度。这种自动化的图形加速技术让动画拥有更高的帧率并且显示效果更加平滑,不会加重CPU的负担而影响程序的运行速度。

----------------------------------------------------

常用属性

duration : 动画的持续时间

beginTime : 动画的开始时间

repeatCount : 动画的重复次数

autoreverses : 执行的动画按照原动画返回执行

timingFunction : 控制动画的显示节奏系统提供五种值选择,分别是:

kCAMediaTimingFunctionLinear 线性动画

kCAMediaTimingFunctionEaseIn 先快后慢

kCAMediaTimingFunctionEaseOut 先慢后快

kCAMediaTimingFunctionEaseInEaseOut 先慢后快再慢

kCAMediaTimingFunctionDefault 默认,也属于中间比较快

delegate : 动画代理。能够检测动画的执行和结束。

------------------------------------------

@interface NSObject (CAAnimationDelegate)

- (void)animationDidStart:(CAAnimation *)anim;

- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag;

@end

path:关键帧动画中的执行路径

type:过渡动画的动画类型,系统提供了四种过渡动画。

kCATransitionFade 渐变效果

kCATransitionMoveIn 进入覆盖效果

kCATransitionPush 推出效果

kCATransitionReveal 揭露离开效果

subtype : 过渡动画的动画方向

kCATransitionFromRight 从右侧进入

kCATransitionFromLeft 从左侧进入

kCATransitionFromTop 从顶部进入

kCATransitionFromBottom 从底部进入

*/

- (void)initAnimation{

/*iOS动画的调用方式*/

/*------1-----------UIView代码块调用---------------------------*/

//    UIView* animationView = [[UIView alloc]init];

//    [self.view addSubview:animationView];

//    animationView.backgroundColor = [UIColor yellowColor];

//    animationView.frame = CGRectMake(0, screenWidth/2-50, 50, 50);//动画起始位置

//    [UIView animateWithDuration:5 animations:^{

//        animationView.frame = CGRectMake(screenWidth-50, screenWidth/2-25, 50, 50);//动画结束位置

//

//    } completion:^(BOOL finished) {

//        animationView.frame = CGRectMake(screenWidth/2-50, screenWidth/2-25, 50, 50);//动画完成之后

//

//    }];

//

/*-------2----------UIView  [begin commit]模式---------------------------*/

//    UIView* animationView = [[UIView alloc]init];

//    [self.view addSubview:animationView];

//    animationView.backgroundColor = [UIColor redColor];

//    animationView.frame = CGRectMake(0, screenWidth/2-50, 50, 50);//动画起始位置

//    [UIView beginAnimations:nil context:nil];//开始动画

////    [UIView setAnimationDelay:5.f];//延迟动画执行时间

//    [UIView setAnimationRepeatCount:3];//动画执行次数

//    [UIView setAnimationDuration:5.0f];//动画时间

//    animationView.frame = CGRectMake(screenWidth-50, screenWidth/2-25, 50, 50);//动画终止位置

//    [UIView commitAnimations];//结束动画

/*-------3----------使用CoreAnimation中的类---------------------------*/

//    UIView* animationView = [[UIView alloc]init];

//    [self.view addSubview:animationView];

//    animationView.backgroundColor = [UIColor redColor];

//    animationView.frame = CGRectMake(screenWidth/2-25, screenHeight/2-75, 50, 50);//此处决定动画的原始位置和最终位置

//

//    CABasicAnimation* basicAnimation = [CABasicAnimation animationWithKeyPath:@"position"];//创建基础动画---指定position属性移动

/*--------基础动画位移-------------*/

//    basicAnimation.fromValue = [NSValue valueWithCGPoint:CGPointMake(0, screenHeight/2-75)];//动画的起始位置

//    basicAnimation.toValue = [NSValue valueWithCGPoint:CGPointMake(screenWidth, screenHeight/2-75)];//动画的终止位置

//    basicAnimation.duration = 5.0f;

//    basicAnimation.fillMode = kCAFillModeForwards;

//    basicAnimation.removedOnCompletion = NO;//注意点:如果basicAnimation.fillMode = kCAFillModeForwards;

//    basicAnimation.removedOnCompletion = NO;则图层在动画执行完毕之后,会保持显示动画执行后的状态。但在实际上,图层的属性值还是动画执行前的初始值,并没有改变

//    [animationView.layer addAnimation:basicAnimation forKey:@"positionAnimation"];//为视图的layer添加设置完成的动画,可以给key指定任意名字

/*--------基础动画位移-------------*/

/*----------------------------iOS动画的使用-------------------------------*/

/*

基础动画---CABasicAnimation

主要提供了对于CALayer对象中的可变属性进行简单动画的操作。比如:位移、透明度、缩放、旋转、背景色等等

*/

/*--------基础动画缩放-------------*/

//    UIView* animationView = [[UIView alloc]init];

//    [self.view addSubview:animationView];

//    animationView.backgroundColor = [UIColor redColor];

//    animationView.frame = CGRectMake(screenWidth/2-25, screenHeight/2-75, 50, 50);//此处决定动画的原始位置和最终位置

//

//    CABasicAnimation* basicAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];//创建基础动画-

//    basicAnimation.autoreverses = YES;//动画结束时执行逆动画

//    basicAnimation.duration = 5.0f;

//    basicAnimation.repeatCount = 1;

//    basicAnimation.fromValue = [NSNumber numberWithFloat:1.0];//开始时的倍率

//

//    basicAnimation.toValue = [NSNumber numberWithFloat:2.0];//结束时的倍率

//

//    [animationView.layer addAnimation:basicAnimation forKey:@"scale-layer"];

/*--------基础动画缩放-------------*/

//    - 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)

/*--------基础动画旋转-------------*/

//    UIView* animationView = [[UIView alloc]init];

//    [self.view addSubview:animationView];

//    animationView.backgroundColor = [UIColor redColor];

//    animationView.frame = CGRectMake(screenWidth/2-25, screenHeight/2-75, 50, 50);//此处决定动画的原始位置和最终位置

//

//    CABasicAnimation* basicAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.x"];//创建基础动画-对y轴进行旋转,指定Z轴的话,就会绕中心旋转

//    basicAnimation.autoreverses = YES;//动画结束时执行逆动画

//    basicAnimation.duration = 5.0f;

//    basicAnimation.repeatCount = 1;

//    //设定旋转角度

//    basicAnimation.fromValue = [NSNumber numberWithFloat:0.0];//起始角度

//

//    basicAnimation.toValue = [NSNumber numberWithFloat:22*M_PI];//终止角度

//

//    [animationView.layer addAnimation:basicAnimation forKey:@"rotate-layer"];

//    说明:CABasicAnimation可看做是最多只有2个关键帧的CAKeyframeAnimation

/*--------基础动画旋转-------------*/

/*------------------------关键帧动画CAKeyframeAnimation--------------------------------------------*/

/*

CAKeyframeAnimation和CABasicAnimation都属于CAPropertAnimation的子类,CABasicAnimation只能从一个数值fromValue变换成另一个数值toValue,而CAKeyframeAnimation则会使用一个NSArray保存一组关键帧

重要属性

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

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

keyTimes:可以为对应的关键帧指定对应的时间点,其取值范围为0到1.0,keyTimes中的每一个时间值都对应values中的每一帧.当keyTimes没有设置的时候,各个关键帧的时间是平分的。

*/

/*-----------------圆形路径--------------------*/

//    UIView* animationView = [[UIView alloc]init];

//    [self.view addSubview:animationView];

//    animationView.backgroundColor = [UIColor redColor];

//    animationView.frame = CGRectMake(screenWidth/2-100, screenHeight/2-100, 50, 50);//此处决定动画的原始位置和最终位置

//    CAKeyframeAnimation* keyFrameAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];

//    UIBezierPath* bezierPath = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(screenWidth/2-100, screenHeight/2-100, 200, 200)];

//    keyFrameAnimation.path = bezierPath.CGPath;

//    keyFrameAnimation.duration = 10.f;

//    [animationView.layer addAnimation:keyFrameAnimation forKey:@"pathAnimation"];

/*组动画---------CAAnimationGroup*/

/*

CAAnimationGroup是CAAnimation的子类,可以保存一组动画对象,将 CAAnimationGroup对象加入层后,组中所有对象可以同时并行运行

重要属性---animations:用来保存一组动画对象的NSArray

*/

}

你可能感兴趣的:(iOS动画---1)