Can you set the anchorPoint of the root layer on a

2019-04-01 00:15发布

问题:

Is it possible to modify the anchorPoint property on the root CALayer of a layer-backed NSView?

I have a view called myView and it seems every time I set the anchorPoint, it gets overridden in the next run loop. I am doing this:

NSView *myView = [[myView alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];

//set the root layer
myView.layer = [CALayer layer];
myView.wantsLayer = YES;

//gets overridden on the next run loop
myView.layer.anchorPoint = CGPointMake(1,1);

回答1:

On 10.8, AppKit will control the following properties on a CALayer (both when "layer-hosted" or "layer-backed"): geometryFlipped, bounds, frame (implied), position, anchorPoint, transform, shadow*, hidden, filters, and compositingFilter. … Use the appropriate NSView cover methods to change these properties.

Basically it will set the anchor to [0,0] from [0.5,0.5], to account for this i uses something like :

+(void) accountForLowerLeftAnchor:(CALayer*)layer
{
    CGRect frame = layer.frame;
    CGPoint center = CGPointMake(CGRectGetMidX(frame), CGRectGetMidY(frame));
    layer.position = center;
    layer.anchorPoint = CGPointMake(0.5, 0.5);
}