iOS渐变色

渐变色需要用到CAGradientLayer这个类。

直接上代码

   UIView * bgView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 200)];
    [self.view addSubview:bgView];

    NSArray *colors = [NSArray arrayWithObjects:(id)[UIColor greenColor].CGColor, (id)[UIColor orangeColor].CGColor, nil];
    CAGradientLayer *gradient = [CAGradientLayer layer];
    //设置开始和结束位置(设置渐变的方向)
    gradient.startPoint = CGPointMake(0, 0);
    gradient.endPoint = CGPointMake(1, 1);;
    gradient.colors = colors;
    gradient.frame = bgView.bounds;
    [bgView.layer insertSublayer:gradient atIndex:0];

其中startPoint和endPoint 的x和y的范围都是0到1,(0,0)表示左上角顶点,(0,1)表示右上角顶点,(0,1)左下角点 ,(1,1)表示右下角点。


iOS渐变色_第1张图片
屏幕快照 2018-12-07 上午11.20.23.png

上面代码效果如下:


iOS渐变色_第2张图片
屏幕快照 2018-12-07 上午11.13.27.png

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