尝试在 UITextView 中像旧电传打字机一样缓慢显示文本。效果很好,直到滚动出视图,此时它应该是逐行向上滚动的平滑线。但只是颤抖并在一个班次中上升。下面的片段,不确定 UI 刷新和计时器是否有问题?
- (void) refreshLocationDescriptionWith: (NSArray *) inMessageList
{
//
// Refresh the location textview by appending new array of string messages
//
// Trim old description by half to prevent it growing too long
//
int const kMaxTextSize = 2000;
NSString *oldDescription;
if ([dispLocationDetails.text length] > kMaxTextSize)
{
NSString *trimDescription = [dispLocationDetails.text substringFromIndex: kMaxTextSize / 2];
oldDescription = [NSString stringWithFormat: @"....... %@", trimDescription];
}
else
oldDescription = dispLocationDetails.text;
// Disable keyboard entry whilst displaying
[self disableKeyBoardEntry];
// Go through all messages and display 'slowly'
//
NSTimeInterval nextDelay = 0;
NSTimeInterval stepDelay = 0.03;
for (NSString *nextMessage in inMessageList)
{
// Add to existing text display with one character at a time, with delay to 'scroll' text
for (NSRange stringRange = NSMakeRange(0, 1); stringRange.location < [nextMessage length]; stringRange.location += 1)
{
NSString *nextChar = [nextMessage substringWithRange: stringRange];
[self performSelector: @selector(addCharToLocationDisplay:) withObject: nextChar afterDelay: nextDelay];
nextDelay += stepDelay;
}
[self performSelector: @selector(addCharToLocationDisplay:) withObject: @"\n" afterDelay: nextDelay];
}
[self performSelector: @selector(addCharToLocationDisplay:) withObject: @"\n" afterDelay: nextDelay];
// Re-enable keyboard entry
[self performSelector: @selector(enableKeyBoardEntry) withObject: nil afterDelay: nextDelay];
}
- (void) addCharToLocationDisplay: (NSString *) nextChar
{
//
// Add one char at a time to the text view display
//
NSString *newText = [NSString stringWithFormat: @"%@%@", dispLocationDetails.text, nextChar];
self.dispLocationDetails.text = newText;
NSRange range = NSMakeRange(dispLocationDetails.text.length - 1, 1);
[dispLocationDetails scrollRangeToVisible:range];
}