Implementation of spray gun typing animation in iOS
The implementation principle is relatively simple. I will not introduce it too much here.
#import ViewController.h@interface ViewController ()@property (weak, nonatomic) IBOutlet UILabel *titleLabel;@property (copy , nonatomic)NSString *contentStr;@end
-(Void) viewDidLoad {[super viewDidLoad]; NSThread * thread = [[NSThread alloc] initWithTarget: self selector: @ selector (animationLabel) object: nil]; [thread start]; self. contentStr = @ the most precious thing in life is life. Life only belongs to one person once. A person's life should be spent like this: When he remembers the past, he will not regret it because of the past, nor be ashamed of being idle. When he dies, he can say: "my whole life and all my energy have been devoted to the most magnificent undertaking in the world-the struggle for the liberation of mankind. "; }
- (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.3]; }}
- (void)refreshUIWithContentStr:(NSString *)contentStr{ self.titleLabel.text = contentStr;}
Final effect: