iOS 跑马灯效果

2017-09-27  本文已影响87人  风外杏林香

一个简单的跑马灯效果,话不多说,直接看代码
建一个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地址

上一篇下一篇

猜你喜欢

热点阅读