手机上的跑马灯

我们经常可以在荧屏上看到滚动的字体(可以叫跑马灯)感觉挺炫的, 今天分享一下他的代码实现, 这个一般在手机上的navigationitem的title上用到. 其实就是一个自定义的View.

首先看一下效果,

手机上的跑马灯_第1张图片
111.gif
首先要自定义一个customView
custom.h

#import 
@interface CustomView : UIView
/** 类方法 */
// 参数1: 自定义View的frame
// 参数2: 要显示的字符串
// 参数3: 自定义View的背景色
// 参数4: 字符串的颜色
// 参数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; // 标记开始位置
    CGRect rectMark2; // 标记结束位置

    NSMutableArray *labelArr; // label数组
    NSTimeInterval timeInterval; // 时间
    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;
        // 计算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;
}
// 计算一下时间间隔
- (NSTimeInterval)displayDurationForString:(NSString *)string {

    return string.length / 4;
}
@end

然后在VC中创建使用类方法调用就可以了
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 = @"里约奥运会上, 中国篮球队VS美国队 中国队员虽不敌美国队, 但是学到和强队带球, 学到了不少经验";
    CustomView *customView = [CustomView shareInitWithFrame:CGRectMake(0, 0, 200, 40) title:str backColor:[UIColor whiteColor] textColor:[UIColor blackColor] textFont:17];
    self.navigationItem.titleView = customView;
}

这样一个简单的滚动字体就完事了. 感觉还可以.

你可能感兴趣的:(手机上的跑马灯)