2018-08-17 iOS代碼實(shí)現(xiàn)顏色漸變

轉(zhuǎn)載:https://www.cnblogs.com/YouXianMing/p/3793913.html

CAGradientLayer的一些屬性解析

iOS中Layer的坐標(biāo)系統(tǒng):

效果:

- (void)viewDidLoad

{

? ? [super viewDidLoad];

? ? CAGradientLayer *colorLayer = [CAGradientLayer layer];

? ? colorLayer.frame? ? = (CGRect){CGPointZero, CGSizeMake(200,200)};

? ? colorLayer.position = self.view.center;

? ? [self.view.layer addSublayer:colorLayer];

? ? // 顏色分配colorLayer.colors = @[(__bridgeid)[UIColor redColor].CGColor,

? ? ? ? ? ? ? ? ? ? ? ? ? (__bridge id)[UIColor greenColor].CGColor,

? ? ? ? ? ? ? ? ? ? ? ? ? (__bridge id)[UIColor blueColor].CGColor];


? ? // 顏色分割線colorLayer.locations? = @[@(0.25), @(0.5), @(0.75)];


? ? // 起始點(diǎn)colorLayer.startPoint = CGPointMake(0,0);


? ? // 結(jié)束點(diǎn)colorLayer.endPoint? = CGPointMake(1,0);

}

顏色分配嚴(yán)格遵守Layer的坐標(biāo)系統(tǒng),locations,startPoint,endPoint都是以Layer坐標(biāo)系統(tǒng)進(jìn)行計(jì)算的.

而locations并不是表示顏色值所在位置,它表示的是顏色在Layer坐標(biāo)系相對(duì)位置處要開始進(jìn)行漸變顏色了.

CAGradientLayer 的這四個(gè)屬性?colors locations startPoint endPoint?都是可以進(jìn)行動(dòng)畫的哦.


附錄:

稍微復(fù)雜點(diǎn)的動(dòng)畫效果

////? RootViewController.m////? Copyright (c) 2014年 Y.X. All rights reserved.//#import"RootViewController.h"#import"YXGCD.h"@interface RootViewController ()

@property (nonatomic, strong) GCDTimer? *timer;@end@implementation RootViewController- (void)viewDidLoad

{

? ? [super viewDidLoad];

? ? CAGradientLayer *colorLayer = [CAGradientLayer layer];

? ? colorLayer.backgroundColor = [UIColor blueColor].CGColor;

? ? colorLayer.frame? ? = (CGRect){CGPointZero, CGSizeMake(200,200)};

? ? colorLayer.position = self.view.center;

? ? [self.view.layer addSublayer:colorLayer];

? ? // 顏色分配colorLayer.colors = @[(__bridgeid)[UIColor cyanColor].CGColor,

? ? ? ? ? ? ? ? ? ? ? ? ? (__bridge id)[UIColor orangeColor].CGColor,

? ? ? ? ? ? ? ? ? ? ? ? ? (__bridge id)[UIColor magentaColor].CGColor];


? ? // 起始點(diǎn)colorLayer.startPoint = CGPointMake(0,0);


? ? // 結(jié)束點(diǎn)colorLayer.endPoint? = CGPointMake(1,0);


? ? _timer = [[GCDTimer alloc] initInQueue:[GCDQueue mainQueue]];

? ? [_timer event:^{


? ? ? ? staticCGFloat test = -0.1f;


? ? ? ? if(test >=1.1)

? ? ? ? {

? ? ? ? ? ? test = -0.1f;

? ? ? ? ? ? [CATransaction setDisableActions:YES];

? ? ? ? ? ? colorLayer.locations? = @[@(test), @(test +0.05), @(test +0.1)];

? ? ? ? }

? ? ? ? else? ? ? ? {

? ? ? ? ? ? [CATransaction setDisableActions:NO];

? ? ? ? ? ? colorLayer.locations? = @[@(test), @(test +0.05), @(test +0.1)];

? ? ? ? }


? ? ? ? test +=0.1f;


? ? } timeInterval:NSEC_PER_SEC];

? ? [_timer start];

}@end

_timer = [[GCDTimer alloc] initInQueue:[GCDQueue mainQueue]];

