iOS-UILabel实现滚动文字效果

代码如下:

#import "Ridehorselight.h"

@interface Ridehorselight ()

@property (nonatomic, strong) NSTimer *timer; //定时器
@property (nonatomic, weak) UIView *viewAnima; //Label的背景
@property (nonatomic, weak) UILabel *customLabel; //Label

@end

@implementation Ridehorselight

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self addClildView];
    [self setTimer];
}

- (void)addClildView {
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    CGFloat viewX = (self.view.frame.size.width-200)/2;
    UIView *viewAnima = [[UIView alloc] initWithFrame:CGRectMake(viewX, 100, 200, 40)];
    viewAnima.backgroundColor = [UIColor  yellowColor];
    self.viewAnima = viewAnima;
    self.viewAnima.clipsToBounds = YES; //剪掉超出view范围部分
    
    CGFloat customLabelY = (self.viewAnima.frame.size.height-30)/2;
    UILabel *customLabel = [[UILabel alloc]initWithFrame:CGRectMake(self.viewAnima.frame.size.width, customLabelY, 200, 30)];
    customLabel.text = @"跑马灯效果,滚动文本!";
    customLabel.textColor = [UIColor redColor];
    self.customLabel = customLabel;
    
    [self.view addSubview:viewAnima];
    [viewAnima addSubview:customLabel];
}

- (void)setTimer {

    self.timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(changePos) userInfo:nil repeats:YES];
}

- (void)changePos {
    
    CGPoint cenPos = self.customLabel.center;
    if (cenPos.x < -100) {
        
        CGFloat distance = self.customLabel.frame.size.width/2;
        self.customLabel.center = CGPointMake(self.viewAnima.frame.size.width+distance, 20);
    } else {
        self.customLabel.center = CGPointMake(cenPos.x-5, 20);
    }
}

你可能感兴趣的:(iOS-UILabel实现滚动文字效果)