iOS 局部跑馬燈效果實(shí)現(xiàn)

前言

開發(fā)時(shí)有一個(gè)需求是實(shí)現(xiàn)跑馬燈效果,其實(shí)跑馬燈還是比較容易實(shí)現(xiàn)的,但是這個(gè)是一個(gè)局部范圍的跑馬燈,平時(shí)使用的都是全屏幕長(zhǎng)度的跑馬燈,經(jīng)過一番折騰,找的了重點(diǎn),下面這句代碼就是我們實(shí)現(xiàn)局部的關(guān)鍵。

@property(nonatomic) BOOL clipsToBounds; // When YES, content and subviews are clipped to the bounds of the view. Default is NO.

效果圖

未命名.gif

代碼

//.h
#import <UIKit/UIKit.h>

@interface HHRunLabelView : UIView
//字體顏色
@property (nonatomic, strong) UIColor *textColor;

//字體大小
@property (nonatomic, strong) UIFont *font;

//要顯示的內(nèi)容
@property (nonatomic, strong) NSString *text;

/**
 移動(dòng)的速度[0~1],默認(rèn)是0.2
 */
@property (nonatomic, assign) CGFloat speed;

@end


//.m
#import "HHRunLabelView.h"

@interface HHRunLabelView()
@property (nonatomic, assign) CGFloat offsetX; //x偏移量
@property (nonatomic, strong) UILabel *moveLabel;
@property (nonatomic, assign) CGFloat labelWidth; //label的寬度
@end

@implementation HHRunLabelView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]){
        _speed = 0.2;//默認(rèn)值
        [self initView];
        [self initTimer];
    }
    return self;
}

- (void)initView{
    _moveLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
    [self addSubview:_moveLabel];
}

//初始化timer
- (void)initTimer{
    CADisplayLink *timer = [CADisplayLink displayLinkWithTarget:self selector:@selector(displayLabelAction)];
    timer.frameInterval = 2.0;
    [timer addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];
}

- (void)setTextColor:(UIColor *)textColor{
    _moveLabel.textColor = textColor;
}

- (void)setFont:(UIFont *)font{
    _moveLabel.font = font;
}

//設(shè)置速度
- (void)setSpeed:(CGFloat)speed{
    if (speed < 0) {
        speed = 0;
    }else if (speed > 1){
        speed = 1;
    }
    _speed = speed*5;
}

//賦值text
- (void)setText:(NSString *)text{
    _moveLabel.text = text;
    [_moveLabel sizeToFit];
    CGRect rect = _moveLabel.frame;
    rect.size.height = self.frame.size.height;
    _moveLabel.frame = rect;
    _offsetX = _moveLabel.frame.origin.x;
    self.clipsToBounds = YES;//設(shè)置了這個(gè)屬性后才能局部顯示
}

- (void)displayLabelAction{
    
    _labelWidth = self.moveLabel.frame.size.width;
    if (_labelWidth < self.frame.size.width) return;//如果字能顯示全則不移動(dòng)
    
    CGRect rect = self.moveLabel.frame;
    _offsetX -= self.speed;
    rect.origin.x = _offsetX;
    self.moveLabel.frame = rect;
    if (_offsetX < -_labelWidth){
        _offsetX = self.frame.size.width; //重置起點(diǎn)
    }
}

用法

//導(dǎo)入頭文件后加載
 HHRunLabelView *runLabel = [[HHRunLabelView alloc] initWithFrame:CGRectMake(10, 100, 100, 50)];
    runLabel.backgroundColor = [UIColor grayColor];
    runLabel.textColor = [UIColor redColor];
    runLabel.speed = 0.5;
    runLabel.text = @"我是一個(gè)局部的小跑馬燈啊";
    [self.view addSubview:runLabel];

Demo地址github

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容