Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

scale and center calayer to superview

When I scale a layer, the result always has origin set at CGPoint(0,0). What is the best way to make sure after the scale, the layer is still center in its super layer?

[CATransaction begin];
[CATransaction setAnimationDuration: 0];
[CATransaction setDisableActions: TRUE];

self.maskLayer.transform = CATransform3DScale(CATransform3DIdentity, .8, .8,1);
self.maskLayer.position = CGPointMake(kScreenWidth/2,kScreenHeight/2);

[CATransaction commit];
like image 730
prostock Avatar asked Jul 31 '12 23:07

prostock


1 Answers

You need to set the layer's anchorPoint to be its center:

self.maskLayer.anchorPoint = CGPointMake(0.5, 0.5);

and then set its position to the centre of its superLayer

self.maskLayer.position = (CGPoint){CGRectGetMidX(superLayer.bounds), CGRectGetMidY(superLayer.bounds)};
like image 156
Ashley Mills Avatar answered Nov 16 '22 02:11

Ashley Mills