Re: How to redraw a view in slow-motion
Re: How to redraw a view in slow-motion
- Subject: Re: How to redraw a view in slow-motion
- From: Ken Tozier <email@hidden>
- Date: Sun, 19 Jun 2011 09:36:19 -0400
Have you tried CALayer/CAAnimation? They have a lot of power and are specifically designed for animation. If for some reason, you don't want to go that route, the following is a bit hokey, but it might work
Add a "subsetRange" property to your view class
Create a "setSubsetRange:(NSRange) inRange" method so KVO will work
Add yourself as an observer to this "subsetRange" property
[self addObserver: self forKeyPath: @"subsetRange" options: NSKeyValueObservingOptionNew context: NULL];
Add an observeValueForKeyPath
- (void)observeValueForKeyPath:(NSString *) inKeyPath
ofObject:(id) inObject
change:(NSDictionary *) inChange
context:(void *) inContext
{
if ([inKeyPath isEqualToString: @"subsetRange"])
{
// figure out how many lines you still have to draw here
if (stillHaveLinesToDraw == YES)
[self setNeedsDisplay: YES];
}
}
Then in your draw method
-(void) drawRect:(NSRect) inRect
{
// draw the set of lines specified in the "subsetRange" property
// calculate the next range and use your setter method
[self setSubsetRange: newRange];
}
As I said, pretty hokey, but it might do the trick. You should really check out CALayer and friends though it seems to be pretty slick. (Still pretty much a newbie, to CALayer, myself, but I like it so far)
On Jun 19, 2011, at 8:46 AM, Matthias Arndt wrote:
> In a document-based app my custom view draws some thousand paths in drawRect: with a good performance. Now I'd like to offer a "slow-motion" animation, so the user can actually watch the paths being drawn (not each single one, but e. g. in steps of 100 paths per sec).
>
> I though of several approaches and all of them seem to be infeasible:
>
> 1. Sleeping the drawing loop in drawRect: (or make the runLoop wait for some time) and use [... flushGraphics]: Freezes the GUI, as the app is single-threaded
> 2. Moving the drawing in a 2nd thread and then pause this one: AFAIK is drawing in a second thread not allowed in Cocoa
> 3. Limit the drawing loop to an increasing high bound, and setup a timer to fire [self setNeedsDisplay:YES] periodically: Causes the first x paths being redrawn at each animation step, resulting in a bad performance
> 4. Same approach, but skipping the first x paths in the next animation step: Corrupted display, e. g. while resizing in an animation
>
> I'm racking my brains over this, any suggestions?
>
> Mattes _______________________________________________
>
> 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