分享

iOS开发 渐变色的简单实现

 玄冰优 2017-05-08

各种设计的颜色,渐变的颜色有的确实漂亮,用设计切图由于不同的机型的大小不同,不能跟其他切图完美匹配,只好是代码完成渐变色的了。


*渐变色简单实现

-(CAGradientLayer *)setChangColorWithView:(UIView *)view andColorStart:(UIColor *)startColor andEndColor:(UIColor *)endColor{
    CAGradientLayer *gradLayer = [CAGradientLayer layer];
    gradLayer.frame = view.bounds;
    gradLayer.colors = @[(__bridge id)startColor.CGColor,(__bridge id)endColor.CGColor];
    gradLayer.startPoint = CGPointMake(0.0, 1.0);
    gradLayer.endPoint = CGPointMake(1.0, 1.0);
    return gradLayer;
    return nil;
}

=======
colors 渐变的颜色
locations 渐变颜色的分割点,以上的方法没有使用到,因为是一个平缓过渡的颜色
具体使用:

 gradLayer.locations = @[@0.3, @0.5, @1.0];
startPoint&endPoint 颜色渐变的方向,范围在(0,0)与(1.0,1.0)之间,如(0,0)(1.0,0)代表水平方向渐变,(0,0)(0,1.0)代表竖直方向渐变
=====


使用

-(void)buildStatusBarBG{
    _statusBGView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kUIWidth, kUIHeight)];
    CAGradientLayer *layer = [self setChangColorWithView:_statusBGView andColorStart:[UIColor colorWithRed:0 green:172/255.0 blue:242/255.0 alpha:1.0] andEndColor:[UIColor colorWithRed:69/255.0 green:117/255.0 blue:241/255.0 alpha:1.0]];
    _statusBGView.layer.sublayers = nil;
    [_statusBGView.layer addSublayer:layer];
    [self.view addSubview:_statusBGView];
}







后来看到有人总结的比较全面,但是没有一一实现。先mark后实现。
http://www.jianshu.com/p/3e0e25fd9b85






    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约