我正在使用以下代码来旋转和转换图像视图:
myImageView.transform = CGAffineTransformMakeRotation(45); // rotation
CGRect frame = myImageView.frame;
frame.origin.x = x_position;
frame.origin.y = y_position;
myImageView.frame = frame; // transformation
我正在使用以下代码来旋转和转换图像视图:
myImageView.transform = CGAffineTransformMakeRotation(45); // rotation
CGRect frame = myImageView.frame;
frame.origin.x = x_position;
frame.origin.y = y_position;
myImageView.frame = frame; // transformation
tl; dr:frame
当你有一个 non-identity 时,这是假的transform
。改为center
改为。
从文档中:
警告如果该
transform
属性不是恒等变换,则该属性的值是未定义的,因此应该被忽略。
当您在第一行设置转换,然后在未定义帧之后读取您实际返回的内容。
// Setting a non-identity transform (1)
myImageView.transform = CGAffineTransformMakeRotation(45);
CGRect frame = myImageView.frame; // At this point the frame is undefined (2)
// You are modifying something which is undefined from now on ...
此外,您不仅不应该因为未定义而读取框架,也不应该设置它。
如果
transform
属性包含非恒等变换,则 frame 属性的值未定义且不应修改。
该问题的解决方案出现在文档的下一句中
在这种情况下,您可以使用 center 属性重新定位视图并使用 bounds 属性调整大小。
由于您只是更改位置,我建议您不要触摸框架,而是读取center
图像视图并将其设置为新值。与框架一样,center
它不受变换的影响。
CGPoint center = myImageView.center;
center.x = x_center_position; // Note that the `center` is not the same as the frame origin.
center.y = y_center_position; // Note that the `center` is not the same as the frame origin.
myImageView.center = center;
尝试删除 ImageView 的自动调整大小掩码
imageView.autoresizingMask = UIViewAutoresizingNone;
这可能会解决您的问题