且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用CGAffineTransform旋转后调整UIView的大小

更新时间:2023-02-02 14:28:37

我自己已经解决了这个问题.我非常有信心地说,没有简单的方法可以做到这一点,您只需要拿起一支笔和一支笔就可以了.论文,然后潜入一些相当繁琐的三角学中!

I've worked on this problem myself. I'm fairly confident in saying that there is no easy way to do this, you just need to pick up a pen & paper and dive into some fairly tedious trigonometry!

首先要注意的是,视图的frame属性超出范围(即,对其应用旋转变换时,其值是不确定的),因此,您必须改为使用boundscenter属性.然后,您只需要根据当前角度计算新的边界和中心位置.烦人的是,知道bounds与水平& amp;不对齐.垂直轴,而center坐标当然是,因此需要一个笔&.纸.对于每个手柄,您的计算将略有不同,但是您可以通过复制一个手柄的代码并更改一些加/减符号来逃脱这种麻烦.

First thing to note is that the view's frame property is out of bounds (i.e. its value is undefined when a rotation transform is applied to it), so you must instead use the bounds and center properties. Then you just need to compute the new bounds and center position based on the current angle. The tedium comes in knowing that the bounds are not aligned to the horizontal & vertical axis anymore, whereas the center coords of course are, hence the need for a pen & paper. Your calculations will be very slightly different for each handle, however you can pretty much just get away with copying one handle's code and changing some plus/minus signs.

实际上,老实说,一旦您勇于做出决定,那就还不错.我自己的实现限制了要调整大小的视图的大小和宽度,这是最难的一点.使事情按用户期望的方式工作可能会花费一些工作,但这是值得的.

Actually to be honest, once you've braved the decision to do it, it's not too bad. My own implementation limited the size and width of the view being resized, and this was the hardest bit. Getting things to behave as the user would expect may take a bit of work, but it's worth it.