iOS UILabel 跑马灯效果与渐变色

热门标签

, ,

特别声明:文章多为网络转载,资源使用一般不提供任何帮助,特殊资源除外,如有侵权请联系!

直接贴代码和效果

跑马灯

效果:

iOS 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

渐变色

iOS UILabel 跑马灯效果与渐变色
渐变色.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;

未经允许不得转载:作者:SheaYang, 转载或复制请以 超链接形式 并注明出处 技术Dog|博客
原文地址:《iOS UILabel 跑马灯效果与渐变色》 发布于2019-10-12

分享到:
赞(0)

评论 抢沙发

2 + 2 =


iOS UILabel 跑马灯效果与渐变色

长按图片转发给朋友

Vieu4.0主题
专业打造轻量级个人企业风格博客主题!专注于前端开发,全站响应式布局自适应模板。

登录

忘记密码 ?

您也可以使用第三方帐号快捷登录

Q Q 登 录
微 博 登 录