【发布时间】:2011-02-25 16:33:28
【问题描述】:
我看到一些非常奇怪的行为。我在一个视图中显示了一个文本视图,该视图是滚动视图的一部分。如果当我开始在文本视图中输入时,键盘通常会隐藏该文本视图,那么我会为文本视图设置动画并将其推到键盘上方以使其保持可见。如果我这样做,我键入的第三行和后续行将不可见,直到我关闭键盘,此时我可以看到我键入的文本。如果,当我开始在文本视图中输入时,我不需要动画它来推动它(因为键盘不会隐藏它),那么我可以看到我输入的所有文本,因此我的问题只出现在文本视图是动画的。
下面是我创建文本视图的代码:
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];
}
我尝试在 textViewDidChange 中调用 setNeedsDisplay/setNeedsLayout,但没有成功。
有人遇到过这个问题,或者知道如何解决吗?
谢谢!
【问题讨论】:
标签: iphone keyboard uitextview