以NSView为中心缩放 [英] Scale NSView centered

查看:194
本文介绍了以NSView为中心缩放的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要围绕其中心缩放NSView. 我尝试使用CATransform3DMakeScale,它可以将视图缩放到拐角处,但无法实现居中对齐.我试图将图层的锚点设置为.5,.5,但这没有用. 我尝试使用scaleUnitSquareToSize,但遇到一个麻烦,即如果不进行大量工作,这似乎无法重置.

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.

最后,我需要能够将NSView的比例尺设置为0.8,并使其中心缩小,并能够将比例尺再次设置为1以重置它.

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.

推荐答案

为@fyasar的答案添加了框架修复,在MacOS上对我有效:

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

这篇关于以NSView为中心缩放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