动画核心动画 [英] Core Animation animating an array of images

查看:84
本文介绍了动画核心动画的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我基本上想要做的是tp以快速速率(60 /秒)显示图像序列以创建序列。我要创建一个计时器,看看它是否足够快。

What I basically want to do is tp display a sequence of images at a fast rate (60/second) in order to create a sequence. I'm going to create a timer and see if it's fast enough.

我想使用动画在可可应用程序中显示一系列图像(每个图像最多60个)

I want to display an array of images in my cocoa app using an animation (up to 60 per seconds).

这是到目前为止我创建的动画:

Here's the animation I've created so far:

- (CAAnimation*)imageAnimation;
{
    CGImageRef image1 = [self nsImageToCGImageRef:[NSImage imageNamed:@"1.png"]];
    CGImageRef image2 = [self nsImageToCGImageRef:[NSImage imageNamed:@"2.png"]];
    CGImageRef image3 = [self nsImageToCGImageRef:[NSImage imageNamed:@"3.png"]];

    CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"position"];

    NSArray *images = [NSArray arrayWithObjects:(id)image1, (id)image2, (id)image3, nil];

    [animation setValues:images];
    [animation setDuration:3.0];

    return animation;
}

其中 nsImageToCGImageRef 是我在其他地方定义的方法。现在,我不确定从何处将动画添加到我的视图中。

where nsImageToCGImageRef is a method I've defined elsewhere. Now, I'm not sure where to go from here to add this animation to my views.

推荐答案

您应该调查 CVDisplayLink ,它使您每次Mac的显示子系统即将显示框架时都能收到回调。这样一来,您可以以60fps的速度显示图像而不会丢帧,因为API仅在需要更新时才调用您的代码。

You should investigate CVDisplayLink, which allows you to receive a callback every time the Mac's display subsystem is about to display a frame. This will let you display images at 60fps with no dropped frames, because the API only calls your code when it needs an update.

这意味着您无需担心关于使计时器与显示器同步的信息,这也意味着您没有做不必要的工作。您还可以执行一些操作,例如仅在备用回调上进行更新,以在绘图代码很慢时将帧速率降低到30fps。

This means you don't need to worry about synchronising a timer with the display, and it also means that you are not doing unnecessary work. You can also do things such as update only on alternate callbacks to reduce the frame rate to 30fps if your drawing code is slow.

这篇关于动画核心动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