美文网首页
坐标系转换

坐标系转换

作者: XIAODAO | 来源:发表于2016-07-13 10:55 被阅读68次

    1、简单的两个view放到控制器的view里,其中blueView是redView的子控件

    2、拖线到控制器里,打印各自的frame、转换坐标系后的frame

    @interface ViewController ()

    @property (weak, nonatomic) IBOutlet RedView *redView;

    @property (weak, nonatomic) IBOutlet BlueView *blueView;

    @end

    @implementation ViewController

    - (void)viewDidLoad {

    [super viewDidLoad];

    NSLog(@"self.redView:%@,self.blueView:%@",NSStringFromCGRect(self.redView.frame),NSStringFromCGRect(self.blueView.frame));

    CGRect newRectRed = [self.redView convertRect:self.redView.bounds toView:[UIApplication sharedApplication].keyWindow];

    CGRect newRectRed2 = [self.redView convertRect:self.redView.bounds toView:self.view];

    CGRect newRectBlue = [self.blueView convertRect:self.blueView.bounds toView:[UIApplication sharedApplication].keyWindow];

    CGRect newRectBlue2 = [self.blueView.superview convertRect:self.blueView.bounds toView:self.view];

    NSLog(@"newRectRed:%@,newRectBlue:%@",NSStringFromCGRect(newRectRed),NSStringFromCGRect(newRectBlue));

    NSLog(@"newRectRed2:%@,newRectBlue2:%@",NSStringFromCGRect(newRectRed2),NSStringFromCGRect(newRectBlue2));

    }

    3、结果如下

    疑问:为什么转到keywindow后的坐标和转到控制器的view的坐标不同呢?

    相关文章

      网友评论

          本文标题:坐标系转换

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