打字机效果

2017-06-16  本文已影响9人  元昊
typewriteGif.gif

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

@interface TypeWriterLabel : UILabel
/** Z
 *  设置单个字打印间隔时间,默认 0.3 秒
 */
@property (nonatomic) NSTimeInterval typewriteTimeInterval;

/** Z
 *  开始打印的位置索引,默认为0,即从头开始
 */
@property (nonatomic) int currentIndex;

/** Z
 *  输入字体的颜色
 */
@property (nonatomic, strong) UIColor *typewriteEffectColor;

/** Z
 *  开始打印
 */
-(void)startTypewrite;

/** Z
 *  定时器
 */
@property (nonatomic, strong) NSTimer  *timer;


@end


 #import "TypeWriterLabel.h"

@implementation TypeWriterLabel

-(void)startTypewrite

{
   
   if (_timer) {
       [_timer invalidate];
       _timer = nil;
   }
   
  _timer = [NSTimer scheduledTimerWithTimeInterval:self.typewriteTimeInterval target:self selector:@selector(outPutWord:) userInfo:nil repeats:YES];

   [_timer fire];
   
}
-(void) outPutWord:(id)atimer

{

   if (self.text.length == self.currentIndex) {
       [atimer invalidate];
       atimer = nil;
   }else{

       self.currentIndex++;
       NSDictionary *dic = @{NSForegroundColorAttributeName: self.typewriteEffectColor};
       NSMutableAttributedString *mutStr = [[NSMutableAttributedString alloc] initWithString:self.text];
       [mutStr addAttributes:dic range:NSMakeRange(0, self.currentIndex)];
       [self setAttributedText:mutStr];

   }

}

@end


-(TypeWriterLabel *)typeLabel{
    if (!_typeLabel) {
        _typeLabel = [[TypeWriterLabel alloc] init];
        [_typeLabel setTypewriteTimeInterval:0.05f];
        [_typeLabel setTypewriteEffectColor:[UIColor grayColor]];
        [_typeLabel setCurrentIndex:0];
        [_typeLabel setBackgroundColor:[UIColor clearColor]];
        [_typeLabel setTextColor:[UIColor clearColor]];
        [_typeLabel setNumberOfLines:0];
    }
    return _typeLabel;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    [self.view setBackgroundColor:[UIColor whiteColor]];
    
    UIImageView *backImageView = [[UIImageView alloc] init];
    [backImageView setFrame:self.view.bounds];
    [backImageView setImage:[UIImage imageNamed:@"1.jpg"]];
    [self.view addSubview:backImageView];
    
    
    [self.typeLabel setFrame:(CGRectMake(20, 30, [UIScreen mainScreen].bounds.size.width - 40, [UIScreen mainScreen].bounds.size.height - 40))];
    [self.view addSubview:self.typeLabel];
    
    [self.typeLabel setText:@"拉塞尔·威斯布鲁克(Russell Westbrook), 1988年11月12日出生于美国加利福尼亚州长滩(Long Beach, CA),美国职业篮球运动员,司职控球后卫,效力于NBA俄克拉荷马城雷霆队。\n拉塞尔·威斯布鲁克于2008年通过选秀进入NBA,新秀赛季入选最佳新秀阵容第一阵容;6次入选全明星阵容,2015、2016连续两年获得全明星赛MVP;2次入选最佳阵容第一阵容,4次入选最佳阵容第二阵容。\n2017年4月10日,雷霆客场106-105战胜掘金,拉塞尔·威斯布鲁克出场37分钟,得到50分16篮板10助攻,收获赛季第42次、职业生涯常规赛第79次三双,打破了1961-62赛季奥斯卡·罗伯特森创造的单赛季41次三纪录。同时,这是威斯布鲁克赛季第3次得分50+的三双,成为NBA历史第一人"];
    [self.typeLabel startTypewrite];
    
}

更新时需要重置currentIndex为0;赋新值;再调用startTypewrite

附Github地址https://github.com/yuanlove/Typewite-TypewriteLabel

上一篇 下一篇

猜你喜欢

热点阅读