123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393 |
- //
- // LMJHorizontalScrollText.m
- // LMJHorizontalScrollTextExample
- //
- // Created by LiMingjie on 2019/8/22.
- // Copyright © 2019 LMJ. All rights reserved.
- //
- #import "LMJHorizontalScrollText.h"
- @interface LMJHorizontalScrollText()
- @property (nonatomic, strong) UILabel * contentLabel1;
- @property (nonatomic, strong) UILabel * contentLabel2;
- @property (nonatomic, assign) CGFloat textWidth;
- @property (nonatomic, assign) int wanderingOffset;
- @property (nonatomic, strong) NSTimer * timer;
- @end
- @implementation LMJHorizontalScrollText
- {
- CGFloat _selfWidth;
- CGFloat _selfHeight;
- }
- #pragma mark - Init
- - (id)init{
- self = [super init];
- if (self) {
- self.frame = CGRectMake(0, 0, 100, 20); // 设置一个初始的frame
- }
- return self;
- }
- - (id)initWithFrame:(CGRect)frame{
- self = [super initWithFrame:frame];
- if (self) {
- [self setInitialSettings];
- }
- return self;
- }
- - (void)awakeFromNib{
- [super awakeFromNib];
- [self setInitialSettings];
- }
- - (void)layoutSubviews{
- _selfWidth = self.frame.size.width;
- _selfHeight = self.frame.size.height;
- }
- - (void)setInitialSettings {
- self.clipsToBounds = YES;
-
- _contentLabel1 = nil;
- _contentLabel2 = nil;
-
- _text = @"";
- _textFont = [UIFont systemFontOfSize:12];
- _textColor = [UIColor blackColor];
-
- _speed = 0.03;
- _textWidth = 0;
- _wanderingOffset = -1;
-
- _moveMode = LMJTextScrollWandering;
- _moveDirection = LMJTextScrollMoveLeft;
-
- _timer = nil;
-
- _selfWidth = self.frame.size.width;
- _selfHeight = self.frame.size.height;
- }
- #pragma mark - Set
- - (void)setText:(NSString *)text{
- if ([_text isEqualToString: text]) return;
- _text = text;
- [self updateTextWidth];
- [self updateLabelsFrame];
- if (_contentLabel1 != nil) {
- _contentLabel1.text = _text;
- }
- if (_contentLabel2 != nil) {
- _contentLabel2.text = _text;
- }
- }
- - (void)setTextFont:(UIFont *)textFont{
- if (_textFont == textFont) return;
- _textFont = textFont;
- [self updateTextWidth];
- [self updateLabelsFrame];
- if (_contentLabel1 != nil) {
- _contentLabel1.font = _textFont;
- }
- if (_contentLabel2 != nil) {
- _contentLabel2.font = _textFont;
- }
- }
- - (void)setTextColor:(UIColor *)textColor{
- _textColor = textColor;
- if (_contentLabel1 != nil) {
- _contentLabel1.textColor = _textColor;
- }
- if (_contentLabel2 != nil) {
- _contentLabel2.textColor = _textColor;
- }
- }
- - (void)setSpeed:(CGFloat)speed{
- _speed = speed;
- [self move];
- }
- - (void)setMoveMode:(LMJTextScrollMode)moveMode {
- _moveMode = moveMode;
-
- // if (self.text.length == 0) {//如果字符串长度为0,直接返回
- // return;
- // }
-
- if (_timer) {
- [_timer invalidate];
- _timer = nil;
- }
-
- [self.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
- [obj removeFromSuperview];
- obj = nil;
- }];
-
- switch (self.moveMode) {
- case LMJTextScrollContinuous:
- {
- if (self.moveDirection == LMJTextScrollMoveLeft) {
- [self creatLabel1WithFrame1:CGRectMake(0, 0, _textWidth, _selfHeight) andLabel2WithFrame2:CGRectMake(_textWidth, 0, _textWidth, _selfHeight)];
- }else{
- [self creatLabel1WithFrame1:CGRectMake(_selfWidth -_textWidth, 0, _textWidth, _selfHeight) andLabel2WithFrame2:CGRectMake(_selfWidth -_textWidth -_textWidth, 0, _textWidth, _selfHeight)];
- }
- }break;
-
- case LMJTextScrollIntermittent:
- {
- if (self.moveDirection == LMJTextScrollMoveLeft) {
- [self creatLabel1WithFrame:CGRectMake(0, 0, _textWidth, _selfHeight)];
- }else{
- [self creatLabel1WithFrame:CGRectMake(_selfWidth -_textWidth, 0, _textWidth, _selfHeight)];
- }
- }break;
-
- case LMJTextScrollFromOutside:
- {
- if (self.moveDirection == LMJTextScrollMoveLeft) {
- [self creatLabel1WithFrame:CGRectMake(_selfWidth, 0, _textWidth, _selfHeight)];
- }else{
- [self creatLabel1WithFrame:CGRectMake(_textWidth, 0, _textWidth, _selfHeight)];
- }
- }break;
-
- case LMJTextScrollWandering:
- {
- [self creatLabel1WithFrame:CGRectMake(0, 0, _textWidth, _selfHeight)];
- }break;
-
- default:
- break;
- }
-
- if (!_timer) {
- [self move];
- }
- }
- #pragma mark - Create Labels
- -(void)creatLabel1WithFrame:(CGRect)frame{
- _contentLabel1 = [[UILabel alloc] initWithFrame:frame];
- _contentLabel1.text = self.text;
- _contentLabel1.font = self.textFont;
- _contentLabel1.textColor = self.textColor;
- _contentLabel1.tag = 1001;
- _contentLabel1.backgroundColor = [UIColor clearColor];
- [self addSubview:_contentLabel1];
-
- if (_contentLabel2) {
- [_contentLabel2 removeFromSuperview];
- _contentLabel2 = nil;
- }
- }
- -(void)creatLabel1WithFrame1:(CGRect)frame1 andLabel2WithFrame2:(CGRect)frame2{
- [self creatLabel1WithFrame:frame1];
-
- _contentLabel2 = [[UILabel alloc] initWithFrame:frame2];
- _contentLabel2.text = self.text;
- _contentLabel2.font = self.textFont;
- _contentLabel2.textColor = self.textColor;
- _contentLabel2.tag = 1002;
- _contentLabel2.backgroundColor = [UIColor clearColor];
- [self addSubview:_contentLabel2];
- }
- #pragma mark - Util
- - (void)updateTextWidth {
- _textWidth = [self.text sizeWithAttributes:[NSDictionary dictionaryWithObjectsAndKeys:self.textFont, NSFontAttributeName, nil]].width;
- }
- - (void)updateLabelsFrame {
-
- CGFloat label1_x = _contentLabel1.frame.origin.x;
- CGFloat label2_x = _contentLabel2.frame.origin.x;
- if (_contentLabel1 && _contentLabel2) {
- if (label1_x < label2_x) {
- _contentLabel1.frame = CGRectMake(label1_x, 0, _textWidth, _selfHeight);
- _contentLabel2.frame = CGRectMake(label1_x + _textWidth, 0, _textWidth, _selfHeight);
- } else {
- _contentLabel2.frame = CGRectMake(label2_x, 0, _textWidth, _selfHeight);
- _contentLabel1.frame = CGRectMake(label2_x + _textWidth, 0, _textWidth, _selfHeight);
-
- }
- } else {
- if (_contentLabel1) {
- _contentLabel1.frame = CGRectMake(label1_x, 0, _textWidth, _selfHeight);
- }
- if (_contentLabel2) {
- _contentLabel2.frame = CGRectMake(label2_x, 0, _textWidth, _selfHeight);
- }
- }
- }
- - (void)resetLabelsPosition {
- switch (self.moveMode) {
- case LMJTextScrollContinuous:
- {
- if (self.moveDirection == LMJTextScrollMoveLeft) {
- _contentLabel1.frame = CGRectMake(0, 0, _textWidth, _selfHeight);
- _contentLabel2.frame = CGRectMake(_textWidth, 0, _textWidth, _selfHeight);
- }else{
- _contentLabel1.frame = CGRectMake(_selfWidth -_textWidth, 0, _textWidth, _selfHeight);
- _contentLabel2.frame = CGRectMake(_selfWidth -_textWidth -_textWidth, 0, _textWidth, _selfHeight);
- }
- }break;
-
- case LMJTextScrollIntermittent:
- {
- if (self.moveDirection == LMJTextScrollMoveLeft) {
- _contentLabel1.frame = CGRectMake(0, 0, _textWidth, _selfHeight);
- }else{
- _contentLabel2.frame = CGRectMake(_selfWidth -_textWidth, 0, _textWidth, _selfHeight);
- }
- }break;
-
- case LMJTextScrollFromOutside:
- {
- if (self.moveDirection == LMJTextScrollMoveLeft) {
- _contentLabel1.frame = CGRectMake(_selfWidth, 0, _textWidth, _selfHeight);
- }else{
- _contentLabel1.frame = CGRectMake(_textWidth, 0, _textWidth, _selfHeight);
- }
- }break;
-
- case LMJTextScrollWandering:
- {
- _contentLabel1.frame = CGRectMake(0, 0, _textWidth, _selfHeight);
- }break;
-
- default:
- break;
- }
- }
- #pragma mark - Move Action
- // LMJTextScrollContinuous
- -(void)moveContinuous{
- if (self.moveDirection == LMJTextScrollMoveLeft) {
- _contentLabel1.frame = CGRectMake(_contentLabel1.frame.origin.x -1, 0, _textWidth, _selfHeight);
- _contentLabel2.frame = CGRectMake(_contentLabel2.frame.origin.x -1, 0, _textWidth, _selfHeight);
- if (_contentLabel1.frame.origin.x < -_textWidth) {
- _contentLabel1.frame = CGRectMake(_contentLabel2.frame.origin.x + _textWidth, 0, _textWidth, _selfHeight);
- }
- if (_contentLabel2.frame.origin.x < -_textWidth) {
- _contentLabel2.frame = CGRectMake(_contentLabel1.frame.origin.x + _textWidth, 0, _textWidth, _selfHeight);
- }
- }else{
- _contentLabel1.frame = CGRectMake(_contentLabel1.frame.origin.x +1, 0, _textWidth, _selfHeight);
- _contentLabel2.frame = CGRectMake(_contentLabel2.frame.origin.x +1, 0, _textWidth, _selfHeight);
- if (_contentLabel1.frame.origin.x > _selfWidth) {
- _contentLabel1.frame = CGRectMake(_contentLabel2.frame.origin.x - _textWidth, 0, _textWidth, _selfHeight);
- }
- if (_contentLabel2.frame.origin.x > _selfWidth) {
- _contentLabel2.frame = CGRectMake(_contentLabel1.frame.origin.x - _textWidth, 0, _textWidth, _selfHeight);
- }
- }
- }
- // LMJTextScrollIntermittent
- -(void)moveIntermittent{
- if (self.moveDirection == LMJTextScrollMoveLeft) {
- _contentLabel1.frame = CGRectMake(_contentLabel1.frame.origin.x -1, 0, _textWidth, _selfHeight);
- if (_contentLabel1.frame.origin.x < -_textWidth) {
- _contentLabel1.frame = CGRectMake(0, 0, _textWidth, _selfHeight);
- }
- }else{
- _contentLabel1.frame = CGRectMake(_contentLabel1.frame.origin.x +1, 0, _textWidth, _selfHeight);
- if (_contentLabel1.frame.origin.x > _selfWidth) {
- _contentLabel1.frame = CGRectMake(_selfWidth -_textWidth, 0, _textWidth, _selfHeight);
- }
- }
- }
- // LMJTextScrollFromOutside
- -(void)moveFromOutside{
- if (self.moveDirection == LMJTextScrollMoveLeft) {
- _contentLabel1.frame = CGRectMake(_contentLabel1.frame.origin.x -1, 0, _textWidth, _selfHeight);
- if (_contentLabel1.frame.origin.x < -_textWidth) {
- _contentLabel1.frame = CGRectMake(_selfWidth, 0, _textWidth, _selfHeight);
- }
- }else{
- _contentLabel1.frame = CGRectMake(_contentLabel1.frame.origin.x +1, 0, _textWidth, _selfHeight);
- if (_contentLabel1.frame.origin.x > _selfWidth) {
- _contentLabel1.frame = CGRectMake(-_textWidth, 0, _textWidth, _selfHeight);
- }
- }
- }
- // LMJTextScrollWandering
- -(void)moveWandering{
- if (_textWidth > _selfWidth) {
- _contentLabel1.frame = CGRectMake(_contentLabel1.frame.origin.x + _wanderingOffset, 0, _textWidth, _selfHeight);
- if (_contentLabel1.frame.origin.x < -(_textWidth -_selfWidth +2)) {
- _wanderingOffset = 1;
- }
- if (_contentLabel1.frame.origin.x > 2) {
- _wanderingOffset = -1;
- }
- }else if (_textWidth < _selfWidth){
- _contentLabel1.frame = CGRectMake(_contentLabel1.frame.origin.x + _wanderingOffset, 0, _textWidth, _selfHeight);
- if (_contentLabel1.frame.origin.x < 0) {
- _wanderingOffset = 1;
- }
- if (_contentLabel1.frame.origin.x > _selfWidth - _textWidth) {
- _wanderingOffset = -1;
- }
- }
- }
- #pragma mark - API Methods
- - (void)move {
- if (_timer != nil) {
- [_timer invalidate];
- _timer = nil;
- }
- switch (self.moveMode) {
- case LMJTextScrollContinuous:
- {
- _timer = [NSTimer scheduledTimerWithTimeInterval:self.speed target:self selector:@selector(moveContinuous) userInfo:nil repeats:YES];
- }
- break;
- case LMJTextScrollIntermittent:
- {
- _timer = [NSTimer scheduledTimerWithTimeInterval:self.speed target:self selector:@selector(moveIntermittent) userInfo:nil repeats:YES];
- }
- break;
- case LMJTextScrollFromOutside:
- {
- _timer = [NSTimer scheduledTimerWithTimeInterval:self.speed target:self selector:@selector(moveFromOutside) userInfo:nil repeats:YES];
- }
- break;
- case LMJTextScrollWandering:
- {
- _timer = [NSTimer scheduledTimerWithTimeInterval:self.speed target:self selector:@selector(moveWandering) userInfo:nil repeats:YES];
- }
- break;
- default:
- break;
- }
- [[NSRunLoop currentRunLoop] addTimer:_timer forMode:NSRunLoopCommonModes];
- }
- - (void)stop {
- [_timer invalidate];
- _timer = nil;
- [self resetLabelsPosition];
- }
- - (void)dealloc {
- [_timer invalidate];
- _timer = nil;
- }
- @end
|