美文网首页iOS开发
CGRectInset、CGRectOffset、UIEdgeI

CGRectInset、CGRectOffset、UIEdgeI

作者: 千_城 | 来源:发表于2017-10-17 09:58 被阅读78次
        UIView *redV = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
        redV.backgroundColor = [UIColor redColor];
        [self.view addSubview:redV];
    
      // Inset 改变大小,- (向外扩张)为变大,+(向内收缩)为减小
        CGRect rect1 = CGRectInset(redV.frame, -10, 10);
        UIView *greenV = [[UIView alloc] initWithFrame:rect1];
        greenV.backgroundColor = [[UIColor greenColor] colorWithAlphaComponent:0.5];
        [self.view addSubview:greenV];
    

    效果:

    image.png
        // offSet 只平移位置,不改变大小
        CGRect rect2 = CGRectOffset(redV.frame, -10, -10);
        UIView *blueV = [[UIView alloc] initWithFrame:rect2];
        blueV.backgroundColor = [[UIColor blueColor] colorWithAlphaComponent:0.5];
        [self.view addSubview:blueV];
    

    效果:

    image.png
        // UIEdgeInsetsInsetRect 在原先的rect上内切出另一个rect出来,-为变大,+为变小
        CGRect rect3 = UIEdgeInsetsInsetRect(redV.frame, UIEdgeInsetsMake(-20, -20, -20, -20));
        UIView *yellowV = [[UIView alloc] initWithFrame:rect3];
        yellowV.backgroundColor = [[UIColor yellowColor] colorWithAlphaComponent:0.5];
        [self.view addSubview:yellowV];
    

    效果:

    image.png

    相关文章

      网友评论

        本文标题:CGRectInset、CGRectOffset、UIEdgeI

        本文链接:https://www.haomeiwen.com/subject/shiruxtx.html