iOS 转换坐标系

本文介绍了在iOS中如何使用UIView的API进行坐标系转换,包括convertPoint和convertRect方法,通过示例代码展示了如何在不同视图之间转换坐标,并打印了转换前后的坐标信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

iOS中视图坐标系的转换主要用到UIView的四个API:
坐标系转换的实质是: 更改坐标系的原点.
- (CGPoint)convertPoint:(CGPoint)point toView:(nullable UIView *)view;
- (CGPoint)convertPoint:(CGPoint)point fromView:(nullable UIView *)view;
- (CGRect)convertRect:(CGRect)rect toView:(nullable UIView *)view;
- (CGRect)convertRect:(CGRect)rect fromView:(nullable UIView *)view;

代码:

#import "ViewController.h"

@interface ViewController ()
@property(nonatomic,strong)UIView *redView;
@property(nonatomic,strong)UIView *blueView;
@property(nonatomic,strong)UIView *yellowView;
@property(nonatomic,strong)UIView *orangeView;
@end

@implementation ViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    _redView = [[UIView alloc]initWithFrame:CGRectMake(50, 50, 300, 300)];
    _redView.backgroundColor = [UIColor redColor];
    [self.view addSubview:_redView];

    _blueView = [[UIView alloc]initWithFrame:CGRectMake(30, 30, 250, 250)];
    _blueView.backgroundColor = [UIColor blueColor];
    [_redView addSubview:_blueView];

    _yellowView = [[UIView alloc]initWithFrame:CGRectMake(30, 30, 200, 200)];
    _yellowView.backgroundColor = [UIColor yellowColor];
    [_blueView addSubview:_yellowView];

    _orangeView = [[UIView alloc]initWithFrame:CGRectMake(30, 30, 150, 150)];
    _orangeView.backgroundColor = [UIColor orangeColor];
    [_yellowView addSubview:_orangeView];  
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{

    NSLog(@"橘色view在父控件中的frame = %@",NSStringFromCGRect(self.orangeView.frame));
    // 两种写法 含义相同 第二种写法 更加清晰
    // 如果toView为空 默认为当前主窗口 即UIWindow
    CGRect orange_in_blue_frame1 = [self.orangeView.superview convertRect:self.orangeView.frame toView:self.blueView];
    CGRect orange_in_blue_frame2 = [self.orangeView convertRect:self.orangeView.bounds toView:self.blueView];
    NSLog(@"橘色view在蓝色view中的frame1 = %@",NSStringFromCGRect(orange_in_blue_frame1));
    NSLog(@"橘色view在蓝色view中的frame2 = %@",NSStringFromCGRect(orange_in_blue_frame2));

    CGRect orange_in_red_frame = [self.orangeView.superview convertRect:self.orangeView.frame toView:self.redView];
    NSLog(@"橘色view在红色view中的frame = %@",NSStringFromCGRect(orange_in_red_frame));

    CGRect orange_in_VCView_frame = [self.orangeView.superview convertRect:self.orangeView.frame toView:self.view];
    NSLog(@"橘色view在控制器view中的frame = %@",NSStringFromCGRect(orange_in_VCView_frame));

    CGRect yellow_in_VCView_frame = [self.view convertRect:self.yellowView.frame fromView:self.yellowView.superview];
    NSLog(@"黄色view在控制view中的frame = %@",NSStringFromCGRect(yellow_in_VCView_frame));
}
@end

打印结果:
2016-04-15 15:51:33.543 坐标系转换[2744:154111] 橘色view在父控件中的frame = {{30, 30}, {150, 150}}
2016-04-15 15:51:33.544 坐标系转换[2744:154111] 橘色view在蓝色view中的frame1 = {{60, 60}, {150, 150}}
2016-04-15 15:51:33.544 坐标系转换[2744:154111] 橘色view在蓝色view中的frame2 = {{60, 60}, {150, 150}}
2016-04-15 15:51:33.544 坐标系转换[2744:154111] 橘色view在红色view中的frame = {{90, 90}, {150, 150}}
2016-04-15 15:51:33.544 坐标系转换[2744:154111] 橘色view在控制器view中的frame = {{140, 140}, {150, 150}}
2016-04-15 15:51:33.544 坐标系转换[2744:154111] 黄色view在控制view中的frame = {{110, 110}, {200, 200}}
截图--1

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值