ios开发-坐标系转换

88 篇文章 0 订阅
#import "ViewController.h"

@interface ViewController ()

@property(nonatomic,weak)UIView *blue;
@property(nonatomic,weak)UIView *red;
@property(nonatomic,weak)UIView *yellow;
@property(nonatomic,weak)UIView *purple;


@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
 
    //蓝色
    UIView *blue = [[UIView alloc]init];
    blue.backgroundColor = [UIColor blueColor];
    blue.frame = CGRectMake(0, 50, 200, 200);
    [self.view addSubview:blue];
    self.blue = blue;

    //红色
    UIView *red = [[UIView alloc]init];
    red.backgroundColor = [UIColor redColor];
    red.frame = CGRectMake(50 , 60, 100, 100);
    [self.blue addSubview:red];
    self.red = red;
    
    //黄色
    UIView *yellow = [[UIView alloc]init];
    yellow.backgroundColor = [UIColor yellowColor];
    yellow.frame = CGRectMake(10, 10, 50, 50);
    [red addSubview:yellow];
    self.yellow = yellow;
    
    //紫色
    UIView *purple = [[UIView alloc]init];
    purple.backgroundColor = [UIColor yellowColor];
    purple.frame = CGRectMake(150, 350, 100, 100);
    [self.view addSubview:purple];
    self.yellow = purple;
    
    
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    
    //原点变了相当于坐标系平移
//    CGRect newRect = [self.red convertRect:CGRectMake(50, 50, 50, 50) fromView:self.blue];
//    NSLog(@"%@",NSStringFromCGRect(newRect));
//    
//    NSLog(@"%@",NSStringFromCGRect(self.yellow.bounds));
//    
//    NSLog(@"%@",NSStringFromCGRect(self.yellow.frame));
    
    //黄色在蓝色中的位置
    //CGRect newRect = [self.yellow convertRect:CGRectMake(0, 0, 50, 50) toView:self.blue];
//    CGRect newRect = [self.yellow convertRect:self.yellow.bounds toView:self.blue];
    
    //计算self.yellow 在其他控件中的位置
//    CGRect newRect1 = [self.yellow convertRect:self.yellow.bounds fromView:self.blue];
//    CGRect newRect = [self.yellow.superview convertRect:self.yellow.frame fromView:self.blue  ];
//    

    //黄色在紫色中的位置
//    CGRect newRect = [self.yellow.superview convertRect:self.yellow.frame toView:self.purple];
    
    //计算self.red 在主窗口中的位置和尺寸(nil 代表屏幕)
    
    CGRect newRect = [self.red convertRect:self.red.bounds toView:nil];
    
    NSLog(@"%@",NSStringFromCGRect(newRect));
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值