美文网首页
convertRect:toView:

convertRect:toView:

作者: Fsn_soul | 来源:发表于2022-03-08 17:09 被阅读0次
    UIView *containerView = [[UIView alloc] initWithFrame:CGRectMake(30, 100, 300, 500)];
    containerView.backgroundColor = [UIColor yellowColor];
    [self.view addSubview:containerView];
    
    UIView *redView = [[UIView alloc] initWithFrame:CGRectMake(20, 100, 200, 200)];
    redView.backgroundColor = [UIColor redColor];
    [containerView addSubview:redView];
    
    CGRect r0 = [redView.superview convertRect:redView.bounds toView:nil];
    CGRect r1 = [redView.superview convertRect:redView.bounds toView:[UIApplication sharedApplication].delegate.window];
    CGRect r2 = [redView.superview convertRect:redView.bounds toView:self.view];
    CGRect r3 = [redView.superview convertRect:redView.frame toView:nil];
    CGRect r4 = [redView.superview convertRect:redView.frame toView:[UIApplication sharedApplication].delegate.window];
    CGRect r5 = [redView.superview convertRect:redView.frame toView:self.view];
    NSLog(@"r0:%@,r1:%@,r2:%@,r3:%@,r4:%@,r5:%@", NSStringFromCGRect(r0),NSStringFromCGRect(r1),NSStringFromCGRect(r2),NSStringFromCGRect(r3),NSStringFromCGRect(r4),NSStringFromCGRect(r5));

打印:

2022-03-08 17:05:23.332883+0800 PodDemo[13991:14490158] r0:{{0, 0}, {200, 200}},r1:{{30, 100}, {200, 200}},r2:{{30, 100}, {200, 200}},r3:{{20, 100}, {200, 200}},r4:{{50, 200}, {200, 200}},r5:{{50, 200}, {200, 200}}

特别注意:CGRect r3 = [redView.superview convertRect:redView.frame toView:nil]; 就等价于自身的frame。而不是什么相对于window的坐标。因此最好不要传nil。

相关文章

网友评论

      本文标题:convertRect:toView:

      本文链接:https://www.haomeiwen.com/subject/peiarrtx.html