2019-03-10 CGRectInset,CGRectOffset

今天是下大雨,今天乘着下雨天来学习一下UIView的基础😆... 拒绝慌不择路的写代码

1. CGRectInset(rect,dx,dy) 平移和缩放

    CGRect rect = CGRectMake(100, 100, 100 , 100);
    UIView *testView = [[UIView alloc] initWithFrame:rect];
    testView.backgroundColor = [UIColor redColor];
    [self.view addSubview: testView];
    
    CGRect rect1 = CGRectInset(rect, 30, 10);
    NSLog(@"%@",NSStringFromCGRect(rect1));
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3* NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [UIView animateWithDuration:0.25 animations:^{
            testView.frame = rect1;
        }];
    });
控制台打印: {{130, 110}, {40, 80}}

CGRectInset(rect,dx,dy)

  • rect是原来的frame
  • dx为正则origin x加上dx,为负数则减(PS.其实都是加,加上一个负数就是减啊)
  • dy 和dx一样处理 y加上dy
  • rect的size大小则是 width是(rect.size.width - dx*2) height是 (rect.size.height - dy*2) 就是dx越大缩放的宽度越小 dy越大缩放的高度度越小
    (正缩小 负增大)

2. CGRectOffset(rect,dx,dy) 仅仅只是平移

    CGRect rect = CGRectMake(100, 100, 100 , 100);
    UIView *testView = [[UIView alloc] initWithFrame:rect];
    testView.backgroundColor = [UIColor redColor];
    [self.view addSubview: testView];

    CGRect rect1 = CGRectOffset(rect, 30, 30);
    NSLog(@"%@",NSStringFromCGRect(rect1));
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3* NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [UIView animateWithDuration:0.25 animations:^{
            testView.frame = rect1;
        }];
    });
  • CGRectInset(rect,dx,dy)同理,唯一区别就是size不变, 只有x和y的变化

3. convertRect: toView: 切换坐标系 也就是坐标变 大小不变

从一个子视图从父视图移除加到其他视图的上,需要重新计算frame,然鹅这个API就免去了去书写计算的步骤

    CGRect rect = CGRectMake(100, 100, 100 , 100);
    UIView *testView = [[UIView alloc] initWithFrame:rect];
    testView.backgroundColor = [UIColor redColor];
    [self.view addSubview: testView];
    
    CGRect rect1 = CGRectMake(30, 30, 60 , 60);
    UIView *subView = [[UIView alloc] initWithFrame:rect1];
    subView.backgroundColor = [UIColor greenColor];
    [testView addSubview: subView];

    CGRect frame = [subView convertRect:subView.bounds toView: self.view];
    NSLog(@"%@",NSStringFromCGRect(frame));
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [UIView animateWithDuration:0.25 animations:^{
            [subView removeFromSuperview];
            subView.frame = frame;
            [self.view addSubview: subView];
            
        }];
    });
转换前的坐标 ==>  {{30, 30}, {60, 60}}
转换后的坐标 ==>  {{130, 130}, {60, 60}}
  • scrollView里的子控件里的子控件如何确定在scrollView里的位置的一个方案, 还有就是tableview通过cell获取在tableview上的位置
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 废话不多说,直接上干货 ---------------------------------------------...
    小小赵纸农阅读 3,436评论 0 15
  • 首先感谢小易童鞋,这里自己也整理下这些容易忘记的代码 设置导航栏的背景颜色用barTintColor self.n...
    HT_Jonson阅读 954评论 0 51
  • Swift1> Swift和OC的区别1.1> Swift没有地址/指针的概念1.2> 泛型1.3> 类型严谨 对...
    cosWriter阅读 11,142评论 1 32
  • *7月8日上午 N:Block :跟一个函数块差不多,会对里面所有的内容的引用计数+1,想要解决就用__block...
    炙冰阅读 2,547评论 1 14
  • 2018.9.13 星期四 晴 亲子日记第284天 中午放学回家的路上,女儿说起自己的考试情况,数学没有不会的,应...
    涓涓流水_672f阅读 225评论 0 3