码迷,mamicode.com
首页 > 移动开发 > 详细

IOS渐变图层CAGradientLayer

时间:2015-10-26 15:21:46      阅读:286      评论:0      收藏:0      [点我收藏+]

标签:

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

一.CAGradientLayer类中属性介绍

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

1.@property(nullable, copy) NSArray *colors; 渐变的颜色

2.@property(nullable, copy) NSArray<NSNumber *> *locations;每种颜色的最亮的位置

3.@property CGPoint startPoint;  @property CGPoint endPoint; 渐变的方向 左上(0,0)  右下(1,1) startPoint——>endPoint

4.@property(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.效果

技术分享

 

IOS渐变图层CAGradientLayer

标签:

原文地址:http://www.cnblogs.com/5ishare/p/4911188.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!