Core Animation Programming Guide学习 Part 3

1. Animation Classes Roadmap

Core Animation provides an expressive set of animation classes you can use in your application:

  • CAAnimation is the abstract class that all animations subclass. CAAnimation adopts the CAMediaTiming protocol which provides the simple duration, speed, and repeat count for an animation. CAAnimation also adopts theCAAction protocol. This protocol provides a standardized means for starting an animation in response to an action triggered by a layer.

    The CAAnimation class also defines an animation’s timing as an instance of CAMediaTimingFunction. The timing function describes the pacing of the animation as a simple Bezier curve. A linear timing function specifies that the animation's pace is even across its duration, while an ease-in timing function causes an animation to speed up as it nears its duration.

  • CAPropertyAnimation is an abstract subclass of CAAnimation that provides support for animating a layer property specified by a key path.

  • CABasicAnimation is a subclass of CAPropertyAnimation that provides simple interpolation for a layer property.

  • CAKeyframeAnimation (a subclass of CAPropertyAnimation) provides support for key frame animation. You specify the key path of the layer property to be animated, an array of values that represent the value at each stage of the animation, as well as arrays of key frame times and timing functions. As the animation runs, each value is set in turn using the specified interpolation.

  • CATransition provides a transition effect that affects the entire layer's content. It fades, pushes, or reveals layer content when animating. On Mac OS X, the stock transition effects can be extended by providing your own custom Core Image filters.

  • CAAnimationGroup allows an array of animation objects to be grouped together and run concurrently.

Figure 1 shows the animation class hierarchy, and also summarizes the properties available through inheritance.

Figure 1  Core Animation classes and protocol Core Animation classes and protocol
2. Keyframe Animations
Keyframe animation, supported in Core Animation by the CAKeyframeAnimation class, is similar to basic animation; however it allows you to specify an array of target values. Each of these target values is interpolated, in turn, over the duration of the animation.

Keyframe values are specified using one of two properties: a Core Graphics path (the path property) or an array of objects (the values property).

A Core Graphics path is suitable for animating a layer’s anchorPoint or position properties, that is, properties that are CGPoints. Each point in the path, except for moveto points, defines a single keyframe segment for the purpose of timing and interpolation. If the path property is specified, the values property is ignored.

By default, as a layer is animated along a CGPath it maintains the rotation to which it has been set. Setting therotationMode property to kCAAnimationRotateAuto or kCAAnimationRotateAutoReverse causes the layer to rotate to match the path tangent.

Providing an array of objects to the values property allows you to animate any type of layer property. For example:

  • Provide an array of CGImage objects and set the animation key path to the content property of a layer. This causes the content of the layer to animate through the provided images.

  • Provide an array of CGRects (wrapped as objects) and set the animation key path to the frame property of a layer. This causes the frame of the layer to iterate through the provided rectangles.

  • Provide an array of CATransform3D matrices (again, wrapped as objects) and set the animation key path to thetransform property. This causes each transform matrix to be applied to the layer’s transform property in turn.

Keyframe animation requires a more complex timing and pacing model than that of a basic animation.

The inherited timingFunction property is ignored. Instead you can pass an optional array ofCAMediaTimingFunction instances in the timingFunctions property. Each timing function describes the pacing of one keyframe to keyframe segment.

While the inherited duration property is valid for CAKeyframeAnimation, you can attain more subtle control of timing by using the keyTimes property.

The keyTimes property specifies an array of NSNumber objects that define the duration of each keyframe segment. Each value in the array is a floating point number between 0.0 and 1.0 and corresponds to one element in the values array. Each element in the keyTimes array defines the duration of the corresponding keyframe value as a fraction of the total duration of the animation. Each element value must be greater than, or equal to, the previous value.

The appropriate values for the keyTimes array are dependent on the calculationMode property.

  • If the calculationMode is set to kCAAnimationLinear, the first value in the array must be 0.0 and the last value must be 1.0. Values are interpolated between the specified keytimes.

  • If the calculationMode is set to kCAAnimationDiscrete, the first value in the array must be 0.0.

  • If the calculationMode is set to kCAAnimationPaced, the keyTimes array is ignored.


