美文网首页
iOS 实现背景颜色渐变的方式

iOS 实现背景颜色渐变的方式

作者: Bonucci | 来源:发表于2017-04-16 14:41 被阅读0次

1.通过CAGradientLayer 设置渐变的背景。

  /** 
     *  1.通过CAGradientLayer 设置渐变的背景。 
     */  
    CAGradientLayer *layer = [CAGradientLayer new];  
    //colors存放渐变的颜色的数组  
    layer.colors=@[(__bridge id)[UIColor greenColor].CGColor,(__bridge id)[UIColor whiteColor].CGColor];  
    /** 
     * 起点和终点表示的坐标系位置,(0,0)表示左上角,(1,1)表示右下角 
     */  
    layer.startPoint = CGPointMake(0.5, 0);  
    layer.endPoint = CGPointMake(0.5, 1);  
    layer.frame = self.bounds;  
    [self.layer addSublayer:layer];  
      

方法2.CGGradientRef

- (void)drawRect:(CGRect)rect{
    CGContextRef ctx = UIGraphicsGetCurrentContext();  
    CGColorSpaceRef colorSpace=CGColorSpaceCreateDeviceRGB();  
    /*指定渐变色 
     space:颜色空间 
     components:颜色数组,注意由于指定了RGB颜色空间,那么四个数组元素表示一个颜色(red、green、blue、alpha), 
     如果有三个颜色则这个数组有4*3个元素 
     locations:颜色所在位置(范围0~1),这个数组的个数不小于components中存放颜色的个数 
     count:渐变个数,等于locations的个数 
     */  
    CGFloat compoents[12]={  
        0,0,0,1,  
        0.8,0.1,0.5,1.0,  
        1.0,1.0,1.0,1.0  
    };  
      
    //设置渐变的位置  
    CGFloat locations[3]={0,0.3,1.0};  
    //创建梯度上下文  
    CGGradientRef gradient= CGGradientCreateWithColorComponents(colorSpace, compoents, locations, 3);  
      
    /*绘制线性渐变 
     context:图形上下文 
     gradient:渐变色 
     startPoint:起始位置 
     endPoint:终止位置 
     options:绘制方式,kCGGradientDrawsBeforeStartLocation 开始位置之前就进行绘制,到结束位置之后不再绘制, 
     kCGGradientDrawsAfterEndLocation开始位置之前不进行绘制,到结束点之后继续填充 
      
     startPoint endPoint 不同与上一种方法,指的是真正的坐标 
     */  
    CGContextDrawLinearGradient(ctx, gradient, CGPointMake(self.frame.size.width/2, 0), CGPointMake(self.frame.size.width/2,self.frame.size.height), kCGGradientDrawsAfterEndLocation);  
      
    //释放颜色空间  
    CGColorSpaceRelease(colorSpace);  
}

这里面也有用,暂时保存

http://www.jianshu.com/p/3e0e25fd9b85

相关文章

  • iOS 实现背景颜色渐变的方式

    1.通过CAGradientLayer 设置渐变的背景。 方法2.CGGradientRef 这里面也有用,暂时保...

  • 2019-10-25

    iOS 渐变色 实现渐变色的几种方式: 使用场景:背景渐变 1. 通过CAGradientLayer实现 使用场景...

  • iOS 渐变色实现

    实现方式 iOS实现控件背景颜色渐变实现方式,我所知道的目前有三种。第一种:用图片代替,简单直接在控件上面放一张渐...

  • iOS 颜色渐变

    iOS里面的背景颜色实现渐变的方式有两种,第一种就是给一个渐变的图片做背景,当然这样有很大的局限性,比如我现在就需...

  • iOS绘制渐变色背景

    iOS实现颜色渐变 我们在平时开发经常就用一个图片来代替背景图,但是如果没有背景图片,怎么实现下面的效果?今天的这...

  • (IOS)UIView背景颜色渐变

    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0...

  • iOS绘制渐变色背景(二):带有文字的UIView

    iOS实现颜色渐变第二篇 在记录完上一篇的"iOS绘制渐变色背景"学习之后,有一位小伙伴get了上篇文章还给我留了...

  • iOS实现颜色渐变

    我们经常会在UIView添加渐变的背景色。虽然找一张渐变颜色的背景图很方便,但是图片是要占用资源的,同时文件也会变...

  • iOS渐变颜色实现

    在iOS中,每一个可见的控件都有一个layer层。该层控制着控件的绘制和重绘。近期在YY交友项目中有要求使用一个渐...

  • iOS通过Category实现UIView渐变的背景颜色

    项目中有很多View和Button需要设置渐变的背景颜色,所以写了一个Category来方便调用。 调用方式,im...

网友评论

      本文标题: iOS 实现背景颜色渐变的方式

      本文链接:https://www.haomeiwen.com/subject/qzeaattx.html