Re: Animation with CALayer?
Re: Animation with CALayer?
- Subject: Re: Animation with CALayer?
- From: Bill Dudney <email@hidden>
- Date: Tue, 5 Feb 2008 13:53:24 -0700
Hi Alex,
I have an example that was going to be part of the book but ended up
not fitting in the flow of things (although I might end up adding it
back) that shows how to animate something like your audio level meter.
The app animates the width of a line changing but the secret sauce can
be applied to the length of the audio meter. The secret sauce, 1 make
sure to call setNeedsDisplay: when the audio value changes, 2
implement the defaultAnimationForKey: method to return the animation
you want used with the audio value changes;
- (void)setDrawnLineWidth:(float)value {
[self willChangeValueForKey:@"drawnLineWidth"];
drawnLineWidth = value;
[self.path setLineWidth:drawnLineWidth];
[self setNeedsDisplay:YES];
[self didChangeValueForKey:@"drawnLineWidth"];
}
+ (id)defaultAnimationForKey:(NSString *)key {
static CABasicAnimation *drawnLineWidthBasicAnimation = nil;
if ([key isEqualToString:@"drawnLineWidth"]) {
if (drawnLineWidthBasicAnimation == nil) {
drawnLineWidthBasicAnimation = [[CABasicAnimation alloc]
init];
}
return drawnLineWidthBasicAnimation;
} else {
return [super defaultAnimationForKey:key];
}
}
Then when you change the audio level do something like this;
- (IBAction)setWidth:(id)sender {
[[self animator] setDrawnLineWidth:[sender floatValue]];
}
This example uses simple cocoa stuff that you already know (except for
the defaultAnimationForKey: method) so its quicker uptake than getting
your head wrapped around drawing with CA (which is conceptually
similar to appkit drawing but in details its quite different). If you
want or need to go that route keep in mind that layers do not clip
their sublayers by default and that you should call display only when
the layer needs to be redrawn not during animation. The whole
animation is done via the presentation and render tree layers and if
you call display a lot it will kill performance because its got to
push the content down onto the render layer too often.
Animating the slider could use a similar approach and look a lot like
the 'turn on time machine' button in the time machine prefs panel.
HTH,
-bd-
http://bill.dudney.net/roller/objc
On Feb 5, 2008, at 1:30 PM, alex wrote:
Hi there,
Hopefully this is the right list for this question. I haven't found
what I was looking for after searching the archives for the "right"
way to animate some graphics using cocoa.
I'm trying to draw two different things. One of them is an audio
level meter and the other is an effect similar to what the iPhone
"slide" graphic does on the lock screen on the iPhone.
(I have a mask that I slide a white blob around behind.)
I'm able to do both with an NSTimer that fires and then I issue a
[setNeedsDisplay] from the timer to update the animation. This
works fine and produces the expected results but seems to use way
too much CPU compared to what I've been able to do "back in the day"
using straight CG or worse Quickdraw. (I am not porting this code
from QD- I threw that code away, this is all new code.)
I've been looking at using CALayers to implement the drawing so that
I can call [display] rather than [setNeedsDisplay] with the hopes
that this would make the animations use less CPU.
What is the right way to approach this problem? (and please don't
suggest using Shark to optimize my code as that is a given and will
be done after I determine what the best course of code will be).
thanks,
alex
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden