简体   繁体   English

如何加快对UITextView的更新?

[英]How can I speed up updates to UITextView?

I have a calculator app with an “accounting tape” made of UITextView . 我有一个计算器应用程序,带有由UITextView制成的“会计胶带”。 Once there's around 50–100 lines on the tape, the text view gets quite slow to update – setting new contents (50–100 lines of text) easily takes 100 ms or more. 磁带上大约有50–100行时,文本视图的更新速度将非常慢–设置新内容(50–100行文本)很容易会花费100毫秒或更长时间。 The problem is that I can't move this delay to the background, since UIKit insists on being called on the main thread. 问题是我不能将这种延迟移到后台,因为UIKit坚持要在主线程上调用。

What can I do? 我能做什么? About the only thing that comes to mind is rewriting the tape using a custom UIView subclass that would be better suited to the task, but I'm not very eager to do that as it's a lot of work and the UITextView works fine apart from this problem. 唯一想到的是使用更适合该任务的自定义UIView子类重写磁带,但是我并不急于这样做,因为这需要大量工作,而且UITextView可以很好地工作问题。

I'm very surprised that you're having a problem when only dealing with 50 lines, so I frankly wonder if there's something else going on here. 您仅处理50条线路时遇到问题,我感到非常惊讶,因此我坦率地想知道这里是否还有其他问题。 I'd run the code through the "Time Profiler" in Instruments and confirm that the problem really is the updating of the UITextView and not something else. 我将通过Instruments中的“ Time Profiler”运行代码,并确认问题确实是UITextView的更新,而不是其他问题。 A little research here might be warranted before giving up on UITextView , 在放弃UITextView之前,可能需要进行一些研究,

But let's assume for a second that you decided that you need to start over (which I find unlikely) and not use UITextView . 但是让我们假设您决定需要重新开始(我认为不太可能)并且不使用UITextView I might suggest leveraging existing control, eg a UITableView with no separator between cells or, if worst came to worst, a UIScrollView where each line on the tape is a UILabel and you do a UITableView -style dequeuing of labels that have scrolled off the screen. 我可能建议利用现有的控件,例如,一个UITableView在单元格之间没有分隔符,或者,如果最糟的话,则是一个UIScrollView ,其中磁带上的每一行都是一个UILabel ,您可以进行UITableView样式的将标签从屏幕上滚动出来的出队。 A bunch of options that are a little better than starting from scratch. 一堆选项比从头开始要好一些。

Assuming it's really the UITextView that is slow and not something else, I would suggest you alter the text of the textview to only that part, which is visible. 假设确实是UITextView较慢而不是其他原因,我建议您将textview的文本更改为仅可见的那一部分。 All the text that has scrolled outside doesn't need to be kept in the textview. 滚动到外部的所有文本都不需要保留在textview中。 Of course you would have to implement your own scrolling then if you need that or use blank lines for the removed ones so the scrolling behavior keeps intact. 当然,如果需要,您将必须实现自己的滚动,或者对已删除的行使用空白行,以使滚动行为保持完整。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM