iOS动画之CABasicAnimation

我们首先来看看Core Animation类的继承关系图

687474703a2f2f696d672e6d792e6373646e2e6e65742f75706c6f6164732f3230313530372f32332f313433373631373536325f333139302e706e67.png

下面通过苹果官方的API来介绍CABasicAnimation

Class
CABasicAnimation
An object that provides basic, single-keyframe animation capabilities for a layer property.
//为图层属性提供基本的单关键帧动画功能的对象。介绍了这个类的作用
//关键词:basic, single-keyframe animation(单关键帧,你可以理解成CABasicAnimation是CAKeyframeAnimation的特殊化) 

Overview(概述)
You create an instance of CABasicAnimation using the inherited animationWithKeyPath: method, specifying the key path of the property to be animated in the render tree.
//你可以使用继承的animationWithKeyPath:方法创建CABasicAnimation的实例,指定要在渲染树中进行动画处理的属性的键路径。

For example, you can animate a layer's scalar (i.e. containing a single value) properties such as its opacity. Listing 1 fades in a layer by animating its opacity from 0 to 1.
//如列表1中,你可以为涂层属性(不透明度)设置动画,使其不透明度从0到1淡化

Listing 1 
Creating an animation to animate opacity
let animation = CABasicAnimation(keyPath: "opacity") 
animation.fromValue = 0 
animation.toValue = 1
Non-scalar properties, such as backgroundColor, can also be animated. Core Animation will interpolate between the fromValue color and the toValue color. The animation created in Listing 2 fades a layer's background color from red to blue.
//非标量属性,如backgroundColor,也可以是动画。 核心动画将在fromValue颜色和toValue颜色之间进行插值。 清单2中创建的动画会将图层的背景颜色从红色渐变为蓝色

Listing 2 
Creating an animation to animate background color.
let animation = CABasicAnimation(keyPath: "backgroundColor")
animation.fromValue = NSColor.red.cgColor
animation.toValue = NSColor.blue.cgColor
If you want to animate the individual(个人) components(组件、元件) of a non-scalar property with different values, you pass the values to toValue and fromValue as arrays. The animation created in Listing 3 moves a layer from (0, 0) to (100, 100).
//如果要为具有不同值的非标量属性的各个组件设置动画,请将值传递到toValue和fromValue作为数组。 清单3中创建的动画将一个图层从(0,0)移动到(100,100)。

Listing 3 
Creating an animation to animate position
let animation = CABasicAnimation(keyPath: "position")
animation.fromValue = [0, 0]
animation.toValue = [100, 100]
The keyPath can access the individual components of a property. For example, the Listing 4 stretches a layer by animating its transform object's x from 1 to 2.
//keyPath可以访问属性的各个组件。 例如,清单4通过将其变换对象的x从1动画化为2来展开图层。

Listing 4 
Creating an animation to animate scale
let animation = CABasicAnimation(keyPath: "transform.scale.x")
animation.fromValue = 1
animation.toValue = 2

Setting Interpolation Values(设置插值)
The fromValue, byValue and toValue properties define the values being interpolated between. All are optional, and no more than two should be non-nil. The object type should match the type of the property being animated.
//fromValue,byValue和toValue属性定义在之间插入的值。 所有都是可选的,不超过两个应该是非零。 对象类型应与要动画的属性的类型匹配
//注意:不超过两个是非零,byValue是一个相对值,例如,如果指定了`fromValue`等于2,`toValue`等于4,`byValue`等于3,那么Core Animation就不知道结果到底是4(`toValue`)还是5(`fromValue + byValue`)了
The interpolation values are used as follows:
//内插值使用如下
Both fromValue and toValue are non-nil. Interpolates between fromValue and toValue.
//fromValue和toValue都是非零。 插值(也就是动画执行)从fromValue到toValue
fromValue and byValue are non-nil. Interpolates between fromValue and (fromValue + byValue).
//fromValue和byValue是非零。 插值(也就是动画执行)从fromValue到fromValue + byValue。
byValue and toValue are non-nil. Interpolates between (toValue - byValue) and toValue.
//byValue和toValue是非零。 插值(也就是动画执行)从toValue - byValue到toValue
fromValue is non-nil. Interpolates between fromValue and the current presentation value of the property.
//fromValue是非零,fromValue->当前
toValue is non-nil. Interpolates between the current value of keyPath in the target layer’s presentation layer and toValue.
//toValue是非零,当前->toValue
byValue is non-nil. Interpolates between the current value of keyPath in the target layer’s presentation layer and that value plus byValue.
//byValue是非零,当前值->当前值 + byValue
All properties are nil. Interpolates between the previous value of keyPath in the target layer’s presentation layer and the current value of keyPath in the target layer’s presentation layer.
//super与当前的对比,没意义
Symbols

