Scale NSView centered

China☆狼群 提交于 2019-12-23 03:08:03

问题


I need to scale an NSView around it's center. I tried using CATransform3DMakeScale and it worked to scale the view towards it's corner but I couldn't achieve to center it. I tried to set the layers anchor point to .5, .5 but that didn't work. I tried using scaleUnitSquareToSize but I ran into the problem that this seems unresetable without a lot of work.

In the end I need to be able to set the scale of an NSView to something like 0.8 and have it zoom out around it's center and be able to set the scale to 1 again to reset it.


回答1:


Added a frame fix for @fyasar's answer, works for me on MacOS:

CGRect frame = layer.frame;
layer.anchorPoint = CGPointMake(.5, .5);
layer.frame = frame; // Fix the location shift caused from anchor change



回答2:


I also faced with smilar problem. Please be sure exeute that line before construct CABasicAnimation.

layer.anchorPoint = CGPointMake(.5, .5);

So, that worked for me.

Good luck




回答3:


If you can require 10.8, then you could call NSScrollView's function:

- (void)setMagnification:(CGFloat)magnification centeredAtPoint:(NSPoint)point


来源:https://stackoverflow.com/questions/17124457/scale-nsview-centered

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!