为什么视图在使用CGAffineTransformMakeRotation旋转时会改变位置 [英] Why does a view changes position when rotating it using CGAffineTransformMakeRotation
问题描述
我有以下超级简单动画,我基本上从原始角度/中心旋转视图2弧度,它旋转很好我唯一的误解是为什么视图在旋转发生时从其原始位置移动。
I have the following super simple animation, I'm basically rotating a view 2 radians from its original angle/center, it rotates fine my only misunderstanding is why does the view move from its original position when the rotation occurs.
[UIView animateWithDuration:1.0 animations:^{
self.someView.transform = CGAffineTransformMakeRotation( 2 );
}];
为什么在使用上面的代码旋转时视图会移动?
Why does the view moves when rotated with the code above?
我目前正在尝试辨别 CGAffineTransform Reference 。
I'm currently trying to discern the information in the CGAffineTransform Reference.
了解主播指出一>。
我找到了这个帖子,但没有给出具体答案。
为什么要旋转imageView,它会改变位置?
I found this threads but it doesn't show a concrete answer. Why rotating imageView, it changes position?
非常感谢
推荐答案
由于@fs_tigre请求,我正在添加另一个答案。问题在于xib文件中的自动布局,遗憾的是,为什么会影响转换,这是未知的。
现在这里是我为解决问题所采取的步骤:
I am adding another answer due to @fs_tigre request. The problem is with the auto layouts in your xib file, unfortunately is it unknown why that affects the transform.
Now here is the steps I did to solve the issue:
1-首先你需要摆脱你的汽车布局(是的,你必须)
取消选中使用Autolayout
1- first you need to get rid off your auto layout (yes, you have to)
uncheck Use Autolayout
2-删除所有约束并自动调整要旋转的视图的遮罩,如截屏
(这里我有我的蓝盒子,看右边自动调整,没有选择)
2- remove all constraints and autoresizing masks for your view that will be rotated, as in the screenshot
(Here I have my blue box, see on the right autoresizing, nothing is selected)
我对您的轮值代码进行了一些更改
I have made some changes for your rotation's code
self.someView.layer.anchorPoint = CGPointMake(0.5, 0.5);
// one degree = pi/180. so...
// rotate by 90
CGFloat radians = (M_PI/180) * 90;
[UIView animateWithDuration:1.0 animations:^{
self.someView.transform = CGAffineTransformRotate(self.someView.transform, radians);
}];
点击旋转
,看看魔术:)
这篇关于为什么视图在使用CGAffineTransformMakeRotation旋转时会改变位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!