美文网首页
渐变色Label

渐变色Label

作者: 脚下的斑马线 | 来源:发表于2018-08-16 19:50 被阅读26次

利用CAGradientLayer和CAKeyframeAnimation去封装一个Label,使用时可根据自己的项目调整。

先上图:


QQ20180816-192224-HD.gif
#import <UIKit/UIKit.h>

@interface GradientLabel : UILabel

/**
 示例:
 self.colors = @[].mutableCopy;
 [self.colors addObject:@[(__bridge id)[[UIColor yellowColor] CGColor], (__bridge id)[[UIColor cyanColor] CGColor],]];
 [self.colors addObject:@[(__bridge id)[[UIColor orangeColor] CGColor], (__bridge id)[[UIColor greenColor] CGColor],]];
 [self.colors addObject:@[(__bridge id)[[UIColor whiteColor] CGColor], (__bridge id)[[UIColor blueColor] CGColor],]];
 [self.colors addObject:@[(__bridge id)[[UIColor brownColor] CGColor], (__bridge id)[[UIColor magentaColor] CGColor],]];
 [self.colors addObject:@[(__bridge id)[[UIColor redColor] CGColor], (__bridge id)[[UIColor lightGrayColor] CGColor],]];
 [self.colors addObject:@[(__bridge id)[[UIColor darkGrayColor] CGColor], (__bridge id)[[UIColor cyanColor] CGColor],]];
 */
@property (nonatomic, strong) NSArray * colors;

/**
 示例:@[@(0.0), @(0.2), @(0.5), @(0.7), @(0.9), @(1.0)];
 */
@property (nonatomic, strong) NSArray * keyTimes;

@property (nonatomic, assign) NSTimeInterval animationDuration;

/**
 是否重复,默认是NO
 */
@property (nonatomic, assign) BOOL isRepeat;

@end
#import "GradientLabel.h"

@interface GradientLabel () <CAAnimationDelegate>
{
    CAKeyframeAnimation * animation;
}
@property (nonatomic, strong) UILabel * label;

@property (nonatomic, strong) CAGradientLayer * gradientLayer;

@end

@implementation GradientLabel

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

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self createContent];
    }
    return self;
}

- (void)createContent {
    self.animationDuration = 20.0;

    self.label = [[UILabel alloc]initWithFrame:CGRectZero];
    [self addSubview:self.label];
    
    self.gradientLayer = [CAGradientLayer layer];
    [self.layer addSublayer:self.gradientLayer];
    self.gradientLayer.colors = @[(__bridge id)[[UIColor yellowColor] CGColor], (__bridge id)[[UIColor cyanColor] CGColor]];
    self.gradientLayer.startPoint = CGPointMake(0.0, 0.0);
    self.gradientLayer.endPoint = CGPointMake(1.0, 1.0);
    self.gradientLayer.name = @"gradientLayer";
    self.gradientLayer.mask = self.label.layer;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    self.gradientLayer.frame = self.bounds;
    self.label.frame = self.gradientLayer.bounds;
    [self gradientLayerAddAnimation];
}

- (void)gradientLayerAddAnimation {
    animation = [CAKeyframeAnimation animation];
    animation.keyPath = @"colors";
    animation.duration = self.animationDuration;
    animation.values = self.colors;
    animation.keyTimes = self.keyTimes;
    animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    animation.delegate = self;
    animation.removedOnCompletion = NO;
    animation.fillMode = kCAFillModeForwards;
    [self.gradientLayer addAnimation:animation forKey:@"gradientLayerAddAnimation"];
}
- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag {
    CAAnimation *animation = [self.gradientLayer animationForKey:@"gradientLayerAddAnimation"];
    if ([anim isEqual:animation]) {
        [self.gradientLayer removeAnimationForKey:@"gradientLayerAddAnimation"];
    }
    
    if (flag && self.isRepeat) {
        [self gradientLayerAddAnimation];
    }
}

- (void)setText:(NSString *)text {
    self.label.text = text;
}
- (void)setTextAlignment:(NSTextAlignment)textAlignment {
    self.label.textAlignment = textAlignment;
}
- (void)setFont:(UIFont *)font {
    self.label.font = font;
}
- (void)setNumberOfLines:(NSInteger)numberOfLines {
    self.label.numberOfLines = numberOfLines;
}
@end

相关文章

网友评论

      本文标题:渐变色Label

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