gpt4 book ai didi

ios - 当我应用旋转和变换时,iOS 上的图像变得狭窄和挤压

转载 作者:行者123 更新时间:2023-11-29 11:02:39 24 4
gpt4 key购买 nike

我正在使用以下代码来旋转和变换 ImageView :

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:当您有一个非身份 transform 时,frame 是伪造的。改为更改 center


来自 the documentation :

Warning If the transform property is not the identity transform, the value of this property is undefined and therefore should be ignored.

当您在第一行设置转换,然后在未定义后读取帧时,您实际返回的是什么。

// 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 ...

此外,你不仅不应该读取框架,因为它是未定义的,你也不应该设置它。

if the transform property contains a non-identity transform, the value of the frame property is undefined and should not be modified.

该问题的解决方案在文档的下一句中

In that case, you can reposition the view using the center property and adjust the size using the bounds property instead.

因为您只是在改变位置,所以我建议您不要触摸框架,而是读取 ImageView 的 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;

关于ios - 当我应用旋转和变换时,iOS 上的图像变得狭窄和挤压,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15172449/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com