gpt4 book ai didi

ios - 转换矩形 : toView: returns double size and twice the distance from the origin

转载 作者:可可西里 更新时间:2023-11-01 04:43:45 26 4
gpt4 key购买 nike

我将 ImageView 传递给需要 ImageView 大小的初始化方法。然后调用 convertRect:toView:

- (id) initWithImageView:(UIImageView*) imageView {
self = [super init];
if (self) {
CGRect imageViewFrame = [[imageView superview] convertRect: imageView.frame toView: self.view];
}
}

调用方式:

MyViewController *viewController = [[MyViewController alloc] initWithImageView:imageView];
viewController.delegate = self;
[self.tabBarController presentViewController:viewController animated:NO completion:nil];

然后是imageViewFrame

(origin = (x = 0, y = -120.22867049625003), size = (width = 750, height = 750))

imageView 有一个框架

frame = (0 -60.1143; 375 375);

它的 super View 有一个框架

frame = (0 0; 375 254.5);

为什么要放大 x2?

在 iPhone 6 plus (3x) 模拟器上进一步测试使 imageViewFrame

(origin = (x = 0, y = -199.30952358000002), size = (width = 1242, height = 1242))

第一次测试是在 iPhone 6 模拟器 (2x) 上完成的。为什么 convertRect:toView: 以像素而不是点为单位工作?

最佳答案

在你的-initWithImageView:中,你的self.view还没有生成,你可以尝试缓存imageView,并且在 -viewDidLoad 中执行 rect 转换操作:

// If you don't user Interface Builder
- (void)loadView
{
CGRect frame = <your_view_frame>;
UIView * view = [[UIView alloc] initWithFrame:frame];
self.view = view;
}

- (void)viewDidLoad
{
[super viewDidLoad];

CGRect imageViewFrame = [[imageView superview] convertRect:imageView.frame toView:self.view];
...
}

编辑

正如文档对 -convertRect:toView: 的 View 参数所说:

The view that is the target of the conversion operation. If view is nil, this method instead converts to window base coordinates. Otherwise, both view and the receiver must belong to the same UIWindow object.

如果你坚持在 -initWithImageView 方法中做,声明一个自定义 View iVar,在你的 -initWithImageView 方法中分配和初始化,然后在 -loadView,将 iVar 分配给 self.view。当然,你在 -initWithImageView 中转换 rect 的方式应该是

CGRect frame = <your_view_frame>;
_iVarView = [[UIView alloc] initWithFrame:frame];
CGRect imageViewFrame = [[imageView superview] convertRect:imageView.frame
toView:_iVarView];

-loadView中:

- (void)loadView
{
self.view = _iVarView;
}

但不建议这样做,我建议您在 View 生命循环的正确方法中初始化 UI。

关于ios - 转换矩形 : toView: returns double size and twice the distance from the origin,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29842391/

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