UILabel 跑马灯效果与渐变色

直接贴代码和效果

跑马灯

效果:

跑马灯效果.gif

.h 文件

#import <UIKit/UIKit.h>

/**
 文字滚动方向
 
 - SALabelScrollLeft: 从右向左
 - SALabelScrollRight: 从左向右
 */
typedef NS_ENUM(NSInteger, SAMarqueeLabelDirection) {
    SAMarqueeLabelLeft,
    SAMarqueeLabelRight
};

/**
 animation 实现跑马灯样式 label
 */
@interface SAMarqueeLabel : UIView

/** 文字 */
@property (nonatomic, copy, nullable) NSString *text;

/** 富文本 */
@property (nonatomic, copy, nullable) NSAttributedString *attributedText;

/** 文字颜色*/
@property (nonatomic, strong, nonnull) UIColor *textColor;

/** 文字font */
@property (nonatomic, strong, nonnull) UIFont *font;

/** 文字阴影颜色 */
@property (nonatomic, strong, nullable) UIColor *shandowColor;

/** 文字阴影偏移 */
@property (nonatomic, assign) CGSize shandowOffset;

/** 文字位置,只在文字不滚动时有效 */
@property (nonatomic, assign) NSTextAlignment textAlignment;

/** 滚动方向,默认 SAMarqueeLabelLeft */
@property (nonatomic, assign) SAMarqueeLabelDirection marqueeDirection;

/** 滚动速度,默认30 */
@property (nonatomic, assign) CGFloat scrollSpeed;

/** 是否可以滚动 */
@property (nonatomic, readonly, assign) BOOL isScroll;

@end

.m 文件

#import "SAMarqueeLabel.h"
#import <Masonry/Masonry.h>

//默认滚动速度
#define kDefaultScrollSpeed 30

@interface SAMarqueeLabel ()<CAAnimationDelegate>

/** 用于显示文字 */
@property (nonatomic, strong) UILabel *label;

@end

@implementation SAMarqueeLabel
#pragma mark -
#pragma mark - View Life Cycle
- (instancetype)initWithCoder:(NSCoder *)coder
{
    self = [super initWithCoder:coder];
    if (self) {
        [self setupInit];
    }
    return self;
}

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

- (void)dealloc {
    [NSObject cancelPreviousPerformRequestsWithTarget:self];
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

#pragma mark -
#pragma mark - Private Method
- (void)setupInit {
    [self addSubview:self.label];
    [self.label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.left.mas_equalTo(self);
    }];
    self.clipsToBounds = YES;
    [self observeApplicationNotifications];
    self.scrollSpeed = kDefaultScrollSpeed;
    self.marqueeDirection = SAMarqueeLabelLeft;
}

- (void)startAnimation {
    
    if (self.isScroll) {
        [self.label.layer removeAnimationForKey:@"animationViewPosition"];
        
        CGPoint pointRightCenter = CGPointMake(self.bounds.size.width + self.label.bounds.size.width/2, self.bounds.size.height/ 2.f);
        CGPoint pointLeftCenter  = CGPointMake(-self.label.bounds.size.width/ 2, self.bounds.size.height / 2.f);
        CGPoint fromPoint        = self.marqueeDirection == SAMarqueeLabelLeft ? pointRightCenter : pointLeftCenter;
        CGPoint toPoint          = self.marqueeDirection == SAMarqueeLabelLeft ? pointLeftCenter  : pointRightCenter;
        
        self.label.center = fromPoint;
        UIBezierPath *movePath    = [UIBezierPath bezierPath];
        [movePath moveToPoint:fromPoint];
        [movePath addLineToPoint:toPoint];
        
        CAKeyframeAnimation *moveAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
        moveAnimation.path                 = movePath.CGPath;
        moveAnimation.removedOnCompletion  = YES;
        moveAnimation.duration             = (self.label.bounds.size.width + self.bounds.size.width) / self.scrollSpeed;
        moveAnimation.delegate             = self;
        [self.label.layer addAnimation:moveAnimation forKey:@"animationViewPosition"];
    }
}

- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag {
    if (flag) {
        [self startAnimation];
    }
}

#pragma mark-
#pragma mark- Event Response
- (void)observeApplicationNotifications {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    
    //程序进入前台继续滚动
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(startAnimation)
                                                 name:UIApplicationWillEnterForegroundNotification
                                               object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(startAnimation)
                                                 name:UIApplicationDidBecomeActiveNotification
                                               object:nil];
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    if (self.label.bounds.size.width > self.bounds.size.width) {
        [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(startAnimation) object:nil];
        _isScroll = YES;
        [self startAnimation];
    }else {
        self.label.frame = self.bounds;
        _isScroll = NO;
    }
}

#pragma mark-
#pragma mark- Getters && Setters 
- (void)setText:(NSString *)text {
    if (![_text isEqualToString:text]) {
        _text = text;
        self.label.text = text;
    }
}

- (void)setFont:(UIFont *)font {
    if (_font != font) {
        _font = font;
        self.label.font = font;
    }
}

- (void)setAttributedText:(NSAttributedString *)attributedText {
    if (![_attributedText.string isEqualToString:attributedText.string]) {
        _attributedText = attributedText;
        self.label.attributedText = attributedText;
    }
}

- (void)setTextColor:(UIColor *)textColor {
    if (_textColor != textColor) {
        _textColor = textColor;
        self.label.textColor = textColor;
    }
    
}

- (void)setShandowColor:(UIColor *)shandowColor {
    if (_shandowColor != shandowColor) {
        _shandowColor = shandowColor;
        self.label.shadowColor = shandowColor;
    }
}

- (void)setShandowOffset:(CGSize)shandowOffset {
    _shandowOffset = shandowOffset;
    self.label.shadowOffset = shandowOffset;
}

- (void)setTextAlignment:(NSTextAlignment)textAlignment {
    _textAlignment = textAlignment;
    self.label.textAlignment = textAlignment;
}

- (void)setMarqueeDirection:(SAMarqueeLabelDirection)marqueeDirection {
    _marqueeDirection = marqueeDirection;
    [self setNeedsLayout];
}

- (UILabel *)label {
    if (!_label) {
        _label = [[UILabel alloc] initWithFrame:self.bounds];
        _label.backgroundColor = [UIColor clearColor];
        _label.textAlignment = NSTextAlignmentLeft;
    }
    return _label;
}

@end

渐变色

渐变色.png
    NSArray *colors = @[(id)[UIColor redColor].CGColor, (id)[UIColor greenColor].CGColor];
    UILabel* testLabel = [[UILabel alloc] init];
    testLabel.text = @"我是渐变色的呀呀呀呀--layer";
    testLabel.font = [UIFont systemFontOfSize:23];
    [testLabel sizeToFit];
    
    [self.view addSubview:testLabel];
    testLabel.center = CGPointMake(self.view.bounds.size.width * 0.5, self.view.bounds.size.height * 0.7);
    
    // 创建渐变层
    CAGradientLayer* gradientLayer = [CAGradientLayer layer];
    gradientLayer.frame = testLabel.frame;
    gradientLayer.colors = colors;
    gradientLayer.startPoint = CGPointMake(0, 1);
    gradientLayer.endPoint = CGPointMake(1, 1);
    [self.view.layer addSublayer:gradientLayer];
    
    gradientLayer.mask = testLabel.layer;
    testLabel.frame = gradientLayer.bounds;
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,020评论 25 708
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,198评论 4 61
  • ¥开启¥ 【iAPP实现进入界面执行逐一显】 〖2017-08-25 15:22:14〗 《//首先开一个线程,因...
    小菜c阅读 6,508评论 0 17
  • 一. 色情凝视 继父,母亲,弟弟,17岁的女孩伊莎贝拉,一家人在异国渡假。异国沙滩上,同为游客的德国男孩,在搭讪...
    跛足游魚阅读 5,075评论 0 0
  • 生活中的我可以过的很粗糙,同时也可以很细腻,主要看自己,看心情,看环境,看天气,好吧,一句话就是任性!早上抓住最后...
    熤妞阅读 271评论 0 0