iOS渐变图层CAGradientLayer

转载自:http://www.tuicool.com/articles/FBfieu


看支付宝蚂蚁积分,天气预报等好多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.效果


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值