2011-02-25 82 views
0

我看到一些非常奇怪的行爲。我有一個文本視圖顯示在一個視圖,這是一個滾動視圖的一部分。如果當我開始在文本視圖中鍵入內容時,鍵盤通常會隱藏該文本視圖,然後對文本視圖進行動畫處理並將其推到鍵盤上方以保持可見。如果我這樣做了,我鍵入的第三行和後續行將不可見,直到我忽略鍵盤時,我才能看到我鍵入的文本。如果當我開始在文本視圖中輸入內容時,我不需要對它進行動畫處理(因爲鍵盤沒有隱藏它),那麼我可以看到我輸入的所有文本,因此我的問題僅出現在文本視圖是動畫的。UITextView在輸入時隱藏文本

下面是我的代碼創建的文本視圖:

self.textView = [[UITextView alloc] initWithFrame:CGRectMake(...)]; 
    textView.font = [UIFont systemFontOfSize:18.0]; 
    textView.backgroundColor = [UIColor whiteColor]; 
    textView.userInteractionEnabled = YES; 
    textView.clipsToBounds = YES; 
    textView.delegate = self; 
    textView.layer.borderWidth = 1; 
    textView.layer.borderColor = [[UIColor blackColor] CGColor]; 
    textView.layer.cornerRadius = 10; 
    [self addSubview:textView]; 
    [textView release]; 

下面是動畫文本視圖代碼:

- (void) makeTextViewVisible: (UITextView *)textArea up:(BOOL) up { 

if (up) { 
    animatedDistance = 0; 

    CGPoint myPoint = [textArea.superview convertPoint:textArea.frame.origin toView:[UIApplication sharedApplication].keyWindow]; 

    UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation]; 
    if (orientation == UIInterfaceOrientationPortrait) { 
     animatedDistance = PORTRAIT_KEYBOARD_HEIGHT - (950 - myPoint.y) + 150; 
    } 
    else if (orientation == UIInterfaceOrientationPortraitUpsideDown) { 
     animatedDistance = PORTRAIT_KEYBOARD_HEIGHT + (PORTRAIT_KEYBOARD_HEIGHT - myPoint.y); 
    } 
    else if (orientation == UIInterfaceOrientationLandscapeLeft) { 
     animatedDistance = myPoint.x - LANDSCAPE_KEYBOARD_HEIGHT + 120; 
    } 
    else if (orientation == UIInterfaceOrientationLandscapeRight) { 
     animatedDistance = LANDSCAPE_KEYBOARD_HEIGHT - myPoint.x + 100; 
    } 

    if (animatedDistance < 0) { 
     animatedDistance = 0.0; 
    } 

    CGRect viewFrame = textArea.frame; 
    viewFrame.origin.y -= animatedDistance; 

    [UIView beginAnimations:nil context:NULL]; 
    [UIView setAnimationBeginsFromCurrentState:YES]; 
    [UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION]; 

    [textArea setFrame:viewFrame]; 

    [UIView commitAnimations]; 

} 
else { 
    CGRect viewFrame = textArea.frame; 
    viewFrame.origin.y += animatedDistance; 

    [UIView beginAnimations:nil context:NULL]; 
    [UIView setAnimationBeginsFromCurrentState:YES]; 
    [UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION]; 

    [textArea setFrame:viewFrame]; 

    [UIView commitAnimations];  
} 

我打過電話setNeedsDisplay/setNeedsLayout內textViewDidChange,但沒有運氣。

以前有人遇到過這個問題,或者知道如何解決它?

謝謝!

回答

1

對你有幾點想法。首先,我認爲你在animatedDistance計算中工作太辛苦。我通常創建主視圖,而不是使用UIWindow的尺寸(不隨設備旋轉)。否則,正如您發現的,您必須考慮狀態欄,電話狀態欄等。使用視圖可以將所有這些計算減少到一個(僅補償兩種不同的鍵盤高度):

if ((orientation == UIInterfaceOrientationLandscapeLeft)|| (orientation == UIInterfaceOrientationLandscapeRight)) { 
    keyboardHeight = LANDSCAPE_KEYBOARD_HEIGHT; 
} else { 
    keyBoardHeight = PORTRAIT_KEYBOARD_HEIGHT; 
} 

CGPoint myPoint = [self.view convertPoint:textArea.frame.origin toView:self.view]; 
float windowHeight = self.view.bounds.size.height; 

animatedDistance = myPoint.y+textArea.bounds.size.height+keyboardHeight-windowHeight; 

if (animatedDistance < 0) { 
    animatedDistance = 0.0; 
} 

因此,使用視圖的邊界可以獲得所需的信息。

其次,爲什麼要移動textView?爲什麼不只是調整滾動窗口的scrollPos?

第三,你指的是在一個視圖內的scrollView中的textView(儘管我沒有在代碼中看到它)。有可能你的textView被中間視圖的邊界截斷,這將導致能夠輸入它,但無法看到結果。你能看到你的textView的邊界嗎?

最後,作爲一個美學觀點,animatedDistance是一個狀態變量,您應該在您將動畫放回之後將animatedDistance設置爲零。這樣,如果你調用了makeTextViewVisible:textView UP:由於某種原因兩次false,它不會讓你感到困惑。

+0

非常有用的觀點。我喜歡調整滾動位置的第二個想法。 – 2011-02-28 12:31:23

相關問題