iOS中的坐标转换

2018-05-31  本文已影响0人  hard_coder

//    1.convertRect的使用

//    1  [A convertRect:B.frame  toView:C];

//    计算A上的B视图在C中的位置CGRect

//    2  [A convertRect:B.frame  fromView:C];

//    计算C上的B视图在A中的位置CGRect

//    2.convertPoint的使用

//    1  [A convertPoint:B.center toView:C];

//    计算A上的B视图在C中的位置CGPoint

//    2  [A convertPoint:B.center fromView:C];

//    计算C上的B视图在A中的位置CGPoint

- (void)viewDidLoad {

//    1.convertRect的使用

//    1  [A convertRect:B.frame  toView:C];

//    计算A上的B视图在C中的位置CGRect

//    2  [A convertRect:B.frame  fromView:C];

//    计算C上的B视图在A中的位置CGRect

//    2.convertPoint的使用

//    1  [A convertPoint:B.center toView:C];

//    计算A上的B视图在C中的位置CGPoint

//    2  [A convertPoint:B.center fromView:C];

//    计算C上的B视图在A中的位置CGPoint

    [super viewDidLoad];

    UIView *redView = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];

    redView.backgroundColor = [UIColor redColor];

    [self.viewaddSubview:redView];

    UIView *blueView = [[UIView alloc] initWithFrame:CGRectMake(150, 150, 30, 30)];

    blueView.backgroundColor = [UIColor blueColor];

    [self.viewaddSubview:blueView];

    //[A convertPoint:B.center toView:C];

    //    计算A上的B视图在C中的位置CGPoint

    CGPointconverPoint = [self.viewconvertPoint:blueView.frame.origintoView:redView];

    NSLog(@"%f,%f",converPoint.x, converPoint.y);

    //    2  [A convertPoint:B.center fromView:C];

    //    计算C上的B视图在A中的位置CGPoint

    CGPointconverFromPoint = [redViewconvertPoint:blueView.frame.originfromView:self.view];

    NSLog(@"%f,%f",converFromPoint.x,converFromPoint.y);

    //    1  [A convertRect:B.frame  toView:C];

    //    计算A上的B视图在C中的位置CGRect

    CGRectconverToRect = [self.viewconvertRect:blueView.frametoView:redView];

    NSLog(@"%f,%f,%f,%f",converToRect.origin.x,converToRect.origin.y,converToRect.size.width,converToRect.size.height);

    // [A convertPoint:B.center fromView:C];

    //    计算C上的B视图在A中的位置CGPoint

    CGRectconverFromRect = [redViewconvertRect:blueView.framefromView:self.view];

    NSLog(@"%f,%f,%f,%f",converFromRect.origin.x,converFromRect.origin.y,converFromRect.size.width,converFromRect.size.height);

}

上一篇下一篇

猜你喜欢

热点阅读