IOS渐变图层CAGradientLayer

看支付宝蚂蚁积分,天气预报等好多APP都有圆形渐变效果,今天就试着玩了。

一.CAGradientLayer类中属性介绍

CAGradientLayer继承CALayer,主要有以下几个属性:

[email protected](nullable, copy) NSArray *colors; 渐变的颜色

[email protected](nullable, copy) NSArray<NSNumber *> *locations;每种颜色的最亮的位置

[email protected] CGPoint startPoint;  @property CGPoint endPoint; 渐变的方向 左上(0,0)  右下(1,1) startPoint——>endPoint

[email protected](copy) NSString *type; 目前只有一种kCAGradientLayerAxial

通过设置上面的属性来看下效果

  

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    CAGradientLayer *gradientLayer =  [CAGradientLayer layer];

    gradientLayer.frame = CGRectMake(20, 100, 200, 200);

    //设置颜色
    [gradientLayer setColors:[NSArray arrayWithObjects:(id)[[UIColor greenColor] CGColor],(id)[[UIColor redColor] CGColor],(id)[[UIColor yellowColor] CGColor],(id)[[UIColor blueColor] CGColor],(id)[[UIColor redColor] CGColor], nil]];

    //每种颜色最亮的位置
    [gradientLayer setLocations:@[@0,@0.2,@0.4,@0.8,@1]];
    //渐变的方向StartPoint->EndPoint
    [gradientLayer setStartPoint:CGPointMake(0, 0)];
    [gradientLayer setEndPoint:CGPointMake(1, 1)];

    [self.view.layer addSublayer:gradientLayer];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

二 .使用CAGradientLayer+UIBezierPath实现圆形渐变

1.自定义GredientLayerView

#import <UIKit/UIKit.h>

@interface GredientLayerView : UIView

@end
//
//  GredientLayerView.m
//  GredientLayerView
//
//  Created by City--Online on 15/10/26.
//  Copyright © 2015年 City--Online. All rights reserved.
//

#import "GredientLayerView.h"

#define degreesToRadians(x) (M_PI*(x)/180.0) //把角度转换成PI的方式

static const float kPROGRESS_LINE_WIDTH=4.0;

@interface GredientLayerView ()

@property (nonatomic,strong) CAShapeLayer *progressLayer;

@end
@implementation GredientLayerView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {

        //设置贝塞尔曲线
        UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(self.bounds.size.width/2, self.bounds.size.height/2) radius:(frame.size.width-kPROGRESS_LINE_WIDTH)/2 startAngle:degreesToRadians(-210) endAngle:degreesToRadians(30) clockwise:YES];

        //遮罩层
        _progressLayer = [CAShapeLayer layer];
        _progressLayer.frame = self.bounds;
        _progressLayer.fillColor =  [[UIColor clearColor] CGColor];
        _progressLayer.strokeColor=[UIColor redColor].CGColor;
        _progressLayer.lineCap = kCALineCapRound;
        _progressLayer.lineWidth = kPROGRESS_LINE_WIDTH;

        //渐变图层
        CAGradientLayer *gradientLayer =  [CAGradientLayer layer];
        gradientLayer.frame = _progressLayer.frame;
        [gradientLayer setColors:[NSArray arrayWithObjects:(id)[[UIColor redColor] CGColor],(id)[[UIColor yellowColor] CGColor],(id)[[UIColor blueColor] CGColor], nil]];
        [gradientLayer setLocations:@[@0,@0.6,@1]];
        [gradientLayer setStartPoint:CGPointMake(0, 0)];
        [gradientLayer setEndPoint:CGPointMake(1, 0)];

        //用progressLayer来截取渐变层 遮罩
        [gradientLayer setMask:_progressLayer];
        [self.layer addSublayer:gradientLayer];

        //增加动画
        CABasicAnimation *pathAnimation=[CABasicAnimation animationWithKeyPath:@"strokeEnd"];
        pathAnimation.duration = 2;
        pathAnimation.timingFunction=[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
        pathAnimation.fromValue=[NSNumber numberWithFloat:0.0f];
        pathAnimation.toValue=[NSNumber numberWithFloat:1.0f];
        pathAnimation.autoreverses=NO;
        _progressLayer.path=path.CGPath;
        [_progressLayer addAnimation:pathAnimation forKey:@"strokeEndAnimation"];
    }
    return self;
}

@end

2.调用

//
//  ViewController.m
//  Gredientlayer
//
//  Created by City--Online on 15/10/26.
//  Copyright © 2015年 City--Online. All rights reserved.
//

