UILabel 跑馬燈效果與漸變色

直接貼代碼和效果

跑馬燈

效果:

跑馬燈效果.gif

.h 文件

#import <UIKit/UIKit.h>

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

/**
 animation 實(shí)現(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;

/** 文字位置,只在文字不滾動(dòng)時(shí)有效 */
@property (nonatomic, assign) NSTextAlignment textAlignment;

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

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

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

@end

.m 文件

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

//默認(rèn)滾動(dòng)速度
#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)入前臺(tái)繼續(xù)滾動(dòng)
    [[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)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末起惕,一起剝皮案震驚了整個(gè)濱河市挺尿,隨后出現(xiàn)的幾起案子壶硅,更是在濱河造成了極大的恐慌梨树,老刑警劉巖葬荷,帶你破解...
    沈念sama閱讀 221,576評(píng)論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件涨共,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡宠漩,警方通過(guò)查閱死者的電腦和手機(jī)举反,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,515評(píng)論 3 399
  • 文/潘曉璐 我一進(jìn)店門(mén),熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)扒吁,“玉大人火鼻,你說(shuō)我怎么就攤上這事。” “怎么了魁索?”我有些...
    開(kāi)封第一講書(shū)人閱讀 168,017評(píng)論 0 360
  • 文/不壞的土叔 我叫張陵融撞,是天一觀的道長(zhǎng)。 經(jīng)常有香客問(wèn)我粗蔚,道長(zhǎng)尝偎,這世上最難降的妖魔是什么? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 59,626評(píng)論 1 296
  • 正文 為了忘掉前任鹏控,我火速辦了婚禮致扯,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘当辐。我一直安慰自己抖僵,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,625評(píng)論 6 397
  • 文/花漫 我一把揭開(kāi)白布缘揪。 她就那樣靜靜地躺著裆针,像睡著了一般。 火紅的嫁衣襯著肌膚如雪寺晌。 梳的紋絲不亂的頭發(fā)上世吨,一...
    開(kāi)封第一講書(shū)人閱讀 52,255評(píng)論 1 308
  • 那天,我揣著相機(jī)與錄音呻征,去河邊找鬼耘婚。 笑死,一個(gè)胖子當(dāng)著我的面吹牛陆赋,可吹牛的內(nèi)容都是我干的沐祷。 我是一名探鬼主播,決...
    沈念sama閱讀 40,825評(píng)論 3 421
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼攒岛,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼赖临!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起灾锯,我...
    開(kāi)封第一講書(shū)人閱讀 39,729評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤兢榨,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后顺饮,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體吵聪,經(jīng)...
    沈念sama閱讀 46,271評(píng)論 1 320
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,363評(píng)論 3 340
  • 正文 我和宋清朗相戀三年兼雄,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了吟逝。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,498評(píng)論 1 352
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡赦肋,死狀恐怖块攒,靈堂內(nèi)的尸體忽然破棺而出励稳,到底是詐尸還是另有隱情,我是刑警寧澤囱井,帶...
    沈念sama閱讀 36,183評(píng)論 5 350
  • 正文 年R本政府宣布麦锯,位于F島的核電站,受9級(jí)特大地震影響琅绅,放射性物質(zhì)發(fā)生泄漏扶欣。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,867評(píng)論 3 333
  • 文/蒙蒙 一千扶、第九天 我趴在偏房一處隱蔽的房頂上張望料祠。 院中可真熱鬧,春花似錦澎羞、人聲如沸髓绽。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 32,338評(píng)論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)顺呕。三九已至,卻和暖如春括饶,著一層夾襖步出監(jiān)牢的瞬間株茶,已是汗流浹背。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 33,458評(píng)論 1 272
  • 我被黑心中介騙來(lái)泰國(guó)打工图焰, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留启盛,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,906評(píng)論 3 376
  • 正文 我出身青樓技羔,卻偏偏與公主長(zhǎng)得像僵闯,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子藤滥,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,507評(píng)論 2 359

推薦閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 172,283評(píng)論 25 707
  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫(kù)鳖粟、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 12,119評(píng)論 4 61
  • ¥開(kāi)啟¥ 【iAPP實(shí)現(xiàn)進(jìn)入界面執(zhí)行逐一顯】 〖2017-08-25 15:22:14〗 《//首先開(kāi)一個(gè)線程拙绊,因...
    小菜c閱讀 6,444評(píng)論 0 17
  • 一. 色情凝視 繼父,母親谨娜,弟弟,17歲的女孩伊莎貝拉磺陡,一家人在異國(guó)渡假趴梢。異國(guó)沙灘上漠畜,同為游客的德國(guó)男孩,在搭訕...
    跛足游魚(yú)閱讀 4,991評(píng)論 0 0
  • 生活中的我可以過(guò)的很粗糙坞靶,同時(shí)也可以很細(xì)膩憔狞,主要看自己,看心情彰阴,看環(huán)境瘾敢,看天氣,好吧尿这,一句話就是任性簇抵!早上抓住最后...
    熤妞閱讀 268評(píng)論 0 0