zoukankan      html  css  js  c++  java
  • iOS开发---转换坐标系

     1 - (void)viewDidLoad
     2 {
     3     [super viewDidLoad];
     4     
     5     // 蓝色
     6     UIView *blue = [[UIView alloc] init];
     7     blue.backgroundColor = [UIColor blueColor];
     8     blue.frame = CGRectMake(0, 50, 200, 200);
     9     [self.view addSubview:blue];
    10     self.blue = blue;
    11     
    12     // 红色
    13     UIView *red = [[UIView alloc] init];
    14     red.backgroundColor = [UIColor redColor];
    15     red.frame = CGRectMake(50, 60, 100, 100);
    16     [blue addSubview:red];
    17     self.red = red;
    18     
    19     // 黄色
    20     UIView *yellow = [[UIView alloc] init];
    21     yellow.backgroundColor = [UIColor yellowColor];
    22     yellow.frame = CGRectMake(10, 10, 50, 50);
    23     [red addSubview:yellow];
    24     self.yellow = yellow;
    25     
    26     // 紫色
    27     UIView *purple = [[UIView alloc] init];
    28     purple.backgroundColor = [UIColor purpleColor];
    29     purple.frame = CGRectMake(150, 350, 100, 100);
    30     [self.view addSubview:purple];
    31     self.purple = purple;
    32 }
    33 
    34 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
    35 {
    36 //    NSLog(@"%@", NSStringFromCGRect(self.yellow.bounds));
    37 //    NSLog(@"%@", NSStringFromCGRect(self.yellow.frame));
    38     
    39     // 计算self.yellow在self.blue中的位置和尺寸
    40 //    CGRect newRect = [self.yellow convertRect:self.yellow.bounds toView:self.blue];
    41     
    42     
    43     // 计算self.yellow在self.purple中的位置和尺寸
    44 //    CGRect newRect = [self.yellow.superview convertRect:self.yellow.frame toView:self.purple];
    45     
    46     // 计算self.red在self.yellow中的位置和尺寸
    47     //    CGRect newRect = [self.red convertRect:self.red.bounds toView:self.yellow];
    48     // 计算self.red在self.yellow中的位置和尺寸
    49 //    CGRect newRect = [self.yellow convertRect:self.red.bounds fromView:self.red];
    50     
    51     // 计算self.red在屏幕中的位置和尺寸(nil代表屏幕)
    52     CGRect newRect = [self.red convertRect:self.red.bounds toView:nil];
    53     NSLog(@"%@", NSStringFromCGRect(newRect));
    54 }
  • 相关阅读:
    模拟死锁
    B站学习斯坦福大学Swift 语言教程 iOS11 开发【第一集】踩到的几个坑(XCode 13.2.1版本)
    数学之美番外篇:平凡而又神奇的贝叶斯方法
    joj 1753: Street Numbers
    二叉树的三种遍历(递归+非递归)
    joj 1905: Freckles
    joj 2630: A Pair of Graphs(同构图的判定)
    vue3.x 中获取dom元素
    defineProperty 和 Proxy 的区别
    vue 按钮的防抖和节流
  • 原文地址:https://www.cnblogs.com/myios/p/4691550.html
Copyright © 2011-2022 走看看