ios坐标位置转换

时间:2020-12-24 10:31:31

//ios常用坐标转换来处理一些下拉框队形的按钮的位置,我以最下面两个来进行一下个人的理解,不足之处多多见谅

- (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;

 

/*

  将当前的视图的frame 转换成到距离目标视图的距离,返回真实视图的距离

    rc值 = view.fram+rect

*/

  //view1为目标视图 self.view表示当前视图

    CGRect rect = CGRectMake(view1.frame.size.width/2, view1.frame.size.height, view1.frame.size.width, view1.frame.size.height);

       CGRect rc = [view2 convertRect:rect fromView:view1];

    view2.frame =rc;

 

  /*

     将当前的视图的frame 转换成到目标视图的距离 返回到目标位置的距离

     frame = rect -view.frame

    */

    CGRect rect = CGRectMake(200, 200, view1.frame.size.width, view1.frame.size.height);

    CGRect rc = [view2 convertRect:rect toView:view1];

    view2.frame =rc;

    NSLog(@"%@",NSStringFromCGRect(rc));