Sky*_*ler 8 iphone 2d core-animation quartz-graphics
对不起,如果这是显而易见的或在其他地方覆盖,但我一直在谷歌搜索,并没有找到实际工作的解决方案.
我的问题如下:我目前正在全屏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
Run Code Online (Sandbox Code Playgroud)
我也尝试了像CGContextTranslateCTM和CGContextSaveGState/UIGraphicsPushContext这样的东西,但这些似乎没什么效果.
我已经和它斗争了好几天,我目前的解决方案似乎很接近,但我不知道如何摆脱那种翻译补间.有没有人有这个或更好的方法来解决这个问题?
[更新]目前我正在以UIview的中心绘制我的图像,然后将UIView.center属性设置为原点,我想旋转然后执行旋转命令.看起来像是一个黑客,但直到我能得到真正的翻译工作它是我唯一的选择.谢谢!
n8g*_*ray 14
duncanwilcox"答案是正确的,但他留下了,你改变视图的层的锚要绕点的一部分.
CGSize sz = theview.bounds.size;
// Anchorpoint coords are between 0.0 and 1.0
theview.layer.anchorPoint = CGPointMake(rotPoint.x/sz.width, rotPoint.y/sz.height);
[UIView beginAnimations:@"rotate" context:nil];
theview.transform = CGAffineTransformMakeRotation( 45. / 180. * M_PI );
[UIView commitAnimations];
Run Code Online (Sandbox Code Playgroud)
这在此处记录:http://developer.apple.com/documentation/Cocoa/Conceptual/CoreAnimation_guide/Articles/Layers.html
这也是一个选择:基础的简单改变;-)
CGAffineTransform transform = CGAffineTransformMakeTranslation(x, y);
transform = CGAffineTransformRotate(transform, angle);
transform = CGAffineTransformTranslate(transform,-x,-y);
Run Code Online (Sandbox Code Playgroud)
(x,y)你喜欢的旋转中心在哪里
| 归档时间: |
|
| 查看次数: |
13524 次 |
| 最近记录: |