手機(jī)上的跑馬燈

我們經(jīng)常可以在熒屏上看到滾動(dòng)的字體(可以叫跑馬燈)感覺挺炫的, 今天分享一下他的代碼實(shí)現(xiàn), 這個(gè)一般在手機(jī)上的navigationitem的title上用到. 其實(shí)就是一個(gè)自定義的View.

首先看一下效果,

111.gif
首先要自定義一個(gè)customView
custom.h

#import <UIKit/UIKit.h>
@interface CustomView : UIView
/** 類方法 */
// 參數(shù)1: 自定義View的frame
// 參數(shù)2: 要顯示的字符串
// 參數(shù)3: 自定義View的背景色
// 參數(shù)4: 字符串的顏色
// 參數(shù)5: 字符串的大小
+ (CustomView *)shareInitWithFrame:(CGRect)frame title:(NSString *)title backColor:(UIColor *)backColor textColor:(UIColor *)textColor textFont:(CGFloat)textFont;
/** init */
- (instancetype)initWithFrame:(CGRect)frame title:(NSString *)title backColor:(UIColor *)backColor textColor:(UIColor *)textColor textFont:(CGFloat)textFont;
/** 開始 */
- (void)start:(UIColor *)color;
/** 停止 */
- (void)stop;
@end

custom.m

#import "CustomView.h"

@implementation CustomView
{
    CGRect rectMark1; // 標(biāo)記開始位置
    CGRect rectMark2; // 標(biāo)記結(jié)束位置

    NSMutableArray *labelArr; // label數(shù)組
    NSTimeInterval timeInterval; // 時(shí)間
    BOOL isStop; // 是否停止
}

+ (CustomView *)shareInitWithFrame:(CGRect)frame title:(NSString *)title backColor:(UIColor *)backColor textColor:(UIColor *)textColor textFont:(CGFloat)textFont {

    static CustomView *customView = nil;

    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        customView = [[CustomView alloc] initWithFrame:frame title:title backColor:backColor textColor:textColor textFont:textFont];
    });
    return customView;
}

- (instancetype)initWithFrame:(CGRect)frame title:(NSString *)title backColor:(UIColor *)backColor textColor:(UIColor *)textColor textFont:(CGFloat)textFont {

    self = [super initWithFrame:frame];
    if (self) {
      // 中間間隔
        title = [NSString stringWithFormat:@"  %@  ", title];
        timeInterval = [self displayDurationForString:title];
        self.backgroundColor = backColor;
        self.clipsToBounds = YES;
        
        UILabel *textLabel = [[UILabel alloc] initWithFrame:CGRectZero];
        textLabel.textColor = [UIColor clearColor];
        textLabel.font = [UIFont boldSystemFontOfSize:textFont];
        textLabel.text = title;
        // 計(jì)算textLabel大小
        CGSize sizeOftext = [textLabel sizeThatFits:CGSizeZero];
        rectMark1 = CGRectMake(0, 0, sizeOftext.width, self.bounds.size.height);
        rectMark2 = CGRectMake(rectMark1.origin.x + rectMark1.size.width, 0, sizeOftext.width, self.bounds.size.height);
        
        textLabel.frame = rectMark1;
        [self addSubview:textLabel];
        
        labelArr = [NSMutableArray arrayWithObject:textLabel];
        
        // 判斷是否需要reserveTextLabel
        BOOL useReserve = sizeOftext.width > frame.size.width ? YES : NO;
        
        if (useReserve) {
            
            UILabel *reserveTextLabel = [[UILabel alloc] initWithFrame:rectMark2];
            reserveTextLabel.textColor = textColor;
            reserveTextLabel.font = [UIFont boldSystemFontOfSize:textFont];
            reserveTextLabel.text = title;
            [self addSubview:reserveTextLabel];
            
            [labelArr addObject:reserveTextLabel];
            
            [self customAnimate:textColor];
        }
    }
    return self;
}

