zoukankan      html  css  js  c++  java
  • iOS convertRect:view.frame toView: x 异常翻倍?

    项目中遇到过,故查资料自己写Demo测试。

     1     UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
     2     scrollView.backgroundColor = [UIColor whiteColor];
     3     [self.view addSubview:scrollView];
     4     
     5     // 来源 view
     6     UIView *sourceView = [[UIView alloc] initWithFrame:CGRectMake(100, 440, 100, 40)];
     7     sourceView.backgroundColor = [UIColor redColor];
     8     [scrollView addSubview:sourceView];
     9     
    10     // 目标 view
    11     UIView *destinationView = [[UIView alloc] initWithFrame:CGRectMake(20, 100, 320, 100)];
    12     destinationView.backgroundColor = [UIColor greenColor];
    13     [self.view addSubview:destinationView];
    14     
    15 label0:// 错误转换
    16     {
    17         // 异常翻倍 目标x+|来源x-目标x|*2 目标y+|来源y-目标y|*2
    18         CGRect rect = [sourceView convertRect:sourceView.frame toView:destinationView];
    19         NSLog(@"%@", NSStringFromCGRect(rect));
    20         
    21         // 两种正确方法
    22         // label3: [sourceView.superview convertRect:sourceView.frame  toView:destinationView];
    23         // label1: [sourceView           convertRect:sourceView.bounds toView:destinationView];
    24         // 对convertRect 调用者理解有误,应该是需要转换控件的参考系视图,而不是自身。
    25     }
    26     
    27 label1:
    28     {
    29         // 来源 view 的尺寸要映射到目标 view 上返回其尺寸
    30         CGRect rect = [sourceView convertRect:sourceView.bounds toView:destinationView];
    31         NSLog(@"%@", NSStringFromCGRect(rect));
    32     }
    33     
    34 label2:
    35     {
    36         CGRect rect = [destinationView convertRect:sourceView.bounds fromView:sourceView];
    37         NSLog(@"%@", NSStringFromCGRect(rect));
    38     }
    39     
    40 label3:
    41     {
    42         CGRect rect = [sourceView.superview convertRect:sourceView.frame toView:destinationView];
    43         NSLog(@"%@", NSStringFromCGRect(rect));
    44     }
    45     
    46 label4:// 错误转换
    47     {
    48         // 异常翻倍 目标x+|来源x-目标x|*2 目标y+|来源y-目标y|*2
    49         CGRect rect = [destinationView convertRect:sourceView.frame fromView:sourceView];
    50         NSLog(@"%@", NSStringFromCGRect(rect));
    51         // 两种正确方法
    52         // label5: [destinationView convertRect:sourceView.frame  fromView:sourceView.superview];
    53         // label2: [destinationView convertRect:sourceView.bounds fromView:sourceView];
    54         // 对convertRect 调用者理解有误,应该是需要转换控件的参考系视图,而不是自身。
    55     }
    56     
    57 label5:
    58     {
    59         CGRect rect = [destinationView convertRect:sourceView.frame fromView:sourceView.superview];
    60         NSLog(@"%@", NSStringFromCGRect(rect));
    61     }
    62     
    63     /*
    64      - (CGPoint)convertPoint:(CGPoint)point toView:(nullable UIView *)view;
    65      - (CGPoint)convertPoint:(CGPoint)point fromView:(nullable UIView *)view;
    66      - (CGRect)convertRect:(CGRect)rect toView:(nullable UIView *)view;
    67      - (CGRect)convertRect:(CGRect)rect fromView:(nullable UIView *)view;
    68      总结:
    69         1. from 和 to 是相对应的一组功能, 熟练一种即可,本人习惯使用 to。
    70         2. 作用:计算[源]上的[被操作的对象]相对于[目标]的frame。from: 源 to: 目标 那么相对应的就是调用者是 from 或者 to 对应的
    71         3. 调用者应该是需要转换控件的参考系视图,而不是自身。
    72         4. point 和 rect 的 差别也不多。
    73      */

    Demo链接: https://pan.baidu.com/s/1skEi0FV 密码: 8swh

    时常一个人发呆,看到宁静的天空。
  • 相关阅读:
    December 23rd 2016 Week 52nd Friday
    December 22nd 2016 Week 52nd Thursday
    December 21st 2016 Week 52nd Wednesday
    December 20th 2016 Week 52nd Tuesday
    December 19th 2016 Week 52nd Sunday
    December 18th 2016 Week 52nd Sunday
    uva294(唯一分解定理)
    uva11624Fire!(bfs)
    fzu2150Fire Game(双起点bfs)
    poj3276Face The Right Way
  • 原文地址:https://www.cnblogs.com/pinweyshg/p/7289724.html
Copyright © 2011-2022 走看看