iOS 颜色渐变

从上到下:
CAGradientLayer *gradient = [CAGradientLayer layer];
gradient.frame = self.view.bounds;
gradient.colors = [NSArray arrayWithObjects:(id)[[UIColor colorWithRed:74/255.0 green:198/255.0 blue:228/255.0 alpha:1] CGColor], (id)[[UIColor colorWithRed:104/255.0 green:215/255.0 blue:223/255.0 alpha:1] CGColor], (id)[[UIColor colorWithRed:107/255.0 green:217/255.0 blue:222/255.0 alpha:1] CGColor], (id)[[UIColor colorWithRed:156/255.0 green:238/255.0 blue:241/255.0 alpha:1] CGColor],nil];
[topView.layer insertSublayer:gradient atIndex:0];

从左到右
[nav.navigationBar.layer insertSublayer:[self gradientLayer] atIndex:0];

  • (CAGradientLayer *)gradientLayer {
    CAGradientLayer *gradientLayer = [[CAGradientLayer alloc]init];
    // CGColor是无法放入数组中的,必须要转型。
    gradientLayer.colors = @[
    (__bridge id)[UIColor colorWithRed:210 / 255.0 green:135 / 255.0 blue:35/255.0 alpha:1.0].CGColor,
    (__bridge id)[UIColor colorWithRed:219 / 255.0 green:83/ 255.0 blue:30 / 255.0 alpha:1.0].CGColor,
    (__bridge id)[UIColor colorWithRed:219 / 255.0 green:83 / 255.0 blue:35/255.0 alpha:1.0].CGColor,
    ];
    // 颜色分割线
    gradientLayer.locations = @[@0, @0.8,@1.5];
    // 颜色渐变的起点和终点,范围为 (0~1.0, 0~1.0)
    gradientLayer.startPoint = CGPointMake(0, 0);
    gradientLayer.endPoint = CGPointMake(1.0, 0);
    gradientLayer.frame = CGRectMake(0, -20,[UIScreen mainScreen].bounds.size.width,64);
    return gradientLayer;
    }
iOS 颜色渐变_第1张图片
IMG_5972.PNG

你可能感兴趣的:(iOS 颜色渐变)