在 iOS中實現噴槍打字動畫的代碼

jopen 9年前發布 | 2K 次閱讀 Objective-C IOS

實現原理比較簡單,這里不做過多介紹.


#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 = @"人生最寶貴的是生命,生命屬于人只有一次。一個人的生命應當這樣度過:當他回憶往事的時候,他不致因虛度年華而悔恨,也不致因碌碌無為而羞愧;在臨死的時候,他能夠說:“我的整個生命和全部精力,都已獻給世界上最壯麗的事業——為人類的解放而斗爭。”";


}

- (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;
}


最終效果:



來自:http://blog.csdn.net/qq_31810357/article/details/49486509

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!