- (void)customAnimate:(UIColor *)color {

    if (!isStop) {
        
        UILabel *labelIndex0 = labelArr[0];
        UILabel *labelIndex1 = labelArr[1];
        
        [UIView transitionWithView:self duration:timeInterval options:UIViewAnimationOptionCurveLinear animations:^{
            
            labelIndex0.frame = CGRectMake(-rectMark1.size.width, 0, rectMark1.size.width, rectMark1.size.height);
            labelIndex0.textColor = color;
            labelIndex1.frame = CGRectMake(labelIndex0.frame.origin.x + labelIndex0.frame.size.width, 0, labelIndex1.frame.size.width, labelIndex1.frame.size.height);
            
        } completion:^(BOOL finished) {
            
            labelIndex0.frame = rectMark2;
            labelIndex1.frame = rectMark1;
            
            [labelArr replaceObjectAtIndex:0 withObject:labelIndex1];
            [labelArr replaceObjectAtIndex:1 withObject:labelIndex0];
            
            [self customAnimate:color];
        }];
    }
}
- (void)start:(UIColor *)color {

    isStop = NO;
    UILabel *labelIndex0 = labelArr[0];
    UILabel *labelIndex1 = labelArr[1];
    
    labelIndex0.frame = rectMark2;
    labelIndex1.frame = rectMark1;
    
    [labelArr replaceObjectAtIndex:0 withObject:labelIndex1];
    [labelArr replaceObjectAtIndex:1 withObject:labelIndex0];
    
    [self customAnimate:color];
}
- (void)stop {

    isStop = NO;
}
// 計(jì)算一下時(shí)間間隔
- (NSTimeInterval)displayDurationForString:(NSString *)string {

    return string.length / 4;
}
@end

然后在VC中創(chuàng)建使用類方法調(diào)用就可以了
VC.m

#import "ViewController.h"
#import "CustomView.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    self.navigationController.navigationBar.backgroundColor = [UIColor whiteColor];
    NSString *str = @"里約奧運(yùn)會(huì)上, 中國籃球隊(duì)VS美國隊(duì) 中國隊(duì)員雖不敵美國隊(duì), 但是學(xué)到和強(qiáng)隊(duì)帶球, 學(xué)到了不少經(jīng)驗(yàn)";
    CustomView *customView = [CustomView shareInitWithFrame:CGRectMake(0, 0, 200, 40) title:str backColor:[UIColor whiteColor] textColor:[UIColor blackColor] textFont:17];
    self.navigationItem.titleView = customView;
}

這樣一個(gè)簡(jiǎn)單的滾動(dòng)字體就完事了. 感覺還可以.

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 227,488評(píng)論 6 531
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 98,034評(píng)論 3 414
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 175,327評(píng)論 0 373
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經(jīng)常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 62,554評(píng)論 1 307
  • 正文 為了忘掉前任,我火速辦了婚禮,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 71,337評(píng)論 6 404
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 54,883評(píng)論 1 321
  • 那天,我揣著相機(jī)與錄音,去河邊找鬼。 笑死,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 42,975評(píng)論 3 439
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 42,114評(píng)論 0 286
  • 序言:老撾萬榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 48,625評(píng)論 1 332
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 40,555評(píng)論 3 354
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 42,737評(píng)論 1 369
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,244評(píng)論 5 355
  • 正文 年R本政府宣布,位于F島的核電站,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 43,973評(píng)論 3 345
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,362評(píng)論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 35,615評(píng)論 1 280
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 51,343評(píng)論 3 390
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 47,699評(píng)論 2 370

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

  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 12,147評(píng)論 4 61
  • 6月早睡早起又一周開營了,這次主動(dòng)申請(qǐng)擔(dān)任了小組長的角色,關(guān)于小組的基本的規(guī)則流程都正常的走下來了,可能是跟大多數(shù)...
    招財(cái)大龍貓閱讀 241評(píng)論 0 0
  • :關(guān)按芬 公司:慧友冠源科技有限公司,,,, 【日精進(jìn)打卡第110天】 【知~學(xué)習(xí)】 早晨誦讀: 《六項(xiàng)精進(jìn)》大綱...
    江陽水閱讀 196評(píng)論 0 0
  • 愿你早日遇見, 那個(gè)愿意讀你整個(gè)人生的人。 請(qǐng)相信, 只要我一息尚存你便是不忘的夢(mèng)。
    影子shadow520閱讀 123評(píng)論 0 1
  • 女人,對(duì)于時(shí)尚的我來說,不癡迷于化妝品,不著迷于名牌包, 最近,我對(duì)某件事著了魔道,甚至達(dá)到癡迷,迷戀的程度,那就...
    時(shí)尚女人蘭閱讀 254評(píng)論 0 1