在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)"
}
}
}
}

iOS开发基础144-逐字打印效果的更多相关文章

  1. IOS开发基础知识碎片-导航

    1:IOS开发基础知识--碎片1 a:NSString与NSInteger的互换 b:Objective-c中集合里面不能存放基础类型,比如int string float等,只能把它们转化成对象才可 ...

  2. iOS开发——总结篇&IOS开发基础知识

    IOS开发基础知识 1:Objective-C语法之动态类型(isKindOfClass, isMemberOfClass,id) 对象在运行时获取其类型的能力称为内省.内省可以有多种方法实现. 判断 ...

  3. IOS开发基础环境搭建

    一.目的 本文的目的是windows下IOS开发基础环境搭建做了对应的介绍,大家可根据文档步骤进行mac环境部署: 二.安装虚拟机 下载虚拟机安装文件绿色版,点击如下文件安装 获取安装包:       ...

  4. iOS开发基础-九宫格坐标(6)

    继续对iOS开发基础-九宫格坐标(5)中的代码进行优化. 优化思路:把字典转模型部分的数据处理操作也拿到模型类中去实现,即将 ViewController 类实现中 apps 方法搬到 WJQAppI ...

  5. iOS开发基础-九宫格坐标(5)

    继续在iOS开发基础-九宫格坐标(4)的基础上进行优化. 一.改进思路 1)iOS开发基础-九宫格坐标(4)中 viewDidLoad 方法中的第21.22行对控件属性的设置能否拿到视图类 WJQAp ...

  6. iOS开发基础-九宫格坐标(4)

    对iOS开发基础-九宫格坐标(3)的代码进行进一步优化. 新建一个 UIView 的子类,并命名为 WJQAppView ,将 appxib.xib 中的 UIView 对象与新建的视图类进行关联. ...

  7. iOS开发基础-九宫格坐标(3)之Xib

    延续iOS开发基础-九宫格坐标(2)的内容,对其进行部分修改. 本部分采用 Xib 文件来创建用于显示图片的 UIView 对象. 一.简单介绍  Xib 和 storyboard 的比较: 1) X ...

  8. iOS开发基础-九宫格坐标(2)之模型

    在iOS开发基础-九宫格(1)中,属性变量 apps 是从plist文件中加载数据的,在 viewDidLoad 方法中的第20行.26行中,直接通过字典的键名来获取相应的信息,使得 ViewCont ...

  9. iOS开发基础-图片切换(4)之懒加载

    延续:iOS开发基础-图片切换(3),对(3)里面的代码用懒加载进行改善. 一.懒加载基本内容 懒加载(延迟加载):即在需要的时候才加载,修改属性的 getter 方法. 注意:懒加载时一定要先判断该 ...

  10. iOS开发基础-图片切换(3)之属性列表

    延续:iOS开发基础-图片切换(2),对(2)里面的代码用属性列表plist进行改善. 新建 Property List 命名为 Data 获得一个后缀为 .plist 的文件. 按如图修改刚创建的文 ...

随机推荐

  1. flutter 打包web应用指定上下文

    使用flutter build web命令打包的应用不包含上下文,只能部署在根目录.如何指定上下文,部署在子目录下呢? 有两种办法: 1.修改web/index.html文件 修改 <base ...

  2. 使用jasypt对springboot配置信息加密

    1.pom文件增加依赖 <dependency> <groupId>com.github.ulisesbocchio</groupId> <artifactI ...

  3. v-for 伪标签:template:

    根据一组数组或对象的选项列表进行渲染. v-for指令需要使用 (item,index) in 数组或对象 形式的特殊语法,同时还需要指定key值,key的作用在vue进行新旧数据比对渲染页面里,如果 ...

  4. LeetCode 460. LFU Cache LFU缓存 (C++/Java)

    题目: Design and implement a data structure for Least Frequently Used (LFU)cache. It should support th ...

  5. 让Easysearch运行在Kylin V10 (Lance)-aarch64上

    简介 本文主要介绍在国产操作系统 Kylin V10 (Lance)-aarch64 上安装单机版 Easysearch/Console/Agent/Gateway/Loadgen 系统配置 在安装之 ...

  6. windows 命令行调整跃点数

    先用 route print -4 命令找到接口号: 接口列表10...00 ff 51 c4 53 b4 ......TAP-Windows Adapter V918...18 c0 4d 29 5 ...

  7. python 二次封装logging,导致日志输出的filename错误及优化封装

    问题 封装logging文件名称为:A.py 调用A模块的文件名称为:B.py 二次封装了logging日志模块,根据需要,传入level,判断等级,调用logging模块的info.debug等日志 ...

  8. Android自动化无障碍服务开源库-Assists v3.0.0

    Assists v3.0.0 Android无障碍服务(AccessibilityService)开发框架,快速开发复杂自动化任务.远程协助.监听等 Android无障碍服务能做什么 利用Androi ...

  9. 《最新出炉》系列入门篇-Python+Playwright自动化测试-52- 字符串操作 - 下篇

    1.简介 在日常的自动化测试工作中进行断言的时候,我们可能经常遇到的场景.从一个字符串中找出一组数字或者其中的某些关键字,而不是将这一串字符串作为结果进行断言.这个时候就需要我们对字符串进行操作,宏哥 ...

  10. Linux 使用 Swap分区

    Linux 使用 Swap分区 背景 买的云服务器在使用的时候,资源经常不够,因此需要使用swap分区. Swap分区在系统的物理内存不够用的时候,把硬盘内存中的一部分空间释放出来,以供当前运行的程序 ...