循环播放文字

import "ViewController.h"

#define IOS_SystemVersion  [[[UIDevice currentDevice]systemVersion]floatValue]
#define WIDThH  [[UIScreen mainScreen] bounds].size.width
#define HEIGHT  [[UIScreen mainScreen] bounds].size.height

#define kBulldesFont [UIFont systemFontOfSize:15]

@interface ViewController ()
{
    UILabel *_contentLabel;
    NSTimer *_timer;
    int _number;
    CGSize _size;
}

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    UIView *view = [[UIView alloc] init];
    view.frame = CGRectMake(0, 60, WIDThH, 45);
    view.backgroundColor = [UIColor redColor];
    [self.view addSubview:view];
    _number = 0;
    _contentLabel = [[UILabel alloc] init];
    _contentLabel.text = @"今天是个好日子,神就是光、道路、真理和生命,敬畏耶和华这人便是有福。基督耶稣是世人唯一的救世主!";
    _size = [_contentLabel.text sizeWithAttributes:@{NSFontAttributeName:kBulldesFont}];
    _contentLabel.frame = CGRectMake(WIDThH, 0, _size.width, 45);
    _contentLabel.font = kBulldesFont;
    _contentLabel.textColor = [UIColor whiteColor];

    [self.view addSubview:_contentLabel];
    _timer = [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(timeFireMethod) userInfo:nil repeats:YES];
}

#pragma mark 计时器
-(void)timeFireMethod
{
    if (_number*0.5<_size.width + WIDThH) {
        _number++;
        _contentLabel.frame = CGRectMake(WIDThH - _number*0.5, 60, _size.width, 45);
        
    }else{
        _number = 0;
        _contentLabel.frame = CGRectMake(WIDThH - _number*0.5, 60, _size.width, 45);
    }
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

你可能感兴趣的:(循环播放文字)