3
votes

I have a layer-backed view, and want to understand how anchor point works for it and is it allowed at all to work with CALayer geometry properties (instead of using frame property of NSView)

First, quote from documentation:

The default value for anchorPoint is (0.5,0.5) which corresponds to the center of the layer's bounds

But when I try to change value of anchor point to (0.5,0.5) directly, I got offset. Here is example, red area is the original position (when nothing applied to the anchor point), the button was moved to the bottom-left corner:

enter image description here

When I set anchor point to (0,0) button placed where it should be:

enter image description here

Update:

Here is the code I use to instantiate Button:

self.button = [[NSButton alloc] initWithFrame:NSMakeRect(130, 130, 100, 40)];
self.button.title = @"My Title";
self.button.wantsLayer = YES;
NSLog(@"Button anchor Point is : (%f;%f)", self.button.layer.anchorPoint.x, self.button.layer.anchorPoint.y);
[panelView addSubview:self.button];

What interesting, seems that for layer-backed view's the default anchor point is 0,0:

NSLog(@"Button anchor Point is : (%f;%f)", self.button.layer.anchorPoint.x, self.button.layer.anchorPoint.y);

produces (0.000000;0.000000)

After all, OS X Deployment target is 10.8

1
Very interesting. What happens if you apply a rotation transform to the layer? It should rotate around its anchor point which seems to be the lower left corner in your case. How do you modify the layer geometry before changing the anchor point? Do you modify the position, frame or bounds? Do you modify the view geometry at all before setting the anchor point? - David Rönnqvist
I update question with code and some Log output. - dmitrynikolaev
+1 Good question. Does calling [NSView addSubview] also add the subview's layer to the backing layer of the receiver? The docs don't mention it. - trojanfoe
Seems that this operations (working with CALayer for layer-backed view) prohibited at all. See the answer below. - dmitrynikolaev

1 Answers

1
votes

Here is quote from Apple documentation on NSView:

When using layer-backed views you should never interact directly with the layer.

Seems this code cross the boundary of what you can do with layer-backed views.