核心动画drawLayer:由NSTimer驱动的inContext,我做得对吗?

发布于 2024-11-30 18:03:12 字数 1076 浏览 1 评论 0原文

我正在做一个涉及 Core Animation 和 AV Foundation 的项目。我想使用 Core Animation 渲染一些效果,然后通过 AV Foundation 将其导出到视频文件。我想要实现的动画效果是重播最终用户在iPad上签署他/她的签名。

到目前为止我所取得的成就: - 参考Apple的GLPainter示例,我能够记录用户签名的笔画,每个单独的笔画由多个触摸点(CGPoint)组成,这些触摸点已记录在我的自定义UIView的touchBegin / touchMoved / touchEnd方法中。 - 由可重复的 NSTimer 驱动,我能够在我的自定义 UIView 上重新绘制它。主要是从一个点到下一个点画线的重复过程。

我想做的事情: 使用Core Animation技术实现签名重绘,然后将核心动画代码与Av Foundation合成代码一起嵌入以渲染最终输出。

我现在关心的事情:

  • 对Core Animation一无所知,我最近几天一直在学习它。然而,我读过的教程/书籍只告诉我如何通过改变CALayer的属性来制作隐式动画,或者制作CAKeyframeAnimation。但我的动画是在图层的画布上逐渐绘制一些东西,而不是移动一些东西。那么,如何在 Core Animation 中做到这一点呢?

我现在正在做的事情:与我对 Quartz2D 所做的非常相似,我使用 NSTimer 重复设置我的 CALayer 对象 needDisplay,这反过来又调用其委托的 drawLayer:inContext 方法的实现,其中我编写的代码与在 UIView 的 drawRect 中编写的代码完全相同。

好吧,我做了一些动画,没有涉及任何 CAAnimation。我只是感觉很奇怪,感觉自己做错了。我还没有继续将核心动画与 AV Foundation 部分结合起来,但我怀疑我的想法不会成功。

我发誓,我做了诚实的搜索,但没有发现任何相关的东西。我不得不说,我在 Quartz2D 和 Core Animation 方面的经验非常有限,所以,如果有人能给我一些提示,我真的很感激,谢谢!

I'm doing a project which involves Core Animation and AV Foundation. I want to use Core Animation to render some effects, then export it to video file via AV Foundation. The animation effect I want to achieve is replaying end-user signing his/her signature on the iPad.

What I've achieved so far:
- With referring GLPainter sample from Apple, I'm able to record the strokes of user signature, each individual stroke is composed of a number of touch points (CGPoint) which have been recorded in touchBegin/touchMoved/touchEnd method of my custom UIView.
- Driven by a repeatable NSTimer, I'm able to re-draw it on my custom UIView. Mainly it's a repeating process of drawing line from one point to the next.

What I'm trying to do:
implement the signature re-drawing with Core Animation technology, then later on, embedded the core animation code with Av Foundation composition code to render the final output.

What concerns me now:

  • Knowing nothing about Core Animation, I've been learning it in the last few days. However, the tutorials/books I've read only tells me how to make implicit animation by altering CALayer's properties, or make CAKeyframeAnimation. But my animation is sort of drawing something on the layer's canvas progressively instead of moving something. So, how to do this in Core Animation?

What I'm doing now: is quite similar to what I did with Quartz2D, I use a NSTimer to repeatedly set my CALayer object needsDisplay, which in turn invokes its delegate's implementation of drawLayer:inContext method, where I write fairly the same code as I wrote in UIView's drawRect.

Well, I did kind of animation, without any CAAnimation involved. I'm just feeling weird, feeling I'm doing it wrong. I haven't proceeded to the combining Core Animation with AV Foundation part, but I doubt my heck won't get through.

I did honest search in SO I swear but found nothing related. My experience in Quartz2D and Core Animation is quite limited I have to say, so, really appreciate if someone could give me some hints, thank you!

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

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

发布评论

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

评论(1

夢归不見 2024-12-07 18:03:13

您想要做的是将绘图与屏幕的刷新率同步。 NSTimer 在这方面并不是特别擅长。您正在寻找的类是CADisplayLink。像这样访问它:

link = [[UIScreen mainScreen] 
             displayLinkWithTarget:self
                          selector:@selector(shouldUpdateDisplayLink:)];

shouldUpdateDisplayLink: 回调中设置需要显示。保留显示链接以确保其保持不变,并可选择设置较低的刷新率,也许是为了匹配媒体播放。

What you want to do is to sync your drawing to the refresh rate of the screen. A NSTimer is not particularly good at this. The class you are looking for is CADisplayLink. Get access to it like this:

link = [[UIScreen mainScreen] 
             displayLinkWithTarget:self
                          selector:@selector(shouldUpdateDisplayLink:)];

Set needs display in the shouldUpdateDisplayLink: callback. Retain the display link to ensure it stays around, and optionally set a lower refresh rate, perhaps to match the media playback.

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