2019-03-10 CGRectInset,CGRectOff

2019-03-10  本文已影响0人  BoooBooo冰Booo

今天是下大雨,今天乘着下雨天来学习一下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)

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;
        }];
    });

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}}
上一篇 下一篇

猜你喜欢

热点阅读