#import "ViewController.h"
#import "GredientLayerView.h"
@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    GredientLayerView *gredientLayerView=[[GredientLayerView alloc]initWithFrame:CGRectMake(30, 100, 200, 200)];
    [self.view addSubview:gredientLayerView];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

3.效果

时间: 2024-10-11 04:21:19

IOS渐变图层CAGradientLayer的相关文章

IOS进度渐变图层CAGradientLayer

看支付宝蚂蚁积分,天气预报等好多APP都有圆形渐变效果,今天就试着玩了. 一.CAGradientLayer类中属性介绍 CAGradientLayer继承CALayer,主要有以下几个属性: [email protected](nullable, copy) NSArray *colors; 渐变的颜色 这个数组中只设置一个颜色是不显示的 [email protected](nullable, copy) NSArray<NSNumber *> *locations;每种颜色的最亮的位置 [

IOS 创建渐变图层

代码如下 typedef enum { GradientLayerKindLeftRight = 2000, GradientLayerKindUpDown, GradientLayerKindLBRT,//左下-右上 GradientLayerKindLTRB //左上-右下 }GradientLayerKind; //-------------------------------------------------------------- // 创建矩形渐变图层 // // @param

给某个view增加颜色渐变图层

//给某个view增加颜色透明度渐变图层 - (void) insertTransparentGradient { NSLog(@"%@",NSStringFromCGRect(self.imgView.bounds)); UIColor *colorOne = [UIColor colorWithRed:(33/255.0)  green:(33/255.0)  blue:(33/255.0)  alpha:0.0]; UIColor *colorTwo = [UIColor col

cocos2dx中的背景图层CCLayerColor和渐变图层CCLayerGradient

1.CCLayerColor是专门用来处理背景颜色的图层,它继承自CCLayer,可以用来设置图层的背景颜色,因为CCLayer默认是透明色的,即无颜色的 2.CCLayerGradient是用来显示颜色渐变效果的图层,它继承自CCLayerColor,是CCLayer的孙类 3.几个特殊的图层:CCLayerColor,CCLayerGradient  颜色图层在游戏中主要用来烘托背景,可以按照RGB设置填充颜色,同时还可以设置图层的透明度,常用于背景    颜色图层还存在一个特殊的子类:CC

iOS开发——图层OC篇&amp;UIColor深入研究(CGColor,CIColor)

UIColor深入研究(CGColor,CIColor) 由于跟人比较喜欢研究关于图层与动画方面的技术,正打算看看别人写的好东西,就遇到了好几个问题, 第一:UIClor累方法的使用 就是关于UIColor的使用,记得之前开发中我们使用的都是UIColor后面直接食用类方法获取颜色活着使用 + (UIColor *)colorWithRed:(CGFloat)red green:(CGFloat)green blue:(CGFloat)blue alpha:(CGFloat)alpha;但是突然

iOS开发——图层OC篇&amp;Quartz 2D各种绘制实例

Quartz 2D各种绘制实例 首先说一下,本篇文章只是介绍怎么使用Quartz 2D绘制一些常用的图像效果,关于Quartz和其他相关技术请查看笔者之前写的完整版(Quartz 2D详解) 一:画线 1 // 1.获取跟当前View相关联的layer上下文(画板) 2 // 总结:目前获取的所有上下文都是以UIGraphics开头 3 // CGContextRef:上下文类型 4 // CG:CoreGraphics Ref:引用 5 CGContextRef ctx = UIGraphic

iOS 渐变进度条

#import <UIKit/UIKit.h> @interface JianBianView : UIView //为了增加一个表示进度条的进行,可们可以使用mask属性来屏蔽一部分 @property (nonatomic, strong) CALayer *maskLayer; @property (nonatomic, assign) CGFloat progress; //动画方法 -(void)performAnimation; -(void)setProgress:(CGFloa

swift渐变之CAGradientLayer

CAGradientLayer是渐变实现最后简单的一种方式,以下有效果demo,可以看效果 直接使用渐变 let gradient:CAGradientLayer = CAGradientLayer.init() gradient.frame = CGRect.init(x: 0, y: 0, width: 375, height: 100);//CAGradientLayer的控件大小 gradient.colors = [UIColor.red.cgColor,UIColor.yellow.

ios新建图层

////  MJViewController.m//  02-CALayer02-新建图层////  Created by apple on 14-4-21.//  Copyright (c) 2014年 itcast. All rights reserved.// #import "MJViewController.h" @interface MJViewController () @end @implementation MJViewController - (void)viewD