Re: Small animation
Re: Small animation
- Subject: Re: Small animation
- From: Shawn Erickson <email@hidden>
- Date: Wed, 21 Jan 2004 14:19:17 -0800
On Jan 21, 2004, at 12:13 PM, Lorenzo wrote:
I continue to don't understand.
How to modify the content of a view and not the view itself?
The worm example shows much of this...
I guess you are attempting move an image over a constant colored
background... In your code snippet (which follows) you are using a
image view and moving that view element around inside of a containing
view that is set to paint a constant color background. So you are
incurring the extra work of moving a view element (sub-class of NSView)
around in its containing view when instead it quicker to just draw the
image at the location you want in your view directly. No need to use an
image view. Additionally your drawRect method below is completely
repainting the whole of the containing view bounds not just the part
that needs updating. Sure it is being clipped as needed but you are
doing extra filling that is just going to be thrown away.
- (void)updateView
{
x++;
y++;
[imageView setFrameOrigin:NSMakePoint(x, y)];
NSRect theRect = [imageView frame];
theRect.origin.x -= 1;
theRect.origin.y -= 1;
theRect.size.width += 2;
theRect.size.height += 2;
[self setNeedsDisplayInRect:theRect];
}
// ---------------------------------------------------------
// drawRect
// ---------------------------------------------------------
- (void)drawRect:(NSRect)rect
{
[gBackgroundColor set];
NSRectFill([self bounds]);
}
So a quick rework of your NSView sub-class (written in Mail) could do
something like the following (assuming 10.3+)...
- (BOOL)isOpaque {
return YES;
}
- (void)updateView
{ //assumes imageRect.size is set to the image's size
[self setNeedsDisplayInRect:imageRect]; //invalidate the old image
location
imageRect.origin.x = <some move logic>
imageRect.origin.y = <some move logic>
[self setNeedsDisplayInRect:imageRect]; //invalidate the new image
location
}
- (void)drawRect:(NSRect)rect
{
NSRect *rects;
int i, count;
[self getRectsBeingDrawn:&rects count:&count];
[gBackgroundColor set];
for (i = 0; i < count; i++) {
NSRectFill(rects[i]); //keep the background painted with our solid
color
}
//"image" is a instance of NSImage that contains the image you want to
paint
//if image is opaque consider using NSCompositeCopy
[image compositeToPoint:imageRect.origin
operation:NSCompositeSourceOver];
}
-Shawn
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.