手機(jī)上的跑馬燈

我們經(jī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)會上, 中國籃球隊(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è)簡單的滾動字體就完事了. 感覺還可以.

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

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

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