zoukankan      html  css  js  c++  java
  • iOS开发笔记--UIView中的坐标转换(convertRect)

    // 将像素point由point所在视图转换到目标视图view中,返回在目标视图view中的像素值
    - (CGPoint)convertPoint:(CGPoint)point toView:(UIView *)view;
    // 将像素point从view中转换到当前视图中,返回在当前视图中的像素值
    - (CGPoint)convertPoint:(CGPoint)point fromView:(UIView *)view;
    
    // 将rect由rect所在视图转换到目标视图view中,返回在目标视图view中的rect
    - (CGRect)convertRect:(CGRect)rect toView:(UIView *)view;
    // 将rect从view中转换到当前视图中,返回在当前视图中的rect
    - (CGRect)convertRect:(CGRect)rect fromView:(UIView *)view;
    // controllerA 中有一个UITableView, UITableView里有多行UITableVieCell,cell上放有一个button
    // 在controllerA中实现:
    CGRect rc = [cell convertRect:cell.btn.frame toView:self.view];
    或
    CGRect rc = [self.view convertRect:cell.btn.frame fromView:cell];
    // 此rc为btn在controllerA中的rect
    
    或当已知btn时:
    CGRect rc = [btn.superview convertRect:btn.frame toView:self.view];
    或
    CGRect rc = [self.view convertRect:btn.frame fromView:btn.superview];

    转摘自:http://blog.csdn.net/xuhuan_wh/article/details/8486337

    心得:仔细理会有其道理,哪个view上的尺寸,要到哪个view上去挖相同的尺寸(总之相对于屏幕位置不变)。总之就是一个View转换到另一个View,就是直接将要转换的Frame往外面凸起或者凹进去,到目标View上对应的尺寸

    eg:

    UIScrollView *ss= [[UIScrollView alloc]initWithFrame:self.view.bounds];
        ss.backgroundColor=[UIColor whiteColor];
        ss.contentSize=CGSizeMake(ss.frame.size.width, ss.frame.size.height*2);
        //原始View
        UIView *vv=[[UIView alloc]initWithFrame:CGRectMake(100, 580, 100, 40)];
        vv.backgroundColor=[UIColor redColor];
        [ss addSubview:vv];
        [self.view addSubview:ss];
        
        //目标View
        UIView *backgroundView=[[UIView alloc]initWithFrame:CGRectMake(0, 100, 320, 100)];
        backgroundView.backgroundColor=[UIColor brownColor];
        [self.view addSubview:backgroundView];
        //原始View的尺寸要映射到目标View上返回其尺寸
        CGRect rect=[vv convertRect:vv.bounds toView:backgroundView];
        NSLog(@"%@",NSStringFromCGRect(rect));
        CGRect rect1=[backgroundView convertRect:vv.bounds fromView:vv];
        NSLog(@"%@",NSStringFromCGRect(rect1));
    输出结果:2015-02-05 10:02:10.301 testConvertRect[1262:27344] {{100, 480}, {100, 40}}
            2015-02-05 10:02:10.302 testConvertRect[1262:27344] {{100, 480}, {100, 40}}
  • 相关阅读:
    html中的浮动
    Html中元素的分类
    前端标签命名规范
    meta详解
    CSS的嵌套方法
    html标签
    W3C标准
    AE待整理
    AE小知识点备忘录
    Maximum Subarray
  • 原文地址:https://www.cnblogs.com/liyang31tg/p/4273971.html
Copyright © 2011-2022 走看看