iOS 动画

iOS 动画分为两种,一种是UIView的视图动画,一种是Layer的动画,本质上UIView的动画也是基于Layer的

一、UIView动画(UIView的动画又分为一般的方式和block的方式)

1、一般方式

[UIView beginAnimations:@“kkkk” context:nil];//设置动画 kkkk为动画名称

[UIView setAnimationDuration:3];//定义动画持续时间

[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut]; //setAnimationCurve来定义动画加速或减速方式

[UIView setAnimationTransition:UIViewAnimationTransitionCurlDown forView:self.window cache:YES];

//设置动画的样式  forView为哪个view实现这个动画效果

[UIView setAnimationDelay:3]; //设置动画延迟多久执行

[UIView setAnimationDelegate:self];  //设置动画的代理 实现动画执行前后的方法 在commitAnimation之前设置

[UIView setAnimationDidStopSelector:@selector(stop)];//设置动画结束后执行的方法

[UIView setAnimationWillStartSelector:@selector(star)];//设置动画将要开始执行的方法

[UIView commitAnimations]; //提交动画

2、Block方式

[UIView animateWithDuration:3 animations:^(void){

           

      //这里相当于在begin和commint之间

    }completion:^(BOOL finished){

         //这里相当于动画执行完成后要执行的方法,可以继续嵌套block

    }];


方法一:

[UIView animateWithDuration:4.0//动画时长

                 animations:^{

                     // code

                 }];

方法二:

[UIView animateWithDuration:4.0//动画时长

animations:^{

// code...

}

completion:^(BOOL finished) {

//动画完成后执行

// code...

}];

方法三:

[UIView animateWithDuration:4.0//动画时长

  delay:2.0//动画延迟

options:UIViewAnimationOptionCurveEaseIn//动画过渡效果

animations:^{

// code...

}

completion:^(BOOL finished) {

//动画完成后执行

// code...

}];

方法四,Spring Animationring Animation):

IOS7开始,系统动画效果广泛应用Spring Animation

[UIView animateWithDuration:4.0//动画时长

  delay:0.0//动画延迟

usingSpringWithDamping:1.0//类似弹簧振动效果 0~1

  initialSpringVelocity:5.0//初始速度

options:UIViewAnimationOptionCurveEaseInOut//动画过渡效果

animations:^{

// code...

CGPoint point = _imageView.center;

point.y += 150;

[_imageView setCenter:point];

} completion:^(BOOL finished) {

//动画完成后执行

// code...

[_imageView setAlpha:1];

}];

usingSpringWithDamping:它的范围为 0.0f 1.0f,数值越小「弹簧」的振动效果越明显。

initialSpringVelocity:初始的速度,数值越大一开始移动越快。值得注意的是,初始速度取值较高而时间较短时,也会出现反弹情况。

转:Spring Animation是线性动画或 ease-out动画的理想替代品。由于 iOS 本身大量使用的就是 SpringAnimation,用户已经习惯了这种动画效果,因此使用它能使 App让人感觉更加自然,用 Apple的话说就是「instantly familiar」。此外,SpringAnimation不只能对位置使用,它适用于所有可被添加动画效果的属性。

方法五,关键帧动画:

UIView动画已经具备高级的方法来创建动画,而且可以更好地理解和构建动画。IOS7以后苹果新加了一个animateKeyframesWithDuration的方法,我们可以使用它来创建更多更复杂更酷炫的动画效果,而不需要去使用到核心动画(CoreAnimatino)。

创建关键帧方法:

/**

 *  添加关键帧方法

 *

 *  @param duration  动画时长

 *  @param delay     动画延迟

 *  @param options   动画效果选项

 *  @param animations动画执行代码

 *  @param completion动画结束执行代码

 */

+ (void)animateKeyframesWithDuration:(NSTimeInterval)duration

                               delay:(NSTimeInterval)delay

                             options:(UIViewKeyframeAnimationOptions)options

                          animations:(void (^)(void))animations

                          completion:(void (^)(BOOL finished))completion;

添加关键帧方法:

/**

 *  添加关键帧

 *

 *  @param frameStartTime动画相对开始时间

 *  @param frameDuration 动画相对持续时间

 *  @param animations    动画执行代码

 */

+ (void)addKeyframeWithRelativeStartTime:(double)frameStartTime

                        relativeDuration:(double)frameDuration

                              animations:(void (^)(void))animations;

以上说的相对时间,也就是说:它们自身会根据动画总持续时长自动匹配其运行时长

