我正在开发终端仿真应用程序以通过 telnet 连接到我的 unix 服务器,我正在使用 tableview(一个单元格用于一个文本行)。令我困扰的是,当服务器发送大量文本数据以响应某些命令时,这会使应用程序对触摸屏幕上的任何按钮或任何手势的响应速度降低(延迟),因为绘图位于主线程中,除非它允许其他进程工作完成的。
什么应该是解决此问题的完美方法,我是否需要 OperationQueue,我对此了解不多。
我需要一些东西,比如在绘图时,我可以与应用程序顺利交互(这可以暂停绘图)。
我认为drawRect需要更长的时间,实际上我在做的不是reloadData或ReloadTableCell,我在tableview的文本行单元格中有一个可变的属性字符串,当需要将文本添加到该单元格时,我只是替换该可变属性字符串中的字符并为调用drawRect的单元格调用setNeedsDisplay。在 drawRect 我的代码如下 -
-(void)drawRect:(CGRect)rect
{
_cursorView.frame = CGRectMake(([_delegate cursorXInTextLine:self] - 1 )* 6, 0, 6, 10);
[self drawText:0 yPosition:0 canvasWidth:self.bounds.size.width canvasHeight:10];
}
- (void)drawText:(CGFloat)xPosition yPosition:(CGFloat)yPosition canvasWidth:(CGFloat)canvasWidth canvasHeight:(CGFloat)canvasHeight
{
//Draw Text
CGRect textRect = CGRectMake(xPosition, yPosition, canvasWidth, canvasHeight);
[self.textLineText drawInRect: textRect];
}
执行 textReplacement 的其他方法如下 -
-(void)initializeWithStringChar:(NSString*)charString
{
NSInteger len = charString.length;
while (len > 0) {
//length can be displayed.
NSInteger cutLength = totalCols - cursorX + 1;
if (len < cutLength) {
cutLength = len;
}
NSString *str = [charString substringToIndex:cutLength];
[self placeTextInCurrentTextLine:str :YES :YES :YES];
charString = [charString substringFromIndex:cutLength];
len -= cutLength;
}
}
-(void)placeTextInCurrentTextLine:(NSString *)text :(BOOL)needsToReplace :(BOOL)useCurrentCharAttrs :(BOOL)adjustCursor{
TextLineCell *textLineCell = (TextLineCell *)[self.telnet_TableView cellForRowAtIndexPath:currentLineIndexPath];
if (cursorX > totalCols) {
if (self.autowrap == YES) {
[self goNewLineAndResetCursor:YES];
textLineCell = (TextLineCell *)[self.telnet_TableView cellForRowAtIndexPath:currentLineIndexPath];
}
else
cursorX--;
}
NSAttributedString *attrText;
if (useCurrentCharAttrs) {
//Use current chars attributes
attrText = [[NSAttributedString alloc]initWithString:text attributes:currentCharAttrDict];
}
else{
//Use default chars attributes
attrText = [[NSAttributedString alloc]initWithString:text attributes:defaultCharAttrDict];
}
NSMutableAttributedString *attrLineText = [telnet_ScreenData objectAtIndex:currentLineIndexPath.row];
if (needsToReplace) {
//Replace chars with existing
[attrLineText replaceCharactersInRange:NSMakeRange(cursorX - 1, text.length) withAttributedString:attrText];
}
else{
//Insert and shift chars
[attrLineText insertAttributedString:attrText atIndex:cursorX - 1];
//shift out the last characters
attrLineText = [[NSMutableAttributedString alloc]initWithAttributedString:[attrLineText attributedSubstringFromRange:NSMakeRange(0, totalCols)]];
}
[telnet_ScreenData replaceObjectAtIndex:currentLineIndexPath.row withObject:attrLineText];
if (adjustCursor) {
cursorX = cursorX + (int)text.length;
}
if (textLineCell) {
textLineCell.textLineText = attrLineText;
[textLineCell setNeedsDisplay];
}
else{
[_telnet_TableView scrollToRowAtIndexPath:homeIndexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];
}
}