开发者

Core Animation drawLayer:inContext driven by NSTimer, am I doing it right?

开发者 https://www.devze.com 2023-03-29 09:25 出处:网络
I\'m doing a project which inv开发者_运维知识库olves 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 animatio

I'm doing a project which inv开发者_运维知识库olves 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!


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号