Re: Hillegass, Third Edition, Chapter 18
Re: Hillegass, Third Edition, Chapter 18
- Subject: Re: Hillegass, Third Edition, Chapter 18
- From: "Jon Buys" <email@hidden>
- Date: Tue, 15 Jul 2008 06:25:25 -0500
On Tue, Jul 15, 2008 at 3:00 AM, Antonio Nunes <email@hidden>
wrote:
> On 15 Jul 2008, at 04:53, Jon Buys wrote:
>
> I'm working through the challenge app at the end of Chapter 18 of Cocoa
>> Programming, Third Edition. I've got my app to the point where it can
>> draw
>> ovals, but each time I click in the window it seems like the view redraws
>> itself. I'm sure that this is a very simple question with a very simple
>> answer, but I'm stuck. I've been looking at it for three days now, and I
>> think its time to let another set of eyes look at it.
>>
>
> You're constantly asking the whole view to redraw using setNeedsDisplay:.
> You only want to redraw the section of the screen that actually needs to be
> redrawn. I.e: you want to erase the portion that encompasses the previous
> bounds of the path, and draw only the portion the encompasses the new bounds
> of the path. For this there is setNeedsDisplayInRect:, plus you need to
> actually use the rect that is passed to you in the drawRect: method, instead
> of the view's bounds, since that tells you exactly what part of the view
> needs updating.
>
Thanks for the tip!
>
> The following code solves the issue of updating only the required area. It
> does not solve drawing a new rect on every mouseDown while maintaining the
> previously drawn rects. That remains for you as an exercise.
Drawing a new rect on every mouseDown is the main problem I'm having now,
but I think it's because I'm not adding the rect to an NSMutableArray. I'm
working on that now, but I've run out of time this morning. How can I add a
rect to an array? Do I have to convert it first?
>
>
> Note that there are still problems with this implementation:
> 1. Copy the code as is and run it. Drag an oval around on the canvas:
> you'll see the problem soon enough. Think about possible causes for this.
> Then comment out the first line in the updateWithOldRect: method, and
> uncomment its second line. Then try again. Make sure you understand why the
> second line solves the issue.
> 2. Afer step 1, if you drag an oval around long enough, you will find that
> in 2 edge cases some ghosting still occurs. This one I'll leave you to
> figure out on your own, other than stating that the cause involves
> NSUnionRect and the result of the ANRectFromPoints function, and that
> reading the documentation on NSUnionRect should give you a good hint as to
> the cause of the issue and how to solve it.
>
> Have fun,
> António
>
António,
Thanks for your help, I'm going through your code now, comparing it to my
old code and reading the documentation. NSUnionRect doesn't seem to be much
help in the docs though:
"Returns the smallest rectangle that completely encloses both *aRect* and *
bRect*. If one of the rectangles has 0 (or negative) width or height, a copy
of the other rectangle is returned; but if both have 0 (or negative) width
or height, the returned rectangle has its origin at (0.0, 0.0) and has 0
width and height."
-
http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Miscellaneous/Foundation_Functions/Reference/reference.html#//apple_ref/c/func/NSUnionRect
Can you explain a little more why this results in the ghosting I saw in the
view earlier?
Thanks a ton,
Jon
_______________________________________________
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