iOS滾動label顯示效果

關(guān)于滾動 label 效果,大家應(yīng)該都不陌生,在移動設(shè)備寸土寸金的顯示屏中可以顯示更多的內(nèi)容,今天給大家?guī)硪粋€滾動 label 效果實現(xiàn)的一個框架:SGAnimationLabel;

GitHub:SGAnimationLabel

簡介:支持文字,大小,滾動速度,開始,結(jié)束滾動自定義
talk is cheap,show me code
具體代碼:
使用:

    SGAnimationLabel *dynamicLabel = [[SGAnimationLabel   alloc]initWithFrame:CGRectMake(60, 100, 200, 30)];
    dynamicLabel.text = @"我不想說再見,不說再見,越長大越孤單";
    dynamicLabel.textColor = [UIColor orangeColor];
    dynamicLabel.font = [UIFont systemFontOfSize:24];
    dynamicLabel.speed = 0.2;
    [self.view addSubview:dynamicLabel];
    [dynamicLabel startAnimation];

SGAnimationLabel.h


#import <UIKit/UIKit.h>
@interface SGAnimationLabel : UIView
@property(nonatomic, strong) NSString *text;
@property(nonatomic, strong) UIColor *textColor;
@property(nonatomic, strong) UIFont *font;
@property(nonatomic, assign) CGFloat speed;//0.1~0.5  默認(rèn)是0.3
/*!
 *
 * @abstract start animation
 */
- (void)startAnimation;
/*!
 *
 * @abstract stop animation
 */
- (void)stopAnimation;
@end

SGAnimationLabel.m

#import "SGAnimationLabel.h"
#define kDynamicLabelAnimationKey @"DynamicLabelAnimation"
@interface SGAnimationLabel()
@property (nonatomic ,strong)UILabel *animationLabel;
@end
@implementation SGAnimationLabel
- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.speed = 0.3;
        [self initAnimationLabel];
        [self addNotification];
    }
    return self;
}
- (void)initAnimationLabel{
    _animationLabel = [[UILabel alloc]init];
    _animationLabel.backgroundColor = [UIColor clearColor];
    [self addSubview:_animationLabel];
    CAShapeLayer* maskLayer = [CAShapeLayer layer];
    maskLayer.path = [UIBezierPath bezierPathWithRect:self.bounds].CGPath;
    self.layer.mask = maskLayer;
}
- (void)addNotification{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(startAnimation) name:UIApplicationWillEnterForegroundNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(stopAnimation) name:UIApplicationDidEnterBackgroundNotification object:nil];
}
#pragma mark -
#pragma mark - set mothods
- (void)setText:(NSString *)text{
    self.animationLabel.text = text;
    [self.animationLabel sizeToFit];
}
- (void)setTextColor:(UIColor *)textColor{
    self.animationLabel.textColor = textColor;
}
- (void)setFont:(UIFont *)font{
    self.animationLabel.font = font;
    [self.animationLabel sizeToFit];
    CGRect frame = self.frame;
    if (frame.size.height < font.lineHeight) {
        frame.size.height = font.lineHeight;
        self.frame = frame;
    }
}
#pragma mark -
#pragma mark - start or stop animation
- (void)startAnimation{
    if ([self.animationLabel.layer animationForKey:kDynamicLabelAnimationKey] || self.animationLabel.frame.size.width <= self.frame.size.width) {
        return;
    }
    CGFloat lenth = self.animationLabel.frame.size.width + self.frame.size.width;
    CAKeyframeAnimation* keyFrame = [CAKeyframeAnimation animation];
    keyFrame.keyPath = @"transform.translation.x";
    keyFrame.values = @[@(0), @(-self.animationLabel.frame.size.width)];
    keyFrame.repeatCount = NSIntegerMax;
    keyFrame.duration = lenth * self.speed / 10;
    keyFrame.timingFunctions = @[[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear]];
    [self.animationLabel.layer addAnimation:keyFrame forKey:kDynamicLabelAnimationKey];
}
- (void)stopAnimation{
    if ([self.animationLabel.layer animationForKey:kDynamicLabelAnimationKey]) {
        [self.animationLabel.layer removeAnimationForKey:kDynamicLabelAnimationKey];
    }
}
#pragma mark -
- (void)dealloc{
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
@end

希望大家能夠喜歡...

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末允扇,一起剝皮案震驚了整個濱河市义矛,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌符匾,老刑警劉巖,帶你破解...
    沈念sama閱讀 218,755評論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件荐开,死亡現(xiàn)場離奇詭異,居然都是意外死亡闲礼,警方通過查閱死者的電腦和手機(jī)寡具,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,305評論 3 395
  • 文/潘曉璐 我一進(jìn)店門秤茅,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人童叠,你說我怎么就攤上這事框喳。” “怎么了厦坛?”我有些...
    開封第一講書人閱讀 165,138評論 0 355
  • 文/不壞的土叔 我叫張陵五垮,是天一觀的道長。 經(jīng)常有香客問我杜秸,道長放仗,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,791評論 1 295
  • 正文 為了忘掉前任撬碟,我火速辦了婚禮诞挨,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘呢蛤。我一直安慰自己惶傻,他們只是感情好,可當(dāng)我...
    茶點故事閱讀 67,794評論 6 392
  • 文/花漫 我一把揭開白布其障。 她就那樣靜靜地躺著银室,像睡著了一般。 火紅的嫁衣襯著肌膚如雪励翼。 梳的紋絲不亂的頭發(fā)上蜈敢,一...
    開封第一講書人閱讀 51,631評論 1 305
  • 那天,我揣著相機(jī)與錄音汽抚,去河邊找鬼抓狭。 笑死,一個胖子當(dāng)著我的面吹牛殊橙,可吹牛的內(nèi)容都是我干的辐宾。 我是一名探鬼主播狱从,決...
    沈念sama閱讀 40,362評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼膨蛮,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了季研?” 一聲冷哼從身側(cè)響起敞葛,我...
    開封第一講書人閱讀 39,264評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎与涡,沒想到半個月后惹谐,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體持偏,經(jīng)...
    沈念sama閱讀 45,724評論 1 315
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,900評論 3 336
  • 正文 我和宋清朗相戀三年氨肌,在試婚紗的時候發(fā)現(xiàn)自己被綠了鸿秆。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 40,040評論 1 350
  • 序言:一個原本活蹦亂跳的男人離奇死亡怎囚,死狀恐怖卿叽,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情恳守,我是刑警寧澤考婴,帶...
    沈念sama閱讀 35,742評論 5 346
  • 正文 年R本政府宣布,位于F島的核電站催烘,受9級特大地震影響沥阱,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜伊群,卻給世界環(huán)境...
    茶點故事閱讀 41,364評論 3 330
  • 文/蒙蒙 一考杉、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧舰始,春花似錦奔则、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,944評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至及老,卻和暖如春抽莱,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背骄恶。 一陣腳步聲響...
    開封第一講書人閱讀 33,060評論 1 270
  • 我被黑心中介騙來泰國打工食铐, 沒想到剛下飛機(jī)就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人僧鲁。 一個月前我還...
    沈念sama閱讀 48,247評論 3 371
  • 正文 我出身青樓虐呻,卻偏偏與公主長得像,于是被迫代替她去往敵國和親寞秃。 傳聞我的和親對象是個殘疾皇子斟叼,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,979評論 2 355

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