美文网首页
手机上的跑马灯

手机上的跑马灯

作者: Parkour皇 | 来源:发表于2016-08-07 22:56 被阅读230次

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

首先看一下效果,

111.gif 首先要自定义一个customView
custom.h
#import <UIKit/UIKit.h>
@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;
}

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

相关文章

  • 手机上的跑马灯

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

  • GitHub上受欢迎的Android UI Library(下)

    跑马灯 MarqueeView★1568 - 垂直翻页公告 MarqueeViewDemo★896 - 跑马灯Vi...

  • textview - 跑马灯

    textview 的跑马灯不用说了吧,大家肯定都知道这是个啥,但是呢我还是放个图吧: 实现单个跑马灯 跑马灯的核心...

  • Android 基础 UI 之 TextView

    一、显示富文本 效果图image 布局文件 逻辑代码 二、跑马灯效果 1. 横向跑马灯 效果图: 单个实现跑马灯:...

  • iOS 跑马灯的实现

    介绍 我们一说起跑马灯第一个想到的就是:山寨机。接下来介绍的跑马灯和那个跑马灯是不一样滴。在iOS中,跑马灯是指l...

  • Android 图文垂直跑马灯

    最近在维护老项目,老项目有一个地方需要修改,就是垂直跑马灯的问题,之前的垂直跑马灯是只有文字跑马灯,新版需要加上。...

  • flutter跑马灯

    flutter_marquee flutter 插件 flutter 跑马灯可以指定跑马灯的方向可以传入数组,可以...

  • Kevin Learn Android:TextView

    一、显示富文本 效果图01.png 布局文件 逻辑代码 二、跑马灯效果 1. 横向跑马灯 效果图: 单个实现跑马灯...

  • 在手机上的手账本

    写手帐是一个需要长期坚持的事情,之前看见姐姐在写手账觉得十分新奇,每天翻看她的手账本,有兴趣时也会画上几笔...

  • iOS 局部跑马灯效果实现

    前言 开发时有一个需求是实现跑马灯效果,其实跑马灯还是比较容易实现的,但是这个是一个局部范围的跑马灯,平时使用的都...

网友评论

      本文标题: 手机上的跑马灯

      本文链接:https://www.haomeiwen.com/subject/qfwlsttx.html