Re: Layering NSViews -- why isn't this recommended?
Re: Layering NSViews -- why isn't this recommended?
- Subject: Re: Layering NSViews -- why isn't this recommended?
- From: "Dennis C. De Mars" <email@hidden>
- Date: Thu, 7 Jun 2001 19:30:16 -0000
Andrew Platzer <email@hidden> said:
>
On Wednesday, June 6, 2001, at 08:29 , Michael P. Rogers wrote:
>
> I've been trying to create a pile of cards that are offset -- so each
>
> card is to the right and below the one beneath it (ala Eric's Solitaire,
>
> which I believe used to ship with the Mac at one time). My Cards are
>
> subclasses of NSImageView.
>
> My plan is to
>
> 1) make a Card class (subclass of NSImageView)
>
> 2) place those Cards in a Window's contentView using a method in NSView
>
> that sounded promising, addSubview:positioned:relativeTo:.
>
> It seems to work, but the documentation has this dire warning:
>
>
The main problem is the drawing code does not check for overlapping views.
>
If view_2 comes after view_1 then it will appear to be over view_1 when
>
the window is redrawn but if an action forces only view_1 to redraw,
>
view_2 won't be redrawn and the layering will appear to change. Also,
>
finding which view a mouse click is in may return the wrong view.
Wow, that's not good. Thanks for explaining that. I really think Apple should change
the way that works, but if not there should at least be a more explicit explanation in
the documentation.
The Cocoa documentation goes into an elaborate explanation about how, when drawing is
done, Cocoa backs up to the closest opaque view behind the view being refreshed (if the
view being refreshed is not itself opaque) and then redraws from that view on up. That
makes it sound like layering is being handled, but I guess it only talks about what it
does about views _behind_ the view being redrawn, it doesn't say anything about the
views in front!
I guess it isn't such a big deal if you know in advance that this is the behavior...at
any rate, this isn't the way things are done in some other frameworks I could name, so
I think those of us making the transition to Cocoa could use an explict warning in the
documentation (I suppose I should email that feedback directly to Apple.)
>
>
For something like a card game, just have a single view that draws all the
>
images. It's faster and smaller.
Yes, this would be a better way to do it regardless of the layering issue. Just make
the Card class a subview of NSImage, not NSImageView, and draw them in the order and
position you want.
- Dennis D.