iOS 跑马灯效果

一个简单的跑马灯效果,话不多说,直接看代码
建一个UIView类

.h文件中声明三个类方法
@property (nonatomic, strong) NSString  * title;
+ (instancetype)marqueeBarWithFrame:(CGRect)frame title:(NSString *)title;
- (void)updateTitle:(NSString *)title;
- (void)resume;
.m文件中
@interface MarqueeBar ()
{
    NSTimer     *_timer;
}
/*我这里label由俩部分构成,因而直接建俩label*/
@property (nonatomic, strong) UILabel *firstLabel;
@property (nonatomic, strong) UILabel *secondLabel;
@property (nonatomic, strong) NSMutableArray *labelArray;
@end
+ (instancetype)marqueeBarWithFrame:(CGRect)frame title:(NSString*)title
{
    JCmarqueBar * marqueBar = [[JCmarqueBar alloc] initWithFrame:frame];
    marqueBar.title = title;
    return marqueBar;
}
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self addSubview:self.firstLabel];
        [self addSubview:self.secondLabel];
        
        [self.labelArray addObject:_firstLabel];
        [self.labelArray addObject:_secondLabel];
    }
    return self;
}
- (void)setTitle:(NSString *)title {
    _title = title;
    
    if (_timer) {
        [_timer invalidate];
        _timer = nil;
    }
    NSString *raceStr = [NSString stringWithFormat:@"%@    ",title];
    _firstLabel.text = raceStr;
    _secondLabel.text = raceStr;
    self.firstLabel.frame = CGRectMake(0, 0, [self getStringWidth:raceStr], self.frame.size.height);
    self.secondLabel.frame = CGRectMake(_firstLabel.frame.origin.x + _firstLabel.bounds.size.width, _firstLabel.frame.origin.y, _firstLabel.bounds.size.width, _firstLabel.bounds.size.height);
    _secondLabel.hidden = ![self isNeedRaceAnimate];
    if ([self isNeedRaceAnimate]) {
        [self startAnimation];
    }
}

- (void)updateTitle:(NSString *)title {
    self.title = title;
}

- (BOOL)isNeedRaceAnimate{
    return !(_firstLabel.bounds.size.width <= self.bounds.size.width);
}

- (void)layoutSubviews{
    [super layoutSubviews];
    
    if (_firstLabel && _secondLabel) {
        _firstLabel.frame = CGRectMake(50, 0, _firstLabel.bounds.size.width, self.bounds.size.height);
        _secondLabel.frame = CGRectMake(_firstLabel.frame.origin.x + _firstLabel.bounds.size.width, _firstLabel.frame.origin.y, _firstLabel.bounds.size.width, _firstLabel.bounds.size.height);
    }
    _secondLabel.hidden = ![self isNeedRaceAnimate];
}


- (void)startAnimation{
    _timer = [NSTimer scheduledTimerWithTimeInterval:1.0 / 120 target:self selector:@selector(raceLabelFrameChanged:) userInfo:nil repeats:YES];
    [_timer fire];
    [[NSRunLoop mainRunLoop] addTimer:_timer forMode:NSRunLoopCommonModes];
}
- (void)raceLabelFrameChanged:(NSTimer *)timer
{
    UILabel *firstLabel = [self.labelArray firstObject];
    UILabel *secondLabel = [self.labelArray lastObject];
    CGRect frameOne = firstLabel.frame;
    CGRect frameTwo = secondLabel.frame;
    CGFloat firstX = firstLabel.frame.origin.x;
    CGFloat secondX = secondLabel.frame.origin.x;
    firstX -= 0.5;
    secondX -= 0.5;
    if (ABS(firstX) >= firstLabel.bounds.size.width) {
        firstX = secondX + firstLabel.bounds.size.width;
        [self.labelArray exchangeObjectAtIndex:0 withObjectAtIndex:1];
    }
    frameOne.origin.x = firstX;
    frameTwo.origin.x = secondX;
    firstLabel.frame = frameOne;
    secondLabel.frame = frameTwo;
}
- (void)resume
{
    [self resumeAndStart:NO];
}

- (void)resumeAndStart
{
    [self resumeAndStart:YES];
}
- (void)resumeAndStart:(BOOL)start
{
    if (_timer) {
        [_timer invalidate];
        _timer = nil;
    }
    _firstLabel.frame = CGRectMake(50, 0, _firstLabel.bounds.size.width, self.bounds.size.height);
    _secondLabel.frame = CGRectMake(_firstLabel.frame.origin.x + _firstLabel.bounds.size.width, _firstLabel.frame.origin.y, _firstLabel.bounds.size.width, _firstLabel.bounds.size.height);
    if (start) {
        [self startAnimation];
    }
}
#pragma mark - Properties
- (UILabel *)firstLabel 
{
    if (_firstLabel == nil) {
        _firstLabel = [[UILabel alloc] init];
        _firstLabel.font = [UIFont systemFontOfSize:14.0];
        _firstLabel.textColor = RGB(102, 102, 102, 1);
        _firstLabel.textAlignment = NSTextAlignmentCenter;
    }
    return _firstLabel;
}

- (UILabel *)secondLabel {
    if (_secondLabel == nil) {
        _secondLabel = [[UILabel alloc] init];
        _secondLabel.font = [UIFont systemFontOfSize:14.0];
        _secondLabel.textColor = RGB(102, 102, 102, 1);
        _secondLabel.textAlignment = NSTextAlignmentCenter;
    }
    return _secondLabel;
}

- (NSMutableArray *)labelArray{
    if (!_labelArray) {
        self.labelArray = [NSMutableArray arrayWithCapacity:0];
    }
    return _labelArray;
}


- (CGFloat)getStringWidth:(NSString *)string{
    if (string) {
        CGRect rect = [string boundingRectWithSize:CGSizeMake(MAXFLOAT, 0)
                                           options:NSStringDrawingTruncatesLastVisibleLine |NSStringDrawingUsesLineFragmentOrigin |
                       NSStringDrawingUsesFontLeading
                                        attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14.0]}
                                           context:nil];
        return rect.size.width;
    }
    return 0.f;
}

到此,一个跑马灯的效果完成,demo地址

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 介绍 因业务需求,需要做一个跑马灯效果的公告栏,查了网上大部分制作原理,都不能满足也无需求,决定自己造轮子 效果 ...
    Topredator阅读 9,483评论 3 10
  • iOS中跑马灯实现由两种一种是通过UIView的动画,第二种是通过UIScrollView实现,最简单的通过UIV...
    FlyElephant阅读 6,287评论 0 4
  • 跑马灯效果在移动客户端中多用于“公告”、“消息”等功能的展示,本次demo以一种简易的实现方法(UILabel...
    fantasy_wan阅读 3,625评论 0 0
  • 前排附上Demo地址:https://github.com/3KK3/AutoScrollDemo/tree/ma...
    芝麻酱的简书阅读 4,469评论 4 1
  • 一.简介 跑马灯其实就是一串文字的轮播,实现的方式有很多种。这里我只介绍一种,通过CADisplayLink与UI...
    Sunshine_Boys阅读 2,560评论 0 1