【问题标题】:UIImage moves right on rotation - wrong center point?UIImage 在旋转时向右移动 - 中心点错误?
【发布时间】:2013-03-07 19:19:54
【问题描述】:

当我旋转图像时,它会向右移动。它不应该。似乎中心设置到了错误的点。见截图:在两种状态下 - 旋转而不是旋转 - “+”的中心不应该改变。

我正在使用这样的自动布局定位 UIImageView:

- (void)viewDidLoad {
...

addIconImageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"AddCardIcon.png"]];
[addIconImageView setContentMode:UIViewContentModeCenter];
[addIconImageView setTranslatesAutoresizingMaskIntoConstraints:NO];
[self.view addSubview:addIconImageView];



[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:[addIconImageView]-(10)-|"
                                                                  options:nil
                                                                  metrics:nil
                                                                    views:NSDictionaryOfVariableBindings(addIconImageView)]];

[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat: @"V:|-[addIconImageView]-|"
                                                                  options:NSLayoutFormatAlignAllCenterY
                                                                  metrics:nil
                                                                    views:NSDictionaryOfVariableBindings(addIconImageView)]];

...
}

这就是我旋转图像的方法

[UIView animateWithDuration:0.2
                 animations:^{
                     addIconImageView.transform = CGAffineTransformRotate(addIconImageView.transform, M_PI/4);
                 } completion:^(BOOL finished) {
                 }
 ];

为什么会在旋转时向右移动,我该如何避免它?

【问题讨论】:

  • 如果图像的位置不是(0,0),则不会从中心旋转,而是从坐标系的原点旋转。
  • @RamyAlZuhouri 他正在使用addIconImageView 的坐标系在addIconImageView 上添加一个变换。如果他使用超级视图的转换属性,您提到的只会是一个问题。

标签: ios objective-c animation uiview autolayout


【解决方案1】:

我的第一个猜测是您的图片资源不是完全正方形的。

调整 AddCardIcon.png 的大小,使其具有相同的宽度和高度,并且在任何一侧都没有任何透明填充。

否则,手动确定+的中心并设置addIconImageView.layer.anchorPoint

【讨论】:

  • 图片绝对是方形的。
  • + 的中心是否真的在正方形图像的正中心?
  • 是的。确实 addIconImageView.center.x 在旋转后会发生变化(从 165.0 到 172.424622),但它确实不应该。嗯。
  • 只是出于好奇,addIconImageView.layer.anchorPoint 设置为什么?
  • 它始终保持 0.50(即使图像偏右)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-12-17
  • 1970-01-01
  • 1970-01-01
  • 2019-11-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多