iOS渐变色

2018-12-07  本文已影响2人  李二侠

渐变色需要用到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)表示右下角点。


屏幕快照 2018-12-07 上午11.20.23.png

上面代码效果如下:


屏幕快照 2018-12-07 上午11.13.27.png
上一篇 下一篇

猜你喜欢

热点阅读