iOS中 喷枪打字动画的实现

2016-10-18 19:17:26来源:CSDN作者:ajwdwl人点击

第七城市

#import <UIKit/UIKit.h>


@interface ViewController : UIViewController

@property (weak, nonatomic) IBOutlet UILabel *titleLabel;

@property (copy, nonatomic)NSString *contentStr;


@end


//

//  ViewController.m

//  1111

//

//  Created by admin on 2016/10/18.

//  Copyright © 2016 shinefs. All rights reserved.

//


#import "ViewController.h"


@interface ViewController ()


@end


@implementation ViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    _titleLabel.textAlignment = UITextAlignmentLeft;

    _titleLabel.lineBreakMode = UILineBreakModeWordWrap;

    _titleLabel.numberOfLines = 0;

    // Do any additional setup after loading the view, typically from a nib.

    NSThread *thread = [[NSThread alloc]initWithTarget:self selector:@selector(animationLabel) object:nil];

    [thread start];

    self.contentStr = @"人生最宝贵的是生命,生命属于人只有一次。一个人的生命应当这样度过:当他回忆往事的时候,他不致因虚度年华而悔恨,也不致因碌碌无为而羞愧;在临死的时候,他能够说:“我的整个生命和全部精力,都已献给世界上最壮丽的事业——为人类的解放而斗争。";

    

}

- (void)animationLabel

{

    for (NSInteger i = 0; i < self.contentStr.length; i++)

    {

        [self performSelectorOnMainThread:@selector(refreshUIWithContentStr:) withObject:[self.contentStr substringWithRange:NSMakeRange(0, i+1)] waitUntilDone:YES];

        [NSThread sleepForTimeInterval:0.05];

    }

}


- (void)refreshUIWithContentStr:(NSString *)contentStr

{

    self.titleLabel.text = contentStr;

}


- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}



@end





第七城市

最新文章

123

最新摄影

微信扫一扫

第七城市微信公众平台