单行文字走马灯效果 IOS Lable 滚动效果

一个独立的简单控件,实现文字走马灯效果,自定义view嵌套Lable实现:


IMG_2423.GIF

.h:

#import 

NS_ASSUME_NONNULL_BEGIN

@interface QQXYLineScrollTextView : UIView

@property (strong,nonatomic)UILabel *label;
@property (strong,nonatomic)NSString *text;
@property (strong,nonatomic)UIFont *font;
@property (strong,nonatomic)UIColor *textColor;

@end

NS_ASSUME_NONNULL_END

.m:


#import "QQXYLineScrollTextView.h"

@interface QQXYLineScrollTextView ()
@property NSTimer *labelScrollTimer;
@end
@implementation QQXYLineScrollTextView

-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
    //使lable超出父视图不显示
        self.layer.masksToBounds = YES;
    }
    return self;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    self.label.frame = CGRectMake(0, 0, self.label.frame.size.width, self.frame.size.height);
    [self labelNeedScroll:self.label];
}
//此方法判断是否需要滚动,如果需要直接调用滚动方法
-(BOOL)labelNeedScroll:(UILabel *)label
{
    BOOL returnBool = NO;
    if (label.frame.size.width >self.frame.size.width/2-30-8) {
        returnBool = YES;
    }
    if (returnBool) {
        [self startScrollLabel:label];
    }
    return returnBool;
}
//停止滚动
-(void)stopScrollLabel
{
    if (self.labelScrollTimer) {
        [self.labelScrollTimer invalidate];
        self.labelScrollTimer = nil;
    }
}
//开始滚动
-(void)startScrollLabel:(UILabel *)label
{
    if (label == self.label) {
        if (self.labelScrollTimer) {
            [self.labelScrollTimer invalidate];
            self.labelScrollTimer = nil;
        }
      //这里是用nstime做的滚动效果,模仿的一些网页或app一顿一顿移动的样子。也可以用动画做流畅的滚动效果,根据lable长度和自己定的移动速度即可算出总动画时长。
        self.labelScrollTimer = [NSTimer scheduledTimerWithTimeInterval:0.4 target:self selector:@selector(scrollTimer:) userInfo:label repeats:YES];
    }
}
-(void)scrollTimer:(NSTimer *)timer
{
    [self scrollLabel:[timer userInfo]];
}
-(void)scrollLabel:(UILabel *)label
{
    if (label.frame.origin.x == 0) {
        label.frame = CGRectMake(11, label.frame.origin.y, label.frame.size.width, label.frame.size.height);
    } 
    if (label.frame.origin.x +label.frame.size.width < self.frame.size.width-10)
    {
        label.frame = CGRectMake(8, label.frame.origin.y, label.frame.size.width, label.frame.size.height);
    }else{
        label.frame = CGRectMake(label.frame.origin.x-4, label.frame.origin.y, label.frame.size.width, label.frame.size.height);
    }
}
//一些参数透传给label
-(void)setText:(NSString *)text{

    _text = text;
    self.label.text = text;
    [self.label sizeToFit];
    [self labelNeedScroll:self.label];
}
- (void)setFont:(UIFont *)font
{
    _font = font;
    self.label.font = font;
    [self.label sizeToFit];
    [self labelNeedScroll:self.label];
}
-(void)setTextColor:(UIColor *)textColor
{
    _textColor = textColor;
    self.label.textColor = textColor;
}
-(UILabel *)label
{
    if (!_label) {
        _label = [[UILabel alloc] init];
        _label.font = [UIFont fontWithName:@"PingFang-SC-Regular" size:12];
        _label.numberOfLines = 1;
        [self addSubview:_label];
    }
    return _label;
}

@end

你可能感兴趣的:(单行文字走马灯效果 IOS Lable 滚动效果)