键盘隐藏文本视图

时间:2011-01-29 05:24:42

标签: iphone

我将一个tableview,textview,按钮放在视图中like this

当我点击textview键盘时隐藏textview.To动画textview我正在编写代码作为休闲。

- (void) textViewDidBeginEditing:(UITextView *)textView {
    [self animateTextField:textView up:YES];
}

- (void) textViewDidEndEditing:(UITextView *)textView {
    [self animateTextField:textView up:NO];
}

- (void) animateTextField: (UITextView*) textView up: (BOOL) up {
    const int movementDistance = 80; // tweak as needed
    const float movementDuration = 0.3f; // tweak as needed

    int movement = (up ? -movementDistance : movementDistance);

    [UIView beginAnimations: @"anim" context: nil];
    [UIView setAnimationBeginsFromCurrentState: YES];
    [UIView setAnimationDuration: movementDuration];
    self.view.frame = CGRectOffset(self.view.frame, 0, movement);
    [UIView commitAnimations];
}

但它没有任何效果。

要隐藏键盘,我正在编写此代码

-(void) touchesBegan :(NSSet *) touches withEvent:(UIEvent *)event {
    [mytextview resignFirstResponder];
}

它也没有任何效果。

任何人都可以帮助我。

提前感谢你。

2 个答案:

答案 0 :(得分:0)

我没有机会看到你观看,因为我的浏览器说“图像不能显示,它包含错误。”

请尝试以下选项。

Chk是否已将textField的委托设置为文件所有者。如果您已设置但仍无法正常工作,请尝试以下操作。就像在textedits上上下动画整个视图一样。

请使用下面的代码。这对我有用。你要做的就是双击要添加文本字段的.xib文件。视图以及包含文件所有者,第一响应者和a的窗口视图也将被打开。

使用cmd + shift + l打开库并添加另一个视图。在.h文件中,为视图添加IBOutlet作为IBOutlet UIView * secondview并映射到文件的所有者。

现在在第二个视图中放置要移动的文本字段。现在打开.xib文件时将出现的默认视图将不包含任何内容。它将为空白,您必须显示的控件将位于第二种观点。

在.m文件中,将第二个视图添加到主视图中

[self.view addSubview:secondView];

并根据需要自定义文本字段名称来使用此代码。

- (BOOL)textFieldShouldReturn:(UITextField *)textField 
{
    CGRect viewFrame = self.view.frame;
    if(textField==textfield1)
    {
    [textfield1 resignFirstResponder];
    [textfield2 becomeFirstResponder];
    }
    else if(textField== textfield2)
    {
        [textfield2 resignFirstResponder];
        [textfield3 becomeFirstResponder];
    }
    else if(textField== textfield3)
    {
        [textfield3 resignFirstResponder];  
        [textfield4 becomeFirstResponder];

    }
    else if(textField== textfield4)
    {
        [textfield4 resignFirstResponder];

    }
    return YES;
}
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
        CGRect viewFrame = secondView.frame;

        CGRect textFieldRect =[secondView.window convertRect:textField.bounds fromView:textField];
        CGRect viewRect =[secondView.window convertRect:secondView.bounds fromView:secondView];
        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  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);
        }

        viewFrame.origin.y -= animatedDistance;

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

        [secondView setFrame:viewFrame];

        [UIView commitAnimations];

}


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

    CGRect viewFrame = secondView.frame;
    viewFrame.origin.y +=animatedDistance;

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

    [secondView setFrame:viewFrame];


    [UIView commitAnimations];

}

让我知道这是否有效。

答案 1 :(得分:0)

退出键盘;

使用条形按钮创建一个工具栏,并为此添加一个IBAction,并将其y坐标设置为480(屏幕高度)

-(IBAction)hide
{
  [mytextview resignFirstResponder];

}

并在viewWillAppear中使用

myTextView.inputAccessoryView=tlBar;

And add two notification in viewWillAppear for keyboard

NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];

    [nc addObserver:self selector:@selector(keyboardWillShow:) name: UIKeyboardWillShowNotification object:nil];
    [nc addObserver:self selector:@selector(keyboardWillHide:) name: UIKeyboardWillHideNotification object:nil];


and then use these methods for Animation

-(void) keyboardWillShow:(NSNotification *) note
{
    if(isAnimate)
    [self viewAnimateUpper];
}

-(void)viewAnimateUpper
{

        [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationDuration:.3];

        CGRect viewFrame=self.view.frame;

        viewFrame.origin.y -=95;//according to you
        self.view.frame=viewFrame;
        [UIView commitAnimations];


}

-(void)viewAnimatedown
{

        [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationDuration:.3];

        CGRect viewFrame=self.view.frame;

        viewFrame.origin.y +=95;//according to you
        self.view.frame=viewFrame;

        [UIView commitAnimations];

}
-(void) keyboardWillHide:(NSNotification *) note
{
        if(isAnimate)
              {
    [self viewAnimatedown];
     }
}

修改

使用textviewShouldBegainEditing

如果你想要的textView调用这个方法

,在这个集合中bool变量为yes

isAnimate = YES;