UITextView和键盘

时间:2014-07-31 16:21:17

标签: objective-c ios7 uitextview

我在同一个视图控制器上有UITextFields和UIText Views。我用这个:

-(void)textFieldDidBeginEditing:(UITextField *)textField
{

    CGRect textFieldRect = [self.view.window convertRect:textField.bounds fromView:textField];
    CGRect viewRect = [self.view.window convertRect:self.view.bounds fromView:self.view];

    CGFloat midLine = textFieldRect.origin.y + 0.5 * textFieldRect.size.height;
    CGFloat numerator = midLine - viewRect.origin.y - MINIMUM_SCROLL_FRACTION *viewRect.size.height;
    CGFloat denominator = (MAXIMUM_SCROLL_FRACTION - MINIMUM_SCROLL_FRACTION) *viewRect.size.height;
    CGFloat heightFraction = numerator / denominator;

    if (heightFraction < 0.0) {
        heightFraction = 0.0;
    }
    else if (heightFraction > 1.0)
    {
        heightFraction = 1.0;
    }


    UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
    if (orientation == UIInterfaceOrientationPortrait || orientation == UIInterfaceOrientationPortraitUpsideDown) {
        animatedDistance = floor(PORTRAIT_KEYBOARD_HEIGHT *heightFraction);
    }
    else
    {
        animatedDistance = floor(LANDSCAPE_KEYBOARD_HEIGHT *heightFraction);
    }


    CGRect viewFrame = self.view.frame;
    viewFrame.origin.y -= animatedDistance;

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

    [self.view setFrame:viewFrame];

    [UIView commitAnimations];
}

-(void)textFieldDidEndEditing:(UITextField *)textField
{

    CGRect viewFrame = self.view.frame;
    viewFrame.origin.y += animatedDistance;

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

    [self.view setFrame:viewFrame];

    [UIView commitAnimations];
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField
{

    [textField resignFirstResponder];
    return YES;
}

处理键盘出现和消失的工作,并在键入时保持文本字段可见。我尝试在方法声明和代码中添加UITextView,但没有成功。任何想法我需要做什么或做错了什么?另外,我在我的代码中声明了我的const,上面的代码适用于UITextFields。

2 个答案:

答案 0 :(得分:1)

实施UITextViewDelegate方法以了解键盘何时出现并移动内容。

– textViewDidBeginEditing:
– textViewDidEndEditing:

目前您只是实施UITextFieldDelegate方法。这就是为什么它适用于文本字段而不适用于文本视图。

答案 1 :(得分:0)

如果你想在屏幕上移动元素并在显示/隐藏键盘时为它们设置动画,你最好不要听取发送的NSNotifications。

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardShown:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDismissed:) name:UIKeyboardWillHideNotification object:nil];

通过使用NSNotificationCenter,您可以获得文本字段委托方法中不可用的信息,如键盘框架大小,最重要的是(在我看来)文本字段动画的时间。作为参考,UIInfo文档中列出了userInfo字典的键。

- (void)keyboardShown:(NSNotification *)note
{
    NSDictionary *info = [note userInfo];
    NSNumber *duration = [info objectForKey:UIKeyboardAnimationDurationUserInfoKey];

    //do you animations here
}