<output id="qn6qe"></output>

    1. <output id="qn6qe"><tt id="qn6qe"></tt></output>
    2. <strike id="qn6qe"></strike>

      亚洲 日本 欧洲 欧美 视频,日韩中文字幕有码av,一本一道av中文字幕无码,国产线播放免费人成视频播放,人妻少妇偷人无码视频,日夜啪啪一区二区三区,国产尤物精品自在拍视频首页,久热这里只有精品12

      iOS開發基礎144-逐字打印效果

      在AIGC類的APP中,實現那種一個字一個字、一行一行地打印出文字的效果,可以通過多種方法來實現。下面是一些實現方法,使用Swift和OC來舉例說明。

      OC版

      1. 基于定時器的逐字打印效果

      可以使用NSTimer來逐字逐行地顯示文字。

      #import "ViewController.h"
      
      @interface ViewController ()
      
      @property (nonatomic, strong) UITextView *textView;
      @property (nonatomic, strong) NSString *content;
      @property (nonatomic, assign) NSInteger currentIndex;
      @property (nonatomic, strong) NSTimer *timer;
      
      @end
      
      @implementation ViewController
      
      - (void)viewDidLoad {
          [super viewDidLoad];
      
          self.textView = [[UITextView alloc] initWithFrame:self.view.bounds];
          self.textView.font = [UIFont systemFontOfSize:18];
          self.textView.editable = NO;
          self.textView.scrollEnabled = YES;
          [self.view addSubview:self.textView];
      
          self.content = @"這是需要逐字逐行打印的文字內容。\n讓我們來實現它。";
          self.currentIndex = 0;
      
          [self startPrinting];
      }
      
      - (void)startPrinting {
          self.timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(printNextCharacter) userInfo:nil repeats:YES];
      }
      
      - (void)printNextCharacter {
          if (self.currentIndex >= self.content.length) {
              [self.timer invalidate];
              self.timer = nil;
              return;
          }
      
          NSRange range = NSMakeRange(self.currentIndex, 1);
          NSString *nextCharacter = [self.content substringWithRange:range];
          self.textView.text = [self.textView.text stringByAppendingString:nextCharacter];
          
          self.currentIndex += 1;
      }
      
      @end
      

      2. 使用CADisplayLink來實現高精度逐字打印

      CADisplayLink可以在屏幕刷新時調用指定的方法,相較于NSTimer,其精度和性能更高。

      #import "ViewController.h"
      
      @interface ViewController ()
      
      @property (nonatomic, strong) UITextView *textView;
      @property (nonatomic, strong) NSString *content;
      @property (nonatomic, assign) NSInteger currentIndex;
      @property (nonatomic, strong) CADisplayLink *displayLink;
      
      @end
      
      @implementation ViewController
      
      - (void)viewDidLoad {
          [super viewDidLoad];
      
          self.textView = [[UITextView alloc] initWithFrame:self.view.bounds];
          self.textView.font = [UIFont systemFontOfSize:18];
          self.textView.editable = NO;
          self.textView.scrollEnabled = YES;
          [self.view addSubview:self.textView];
          
          self.content = @"這是需要逐字逐行打印的文字內容。\n讓我們來實現它。";
          self.currentIndex = 0;
      
          [self startPrinting];
      }
      
      - (void)startPrinting {
          self.displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(printNextCharacter)];
          self.displayLink.preferredFramesPerSecond = 10; // 控制打印速度
          [self.displayLink addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];
      }
      
      - (void)printNextCharacter {
          if (self.currentIndex >= self.content.length) {
              [self.displayLink invalidate];
              self.displayLink = nil;
              return;
          }
      
          NSRange range = NSMakeRange(self.currentIndex, 1);
          NSString *nextCharacter = [self.content substringWithRange:range];
          self.textView.text = [self.textView.text stringByAppendingString:nextCharacter];
          
          self.currentIndex += 1;
      }
      
      @end
      

      3. CATextLayer + Animation

      還可以使用CATextLayer和動畫來實現更為復雜和流暢的逐字逐行打印效果。

      #import "ViewController.h"
      #import <QuartzCore/QuartzCore.h>
      
      @interface ViewController ()
      
      @property (nonatomic, strong) CATextLayer *textLayer;
      @property (nonatomic, strong) NSString *content;
      
      @end
      
      @implementation ViewController
      
      - (void)viewDidLoad {
          [super viewDidLoad];
      
          self.textLayer = [CATextLayer layer];
          self.textLayer.frame = self.view.bounds;
          self.textLayer.fontSize = 18;
          self.textLayer.alignmentMode = kCAAlignmentLeft;
          self.textLayer.contentsScale = [UIScreen mainScreen].scale;
          self.textLayer.wrapped = YES;
          [self.view.layer addSublayer:self.textLayer];
      
          self.content = @"這是需要逐字逐行打印的文字內容。\n讓我們來實現它。";
      
          [self startPrinting];
      }
      
      - (void)startPrinting {
          self.textLayer.string = @"";
          
          for (NSInteger index = 0; index < self.content.length; index++) {
              dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(index * 0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                  NSString *nextCharacter = [self.content substringWithRange:NSMakeRange(index, 1)];
                  self.textLayer.string = [self.textLayer.string stringByAppendingString:nextCharacter];
              });
          }
      }
      
      @end
      

      Swift版

      1. 基于定時器的逐字打印效果

      可以使用Timer來逐字逐行地顯示文字。

      import UIKit
      
      class ViewController: UIViewController {
          private let textView = UITextView()
          private let content = "這是需要逐字逐行打印的文字內容。\n讓我們來實現它。"
          private var currentIndex = 0
          private var timer: Timer?
      
          override func viewDidLoad() {
              super.viewDidLoad()
              view.addSubview(textView)
              textView.frame = view.bounds
              textView.font = UIFont.systemFont(ofSize: 18)
              textView.isEditable = false
              textView.isScrollEnabled = true
              startPrinting()
          }
      
          private func startPrinting() {
              timer = Timer.scheduledTimer(timeInterval: 0.1, target: self, selector: #selector(printNextCharacter), userInfo: nil, repeats: true)
          }
      
          @objc private func printNextCharacter() {
              guard currentIndex < content.count else {
                  timer?.invalidate()
                  timer = nil
                  return
              }
              
              let nextIndex = content.index(content.startIndex, offsetBy: currentIndex)
              textView.text.append(content[nextIndex])
              currentIndex += 1
          }
      }
      

      2. 使用CADisplayLink來實現高精度逐字打印

      CADisplayLink可以在屏幕刷新時調用指定的方法,相較于Timer,其精度和性能更高。

      import UIKit
      
      class ViewController: UIViewController {
          private let textView = UITextView()
          private let content = "這是需要逐字逐行打印的文字內容。\n讓我們來實現它。"
          private var currentIndex = 0
          private var displayLink: CADisplayLink?
      
          override func viewDidLoad() {
              super.viewDidLoad()
              view.addSubview(textView)
              textView.frame = view.bounds
              textView.font = UIFont.systemFont(ofSize: 18)
              textView.isEditable = false
              textView.isScrollEnabled = true
              startPrinting()
          }
      
          private func startPrinting() {
              displayLink = CADisplayLink(target: self, selector: #selector(printNextCharacter))
              displayLink?.preferredFramesPerSecond = 10  // 控制打印速度
              displayLink?.add(to: .main, forMode: .default)
          }
      
          @objc private func printNextCharacter() {
              guard currentIndex < content.count else {
                  displayLink?.invalidate()
                  displayLink = nil
                  return
              }
              
              let nextIndex = content.index(content.startIndex, offsetBy: currentIndex)
              textView.text.append(content[nextIndex])
              currentIndex += 1
          }
      }
      

      3. CATextLayer + Animation

      還可以使用CATextLayer和動畫來實現更為復雜和流暢的逐字逐行打印效果。

      import UIKit
      
      class ViewController: UIViewController {
          private let textLayer = CATextLayer()
          private let content = "這是需要逐字逐行打印的文字內容。\n讓我們來實現它。"
          
          override func viewDidLoad() {
              super.viewDidLoad()
              
              textLayer.frame = view.bounds
              textLayer.fontSize = 18
              textLayer.alignmentMode = .left
              textLayer.contentsScale = UIScreen.main.scale
              textLayer.isWrapped = true
              view.layer.addSublayer(textLayer)
              
              startPrinting()
          }
          
          private func startPrinting() {
              textLayer.string = ""
              for (index, character) in content.enumerated() {
                  DispatchQueue.main.asyncAfter(deadline: .now() + Double(index) * 0.1) {
                      self.textLayer.string = "\(self.textLayer.string ?? "")\(character)"
                  }
              }
          }
      }
      
      posted @ 2024-08-01 14:32  Mr.陳  閱讀(607)  評論(0)    收藏  舉報
      主站蜘蛛池模板: 国产精品无码无卡在线观看久| 香港特级三A毛片免费观看| 亚洲中文字幕国产综合| 麻花传媒在线观看免费| 国内揄拍国内精品人妻 | 国产一区二区日韩在线| 亚洲a免费| 平遥县| h无码精品动漫在线观看| 成午夜福利人试看120秒| 国产中文字幕在线精品| 男女猛烈激情xx00免费视频| 亚洲产国偷v产偷v自拍色戒| 商水县| 久久亚洲精品中文字幕馆| 国产精品推荐手机在线| 国产第一页浮力影院入口| 久热这里只有精品视频3| 国产成人无码一区二区三区| 99久久国产成人免费网站| 97精品国产91久久久久久久| 午夜成人精品福利网站在线观看 | 亚洲男人第一无码av网站| 亚洲性猛交xxxx| 亚洲国产精品日韩专区av| 久久日韩精品一区二区五区| 国产精品大全中文字幕| 亚洲中少妇久久中文字幕| 精品综合久久久久久97| 浴室人妻的情欲hd三级国产| 国产国拍亚洲精品永久软件| 粗大的内捧猛烈进出小视频| 久久亚洲精品11p| 中文国产成人久久精品小说| 午夜高清福利在线观看| 久久综合九色综合久桃花| 日本伊人色综合网| 亚洲午夜激情久久加勒比| 少妇熟女久久综合网色欲| 曰韩无码av一区二区免费| 久久亚洲中文无码咪咪爱|