Re: More CALayer Questions
Re: More CALayer Questions
- Subject: Re: More CALayer Questions
- From: "douglas a. welton" <email@hidden>
- Date: Mon, 7 Jul 2008 18:51:41 -0400
See my comments embedded below...
On Jul 7, 2008, at 3:42 PM, Gordon Apple wrote:
I'm suffering from extreme frustration with CALayers. I
obviously don't
understand the documentation available and there is a lot that is not
documented, especially since the Views guide has not been updated to
include
CALayers. Also, as others have observed, the "flipped" paremeter in
[NSGraphicsContext graphicsContextWithGraphicsPort:ctx flipped:YES]
simply
does not work.
A layer's context exist without regards to the current drawing
context. This makes sense because layer do not necessarily have to be
rendered anywhere. Why should they care about the graphic context?
If you care about the context when you are drawing, then query the
context being passed to your drawing method or delegate method.
NSGraphicsContext's -isFlipped method may become your new best
friend . ;^}
I have a view with inverted coordinates. I have been able to use a
stack of layers to draw when the view is unmodified. However, when
I scale
the view by any means (usually by changing the frame and resetting
to the
original bounds), it works without layers, but when using layers I
cannot
get the layers to track and can't seem to force them into the right
scale
and position. I've simplified it to just use only the view's layer
and
still can't get it to work properly.
What exactly are you doing to scale your views? layers?
When scaling layers within a non-scrolling view, I have used the
following method:
[targetLayer setValue: [NSNumber numverWithFloat: newScaleValue]
forKeyPath: @"scale"];
Usually, I place a statement like this in an action method that is
targetted by a slider and it produces no-brainer results
When I needed to scale a layer hosted by a view that was enclosed in a
NSScrollview, i used the following method:
clipView = [targetScrollView contentView];
` newBoundsSize = NSMakeSize( NSWidth( [clipView frame] ) /
newScaleValue, NSHeight( [clipView frame]) / newScaleValue);
[clipView setBoundsSize: newBoundsSize];
This works for the most part. The scaling is sluggish and sometimes
the view doesn't redraw properly until a scroller is movedc.
Have you tried both of these methods? Other methods?
One source of confusion is the anchorPoint/Position relation.
Reference
says "The position is relative to anchorPoint". Huh? What
anchorPoint? --
the layer in question, or its superLayer?
Think of the anchor point as the place where you stick a pin into the
layer when positioning it. An anchorpoint of (0.0, 0.0) puts the pin
in the lower left corner. An anchorPoint of (0.5, 0.5) puts the pin
in the center. And an achorPoint of (1.0, 1.0) places the pin in the
upper right corner.
Your layer's position will be aligned based on where the "pin" is stuck.
And what does this mean for the
view's layer in terms of the view's coordinates?
Important: There is no (real) implicit relationship between a view's
coordinate system and any of the hosted layer's coordinate system...
or at least not one that I have been able to find a reference to.
From what I have observed, the view's layer ( i.e., the "layer"
property) seems to operate as if the bounds of the enclosing view
defines its geometry.
Another one is "frame". "Specifies receiver’s frame rectangle in the
super-layer’s coordinate space". So ok, how does the view's layer
frame
relate to the view's frame?
Both of these questions can likely be answered by the question: How do
the view's layer coordinates relate to the view's coordinates?
See previous "Important" notice. For any arbitrary layer that is
hosted within a view, there is no actual relationship between the
view's frame and the layer's frame. Remember, the frame of a layer is
computed dynamically given an anchorPoint, position, bounds and
transform matrix. The transform matrix is very important in this
equation.
Also Important: Note that if you want to convert from a layer's
coordinate system to the view coordinate system, there is really no
out-of-the-box way to do this. Somewhere in the archives is a
discussion of this. If you need to know this relationship (for hit
testing, for example) then you'll have to roll your own... or rethink
how you are doing this (I had to follow this route, but ultimately it
made my code simpler)
Also, do springs and struts have any meaning between view and layer?
I think not. Layers have no clue as to what a view is. You can't ask
a layer what view is currently hosting it. Moreover, a layer can
exist without being in a view all together. If you want to manage the
"springs and struts" of you layers, then you need to make friend with
CAConstraint. Constraints give you mare flexibility (my opinion) to
manage how your layers are laid out in regards to one another
The questions are going to get worse when I start trying to change the
view's bounds size (drawing canvas size), but let's handle one issue
at a
time and stick with scaling for now.
with preemptive curiosity: have you set the autoresizingMask property?
Apparently, the view's layer corresponds to the view itself when
created, with the possible exception of coordinate inversion. I can
flip by
transforming the context when drawing, but I would rather transform
the
layer, because the latter is also involved when changing scaling.
However,
this gets involved with the entire anchorPoint/position question
because the
transform is not around thelayer's bounds origin, but around the
anchorPoint.
You can set the anchorPoint to be the bounds origin if you'd like._______________________________________________
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