CA关键帧动画

发布于 2024-09-28 23:12:24 字数 1167 浏览 0 评论 0原文

您好,我正在从多个图像创建关键帧动画。我的问题是我希望动画立即从一个图像变为下一个图像而不是淡入淡出。

    CALayer *animLayer = [CALayer layer];
    animLayer.bounds = CGRectMake(0, 0, width, height);
    animLayer.position = CGPointMake(0, 0);

    CAKeyframeAnimation *customFrameAnimation = [CAKeyframeAnimation animationWithKeyPath:@"contents"];
    NSArray *sizeValues = [NSArray arrayWithObjects:(id)image1, (id)image2, nil];
    NSArray *times = [NSArray arrayWithObjects:[NSNumber numberWithFloat:0.0f], [NSNumber numberWithFloat:0.5f], nil]; 
    NSArray *timingFunctions = [NSArray arrayWithObjects: [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionDefault], [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionDefault], nil];

    [customFrameAnimation setValues:sizeValues];
    [customFrameAnimation setKeyTimes:times];

    customFrameAnimation.duration=5.0;
    customFrameAnimation.beginTime = 1e-100;
    customFrameAnimation.fillMode = kCAFillModeRemoved;
    customFrameAnimation.timingFunctions = timingFunctions;
    customFrameAnimation.removedOnCompletion = YES;
    [animLayer addAnimation:customFrameAnimation forKey:nil]; 

提前致谢。

Hi i'm creating a Keyframe animation from multiple images. My problem is i would like the animation to instantly change from one image to the next rather than a fade.

    CALayer *animLayer = [CALayer layer];
    animLayer.bounds = CGRectMake(0, 0, width, height);
    animLayer.position = CGPointMake(0, 0);

    CAKeyframeAnimation *customFrameAnimation = [CAKeyframeAnimation animationWithKeyPath:@"contents"];
    NSArray *sizeValues = [NSArray arrayWithObjects:(id)image1, (id)image2, nil];
    NSArray *times = [NSArray arrayWithObjects:[NSNumber numberWithFloat:0.0f], [NSNumber numberWithFloat:0.5f], nil]; 
    NSArray *timingFunctions = [NSArray arrayWithObjects: [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionDefault], [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionDefault], nil];

    [customFrameAnimation setValues:sizeValues];
    [customFrameAnimation setKeyTimes:times];

    customFrameAnimation.duration=5.0;
    customFrameAnimation.beginTime = 1e-100;
    customFrameAnimation.fillMode = kCAFillModeRemoved;
    customFrameAnimation.timingFunctions = timingFunctions;
    customFrameAnimation.removedOnCompletion = YES;
    [animLayer addAnimation:customFrameAnimation forKey:nil]; 

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

魂牵梦绕锁你心扉 2024-10-05 23:12:24

您的动画需要将其计算模式设置为 kCAAnimationDiscrete。

看看 keyTimes 文档 描述了如何使用calculationMode:

keyTimes 中的适当值
数组依赖于
计算模式属性。

  • 如果计算模式设置为
    kCAAnimationLinear,第一个值
    数组必须是 0.0 并且最后一个
    值必须为 1.0。值为
    在指定键之间插入
    次。

  • 如果设置了计算模式
    kCAAnimationDiscrete,第一个
    数组中的值必须为 0.0。

  • 如果
    计算模式设置为
    kCAAnimationPaced 或
    kCAAnimationCubicPaced,关键时间
    数组被忽略。

如果值在
keyTimes 数组无效或
不适合计算模式,
keyTimes 数组被忽略。

然后你可以阅读计算模式的说明:

价值计算模式

这些常量由
计算模式属性。

NSString * const kCAAnimationLinear;

NSString * const kCAAnimationDiscrete;

NSString * const kCAAnimationPaced;

常量

kCAAnimationLinear

简单线性
关键帧值之间的计算。
适用于 Mac OS X v10.5 及更高版本。
在CAAnimation.h中声明。

kCAAnimationDiscrete

依次使用每个关键帧值,不进行插值
计算值。
可用于
Mac OS X v10.5 及更高版本。
在CAAnimation.h中声明。

kCAAnimationPaced

对关键帧值进行插值以产生偶数
整个动画的节奏。
适用于 Mac OS X v10.5 及更高版本。
在CAAnimation.h中声明。

换句话说,离散计算模式使动画跳转到每个关键帧,而不是对其进行动画/过渡。

此致。

Your animation will need its calculationMode set to kCAAnimationDiscrete.

Take a look at the documentation on keyTimes which describes how the calculationMode is used:

The appropriate values in 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 key
    times.

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

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

If the values in the
keyTimes array are invalid or
inappropriate for the calculationMode,
the keyTimes array is ignored.

And then you can read the description of the calculation modes:

Value calculation modes

These constants are used by the
calculationMode property.

NSString * const kCAAnimationLinear;

NSString * const kCAAnimationDiscrete;

NSString * const kCAAnimationPaced;

Constants

kCAAnimationLinear

Simple linear
calculation between keyframe values.
Available in Mac OS X v10.5 and later.
Declared in CAAnimation.h.

kCAAnimationDiscrete

Each keyframe value is used in turn, no interpolated
values are calculated.
Available in
Mac OS X v10.5 and later.
Declared in CAAnimation.h.

kCAAnimationPaced

Keyframe values are interpolated to produce an even
pace throughout the animation.
Available in Mac OS X v10.5 and later.
Declared in CAAnimation.h.

In other words, the discrete calculation mode makes the animation jump to each key frame rather than animate/transition to it.

Best regards.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文