iOS开发

CGRectInset、CGRectOffset、UIEdgeI

2017-10-17  本文已影响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
上一篇下一篇

猜你喜欢

热点阅读