抱歉,如果这很明显或在其他地方被覆盖,但我整天都在谷歌上搜索,还没有找到真正有效的解决方案。
我的问题如下:我目前正在全屏 UIView 中绘制图像,例如,我们会说图像在 UIView 的右下角。我想在该图像的中心进行旋转变换(CGAffineTransformMakeRotation),但是,默认情况下,旋转命令围绕它自身的 UIView 的中心旋转。结果,当我旋转时,我的图像会在屏幕上移动,而不是停留在原地并围绕自己的中心旋转。
根据我收集到的信息,我需要翻译我的上下文,以便原点(UIView 的中心)位于我的图像中心,旋转,然后通过翻译回原始位置来恢复上下文。
以下是我最接近的工作,但问题是图像旋转时,它在旋转时向下移动。我认为这是由动画补间第一步翻译和第三步翻译引起的,而不是仅仅意识到翻译的起点和终点是相同的......
// Before this i'd make a call to a function that draws a path to a passed in context
CGAffineTransform inverseTranslation = CGAffineTransformMakeTranslation( transX, transY );
CGAffineTransform translation = CGAffineTransformMakeTranslation( -transX, -transY );
CGAffineTransform rot = CGAffineTransformMakeRotation( 3.14 );
CGAffineTransform final = CGAffineTransformConcat( CGAffineTransformConcat( inverseTranslation, rot ), translation );
// Then i apply the transformation animation like normal using self.transform = final etc etc
我也尝试过 CGContextTranslateCTM 和 CGContextSaveGState/UIGraphicsPushContext 之类的东西,但这些似乎效果不大。
我已经为此奋斗了好几天,我目前的解决方案似乎很接近,但我不知道如何摆脱这种翻译补间。有没有人对此有解决方案或更好的方法来解决这个问题?
[更新] 目前我正在以 UIview 的中心为中心绘制我的图像,然后将 UIView.center 属性设置为我想要旋转的原点,然后执行旋转命令。看起来有点像 hack,但在我得到真正的翻译工作之前,这是我唯一的选择。谢谢!