? ? [_timer event:^{


? ? ? ? staticCGFloat test = -0.1f;


? ? ? ? if(test >=1.1)

? ? ? ? {

? ? ? ? ? ? test = -0.1f;

? ? ? ? ? ? [CATransaction setDisableActions:NO];

? ? ? ? ? ? colorLayer.locations? = @[@(test), @(test +0.01), @(test +0.011)];

? ? ? ? }

? ? ? ? else? ? ? ? {

? ? ? ? ? ? [CATransaction setDisableActions:NO];

? ? ? ? ? ? colorLayer.locations? = @[@(test), @(test +0.01), @(test +0.011)];

? ? ? ? }


? ? ? ? test +=0.1f;


? ? } timeInterval:NSEC_PER_SEC];

? ? [_timer start];

配合CAShapeLayer使用

////? RootViewController.m////? Copyright (c) 2014年 Y.X. All rights reserved.//#import"RootViewController.h"#import"YXGCD.h"@interface RootViewController ()

@property (nonatomic, strong) GCDTimer? *timer;@end// 將常數(shù)轉(zhuǎn)換為度數(shù)#defineDEGREES(degrees)? ((M_PI * (degrees))/ 180.f)@implementation RootViewController- (void)viewDidLoad

{

? ? [super viewDidLoad];

? ? self.view.backgroundColor = [UIColor blackColor];

? ? CAGradientLayer *colorLayer = [CAGradientLayer layer];

? ? colorLayer.backgroundColor = [UIColor blueColor].CGColor;

? ? colorLayer.frame? ? = (CGRect){CGPointZero, CGSizeMake(200,200)};

? ? colorLayer.position = self.view.center;

? ? [self.view.layer addSublayer:colorLayer];

? ? // 顏色分配colorLayer.colors = @[(__bridgeid)[UIColor redColor].CGColor,

? ? ? ? ? ? ? ? ? ? ? ? ? (__bridge id)[UIColor whiteColor].CGColor,

? ? ? ? ? ? ? ? ? ? ? ? ? (__bridge id)[UIColor redColor].CGColor];

? ? colorLayer.locations? = @[@(-0.2), @(-0.1), @(0)];


? ? // 起始點(diǎn)colorLayer.startPoint = CGPointMake(0,0);


? ? // 結(jié)束點(diǎn)colorLayer.endPoint? = CGPointMake(1,0);


? ? CAShapeLayer *circle = [RootViewController LayerWithCircleCenter:CGPointMake(102,100)

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? radius:80? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? startAngle:DEGREES(0)

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? endAngle:DEGREES(360)

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? clockwise:YES

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? lineDashPattern:nil];

? ? circle.strokeColor = [UIColor redColor].CGColor;

? ? [self.view.layer addSublayer:circle];

? ? circle.strokeEnd =1.f;

? ? colorLayer.mask = circle;


? ? _timer = [[GCDTimer alloc] initInQueue:[GCDQueue mainQueue]];

? ? [_timer event:^{

? ? ? ? staticinti =0;

? ? ? ? if(i++ %2==0)

? ? ? ? {

? ? ? ? ? ? CABasicAnimation* fadeAnim = [CABasicAnimation animationWithKeyPath:@"locations"];

? ? ? ? ? ? fadeAnim.fromValue = @[@(-0.2), @(-0.1), @(0)];

? ? ? ? ? ? fadeAnim.toValue? = @[@(1.0), @(1.1), @(1.2)];

? ? ? ? ? ? fadeAnim.duration? =1.5;

? ? ? ? ? ? [colorLayer addAnimation:fadeAnim forKey:nil];

? ? ? ? }

? ? } timeInterval:NSEC_PER_SEC];

? ? [_timer start];

}+ (CAShapeLayer *)LayerWithCircleCenter:(CGPoint)point

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? radius:(CGFloat)radius

? ? ? ? ? ? ? ? ? ? ? ? ? ? startAngle:(CGFloat)startAngle

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? endAngle:(CGFloat)endAngle

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? clockwise:(BOOL)clockwise

? ? ? ? ? ? ? ? ? ? ? ? lineDashPattern:(NSArray *)lineDashPattern

{

? ? CAShapeLayer *layer = [CAShapeLayer layer];


? ? // 貝塞爾曲線(創(chuàng)建一個(gè)圓)UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(0,0)

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? radius:radius

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? startAngle:startAngle

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? endAngle:endAngle

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? clockwise:clockwise];


? ? // 獲取pathlayer.path = path.CGPath;

? ? layer.position = point;


? ? // 設(shè)置填充顏色為透明layer.fillColor = [UIColor clearColor].CGColor;


? ? // 獲取曲線分段的方式if (lineDashPattern)

? ? {

? ? ? ? layer.lineDashPattern = lineDashPattern;

? ? }


? ? return layer;

}@end

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容