3. Transition Animation
Transition animation is used when it is impossible to mathematically interpolate the effect of changing the value of a layer property, or the state of a layer in the layer tree.

The CATransition class provides transition functionality to Core Animation. It is a direct subclass of CAAnimation as it affects an entire layer, rather than a specific property of a layer.

A new instance of CATransition is created using the inherited class method animation. This will create a transition animation with the default values shown in Table 1:

Table 1  Default  CATransition property values

Transition Property

Value

type

Uses a fade transition. The value is kCATransitionFade.

subType

Not applicable.

duration

Uses the duration of the current transaction or 0.25 seconds if the duration has not been set for a transaction. The value is 0.0

timingFunction

Uses linear pacing. The value is nil.

startProgress

0.0

endProgress

1.0

Once created, you can configure the transition animation using one of the predefined transition types or, on Mac OS X, create a custom transition using a Core Image filter.

The predefined transitions are used by setting the type property to one of the constants in Table 2.

Table 2  Common transition types

Transition Type

Description

kCATransitionFade

The layer fades as it becomes visible or hidden.

kCATransitionMoveIn

The layer slides into place over any existing content.

kCATransitionPush

The layer pushes any existing content as it slides into place

kCATransitionReveal

The layer is gradually revealed in the direction specified by the transition subtype.

With the exception of kCATransitionFade, the predefined transition types also allow you to specify a direction for the transition by setting the subType property to one of the constants in Table 3.

Table 3  Common transition subtypes

Transition Subtype Constant

Description

kCATransitionFromRight

The transition begins at the right side of the layer.

kCATransitionFromLeft

The transition begins at the left side of the layer.

kCATransitionFromTop

The transition begins at the top of the layer.

kCATransitionFromBottom

The transition begins at the bottom of the layer.

The startProgress property allows you to change the start point of the transition by setting a value that represents a fraction of the entire animation. For example, to start a transition half way through its progress the startProgressvalue would be set to 0.5. Similarly, you can specify the endProgress value for the transition. The endProgress is the fraction of the entire transition that the transition should stop at. The default values are 0.0 and 1.0, respectively.

 
4. Using a Single Timing Function For a Keyframe Animation

The CAKeyframeAnimation class provides a powerful means of animating layer properties. However,CAKeyframeAnimation does not allow you to specify a single animation timing function that is used for the entire path. Instead you are required to specify the timing using the keyTimes property, or by specifying an array of timing functions in the timingFunctions property.

You can provide a single timing function for the animation by grouping the keyframe animation in aCAAnimationGroup, and setting the group animation’s timing function to the desired CAMediaTimingFunction. The animation group’s timing function and duration take precedence over the keyframe animation’s timing properties.

A code fragment that implements this strategy is shown in Listing 1.

Listing 1  Using a single timing function for a keyframe animation

// create the path for the keyframe animation
CGMutablePathRef thePath = CGPathCreateMutable();
CGPathMoveToPoint(thePath,NULL,15.0f,15.f);
CGPathAddCurveToPoint(thePath,NULL,
                      15.f,250.0f,
                      295.0f,250.0f,
                      295.0f,15.0f);
 
// create an explicit keyframe animation that
// animates the target layer's position property
// and set the animation's path property
CAKeyframeAnimation *theAnimation=[CAKeyframeAnimation
 
                                      animationWithKeyPath:@"position"];
theAnimation.path=thePath;
 
// create an animation group and add the keyframe animation
CAAnimationGroup *theGroup = [CAAnimationGroup animation];
theGroup.animations=[NSArray arrayWithObject:theAnimation];
 
// set the timing function for the group and the animation duration
theGroup.timingFunction=[CAMediaTimingFunction
 
                                functionWithName:kCAMediaTimingFunctionEaseIn];
theGroup.duration=15.0;
// release the path
CFRelease(thePath);
 
 
// adding the animation to the target layer causes it
// to begin animating
[theLayer addAnimation:theGroup forKey:@"animatePosition"];

转载于:https://www.cnblogs.com/pengyingh/articles/2339437.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值