2011-02-25 70 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