您的位置:首页 > 移动开发 > IOS开发

ios开发-坐标系转换

2015-09-16 21:28 639 查看
#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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: