UILabel 跑馬燈效果與漸變色

直接貼代碼和效果

跑馬燈

效果:

跑馬燈效果.gif

.h 文件

#import <UIKit/UIKit.h>

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

/**
 animation 實現(xiàn)跑馬燈樣式 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;

/** 滾動方向,默認(rèn) SAMarqueeLabelLeft */
@property (nonatomic, assign) SAMarqueeLabelDirection marqueeDirection;

/** 滾動速度,默認(rèn)30 */
@property (nonatomic, assign) CGFloat scrollSpeed;

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

@end

.m 文件

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

//默認(rèn)滾動速度
#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];
    
    //程序進(jìn)入前臺繼續(xù)滾動
    [[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);
    
    // 創(chuàng)建漸變層
    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;
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

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

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 178,741評論 25 709
  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 15,041評論 4 61
  • ¥開啟¥ 【iAPP實現(xiàn)進(jìn)入界面執(zhí)行逐一顯】 〖2017-08-25 15:22:14〗 《//首先開一個線程,因...
    小菜c閱讀 7,295評論 0 17
  • 一. 色情凝視 繼父,母親,弟弟,17歲的女孩伊莎貝拉,一家人在異國渡假。異國沙灘上,同為游客的德國男孩,在搭訕...
    跛足游魚閱讀 5,237評論 0 0
  • 生活中的我可以過的很粗糙,同時也可以很細(xì)膩,主要看自己,看心情,看環(huán)境,看天氣,好吧,一句話就是任性!早上抓住最后...
    熤妞閱讀 295評論 0 0

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