2012-10-27 91 views
0

嘗試在UITextView中慢慢顯示文本,如舊的電傳打字。很好的工作,直到滾動視圖,在這一點上,它應該是一個平滑的行,向上滾動。但只是顫抖,並在一個班次上升。下面的代碼段,不確定UI刷新和定時器是否是問題?在UITextView中滾動電傳文本顯示不滾動

- (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]; 
} 

回答

0

工作了使用塊動畫

// Scroll through text, one char at a time with delay to simulate teletype type display 
// 
NSTimeInterval nextDelay = 0; 
NSTimeInterval stepDelay = 0.03; 

if (dispLocationDetails.text.length > 0) 
    [self performSelector: @selector(addCharToLocationDisplay:) withObject: @"\n" afterDelay: nextDelay]; 

for (NSString *nextMessage in inMessageList) 
{ 
    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]; 
} 

而且

- (void) addCharToLocationDisplay: (NSString *) nextChar 
{ 
    // 
    // Add one char at a time to the text view display 
    // Scroll up line by line as text displayed 
    // 

    NSString *newText    = [NSString stringWithFormat: @"%@%@", dispLocationDetails.text, nextChar]; 
    self.dispLocationDetails.text = newText; 

    [UIView animateWithDuration:.25 animations:^ 
    { 
     CGPoint bottomOffset = CGPointMake(0, dispLocationDetails.contentSize.height - dispLocationDetails.bounds.size.height); 
     DLog(@"Bottom: %f", bottomOffset.y); 
     if (bottomOffset.y > 0) 
      [self.dispLocationDetails setContentOffset: bottomOffset animated: NO]; 
    }]; 
}