下面用一个简单的示例作解答,彩虹变化视图:

void (^keyFrameBlock)() = ^(){

    // 创建颜色数组

    NSArray *arrayColors = @[[UIColor orangeColor],

    [UIColor yellowColor],

    [UIColor greenColor],

    [UIColor blueColor],

    [UIColor purpleColor],

    [UIColor redColor]];

    NSUInteger colorCount = [arrayColors count];

    // 循环添加关键帧

    for (NSUInteger i =0; i < colorCount; i++) {

        [UIView addKeyframeWithRelativeStartTime:i / (CGFloat)colorCount

      relativeDuration:1 / (CGFloat)colorCount

            animations:^{

                [_graduallyView setBackgroundColor:arrayColors[i]];

            }];

    }

};

[UIView animateKeyframesWithDuration:4.0

      delay:0.0

    options:UIViewKeyframeAnimationOptionCalculationModeCubic |UIViewAnimationOptionCurveLinear

animations:keyFrameBlock

completion:^(BOOL finished) {

    //动画完成后执行

    // code...

}];


二、Layer (又分为隐式动画和显示动画)

1、显式动画:我们是 显式 的向一个 CALayer 添加了一个动画,所以这种方式叫做显式动画 CAAnimation

显示动画又分为基础动画(CABasicAnimation)、关键帧动画(CAKeyframeAnimation)、组动画(CAAnimationGroup)、过渡动画(CATransition

CAReplicatorLayer可以复制自己子层的layer,并且复制的出来的layer和原来的子layer拥有相同的动效

  • 1.CABasicAnimation
  • 通过设定起始点,终点,时间,动画会沿着你这设定点进行移动。可以看做特殊的CAKeyFrameAnimation
  • 2.CAKeyframeAnimation
  • Keyframe顾名思义就是关键点的frame,你可以通过设定CALayer的始点、中间关键点、终点的frame,时间,动画会沿你设定的轨迹进行移动
  • 3.CAAnimationGroup
  • Group也就是组合的意思,就是把对这个Layer的所有动画都组合起来。PS:一个layer设定了很多动画,他们都会同时执行,如何按顺序执行我到时候再讲。
  • 4.CATransition
  • 这个就是苹果帮开发者封装好的一些动画

1: CABasicAnimation 

CABasicAnimation的基本使用顺序

1.引用QuartzCore.framework

"QuartzCore.framework"这个库添加到项目中。并且在需要使用CABaseAnimation类的地方import头文件。

#import <QuartzCore/QuartzCore.h>  


2.CABaseAnimation的实例化以及关键路径的注册

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

// 指定position属性  CABasicAnimation *animation =      [CABasicAnimation animationWithKeyPath:@"position"];  


3.设定动画

设定动画的属性。以下是属性及其对应的说明:

4.设定动画的开始帧和结束帧

设定动画开始和结束帧时的状态。设定的值会变为KeyPath所指定的属性的值。

5.添加动画

Layer添加设置完成的动画,可以给Key指定任意名字。

[myView.layer addAnimation:animation forKey:@"move-layer"]; 

CABasicAnimation使用时候的注意点

 

CABasicAnimation正在进行动画的时候,点击了Home按钮后再回到app的时候,动画会被清空。

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

- 属性解析: 

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

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

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

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


2、隐式动画,即系统自动添加上的动画

    CALayer *layer =[CALayerlayer];

    layer.backgroundColor = [UIColorgreenColor].CGColor;

    layer.frame = CGRectMake(0,0,100,100);

    [self.view.layeraddSublayer:layer];

 

    layer.frame =CGRectOffset(layer.frame,100,0);

(在创建隐式动画的时候,系统会隐式的创建一个动画事务,以保证所有的动画能同时进行)除此之外,还可以显式的创建一个事务。

       [CATransactionbegin];

    [CATransactionsetDisableActions:YES];

    ///...

    layer.frame = CGRectOffset(layer.frame,100,0);

    ///...

    [CATransactioncommit];


参考链接:http://ios.jobbole.com/84232/

            仿射变换:http://www.jianshu.com/p/6c09d138b31d

           uiview动画:    https://www.tuicool.com/articles/BjMrQne

            核心动画:http://www.cnblogs.com/iOS-mt/p/4194575.html

                        http://www.cnblogs.com/iBaby/p/3818445.html

              CAReplicatorLayerhttp://blog.csdn.net/u014084081/article/details/49421011

                                             http://www.jianshu.com/p/a927157ac62a




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值