Interpolation values
fromValue
Defines the value the receiver uses to start interpolation.
//定义接收器用于开始插值的值。
toValue
Defines the value the receiver uses to end interpolation.
//定义接收器用于结束插值的值
byValue
Defines the value the receiver uses to perform relative interpolation.
//定义接收器用于执行相对插值的值

示例

从苹果官方API我们可以了解到CABasicAnimation(基础动画)算是CAKeyframeAnimation(关键帧动画)的特殊化,CABaseAnimation只能从一个数值(fromValue)变换成另一个数值(toValue),而CAKeyframeAnimation则会使用一个NSArray保存一组关键帧.

使用CABasicAnimation可操控的几种属性

  • opacity (透明度)
  • backgroundColor (背景色)
  • position (位置)
  • transform.scale.x(多种)
1 opacity

 CABasicAnimation *anima = [CABasicAnimation animationWithKeyPath:@"opacity"];
 anima.fromValue = [NSNumber numberWithFloat:1.0f];
 anima.toValue = [NSNumber numberWithFloat:0.2f];
 anima.duration = 1.0f;
 [_demoView.layer addAnimation:anima forKey:@"opacityAniamtion"];

2 backgroundColor

CABasicAnimation *anima = [CABasicAnimation animationWithKeyPath:@"backgroundColor"];
anima.toValue =(id) [UIColor greenColor].CGColor;
anima.duration = 1.0f;
[_demoView.layer addAnimation:anima forKey:@"backgroundAnimation"];

3 position

CABasicAnimation *anima = [CABasicAnimation animationWithKeyPath:@"position"];
anima.fromValue = [NSValue valueWithCGPoint:CGPointMake(0, SCREEN_HEIGHT/2-75)];
anima.toValue = [NSValue valueWithCGPoint:CGPointMake(SCREEN_WIDTH, SCREEN_HEIGHT/2-75)];
anima.duration = 3.0f;
//如果fillMode=kCAFillModeForwards和removedOnComletion=NO,那么在动画执行完毕后,图层会保持显示动画执行后的状态。但在实质上,图层的属性值还是动画执行前的初始值,并没有真正被改变。
//anima.fillMode = kCAFillModeForwards;
//anima.removedOnCompletion = NO;
anima.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];
[_demoView.layer addAnimation:anima forKey:@"positionAnimation"];

4 transform

//缩放
CABasicAnimation *anima = [CABasicAnimation animationWithKeyPath:@"transform.scale"];//同上
anima.toValue = [NSNumber numberWithFloat:2.0f];
anima.duration = 3.0f;
[_demoView.layer addAnimation:anima forKey:@"scaleAnimation"];


//旋转
CABasicAnimation *anima = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];//绕着z轴为矢量,进行旋转(@"transform.rotation.z"==@@"transform.rotation")
anima.toValue = [NSNumber numberWithFloat:M_PI];
anima.duration = 1.0f;
[_demoView.layer addAnimation:anima forKey:@"rotateAnimation"];

你可能感兴趣的:(iOS动画之CABasicAnimation)