单击时设置动画文本框 [英] animating text box when clicked

查看:115
本文介绍了单击时设置动画文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从iPad应用中移动屏幕底部的文本框,以便键盘不会覆盖它。

I am trying to move a text box from the bottom of the screen in an iPad app so that the keyboard does not cover it.

我有以下工作。

-(void) textViewDidBeginEditing:(UITextView *) observationComment
{
observationComment.frame=CGRectMake(190, 100, 700, 250);
}

但是1 - 我想动画动画 - 这可能吗?

But 1 - I would like to animate the movement - is this possible?

2 - 我收到警告

2 - I get a warning

Local declaration of 'observationComment' hides instance variable

任何建议?也许这不是最好的方法。

Any advice? Maybe this is not the best way to do it.

推荐答案

我之前找到了这里的答案,但我不得不添加代码以便当用户通过按键键切换键盘(如国际或表情符号)时,View Controller的view.frame会调整()。

I previously found the answer here, but I had to add code so that the View Controller's view.frame adjusts when the user switches between keyboards (like international or emoji) by pressing the globe key ().

在YourViewController.h中,将键盘的高度(仅当它可见时)存储在一个实例变量中。

In YourViewController.h, store the height of the keyboard (only when it's visible) in an instance variable.

@interface YourViewController : UIViewController {
    CGFloat keyboardHeightIfShowing ;
}

在YourViewController.m中,实现这些方法。

In YourViewController.m, implement these methods.

- (void) keyboardWillShow:(NSNotification*)notification {
    [self moveTextViewForKeyboard:notification up:YES] ;
}

- (void) keyboardWillHide:(NSNotification*)notification {
    [self moveTextViewForKeyboard:notification up:NO] ;
}

- (void) moveTextViewForKeyboard:(NSNotification*)notification up:(BOOL)up {
    NSDictionary* userInfo = [notification userInfo] ;
    UIViewAnimationCurve animationCurve ;
    NSTimeInterval animationDuration ;
    CGRect keyboardEndFrame ;

    [[userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey] getValue:&animationCurve] ;
    [[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] getValue:&animationDuration] ;
    [[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] getValue:&keyboardEndFrame] ;

    [UIView beginAnimations:nil context:nil] ;
    [UIView setAnimationDuration:animationDuration] ;
    [UIView setAnimationCurve:animationCurve] ;

    CGRect keyboardFrame = [self.view convertRect:keyboardEndFrame toView:nil] ;
    //Since I have a UITabBar, when the keyboard appears, self.view.frame.height should shrink by slightly less than if I did not have a UITabBar.
    keyboardFrame.size.height -= self.tabBarController.tabBar.frame.size.height ;
    CGRect newViewFrame = self.view.frame ;
    //UIKeyboardWillShowNotification can be triggered even when the keyboard is already showing, such as when switching between certain international keyboards. When this happens, before shrinking newViewFrame to accommodate keyboardFrame, enlarge newViewFrame so that it is the size it was before the previous keyboard appeared.
    if ( up && keyboardHeightIfShowing ) {
        NSLog(@"hiding keyboard with height %0.1f, showing keyboard with height %0.1f",keyboardHeightIfShowing, keyboardFrame.size.height) ;
        newViewFrame.size.height += keyboardHeightIfShowing ;
    }
    newViewFrame.size.height -= keyboardFrame.size.height * (up ? 1 : -1) ;
    keyboardHeightIfShowing = ( up ? keyboardFrame.size.height : 0 ) ;
    self.view.frame = newViewFrame ;

    [UIView commitAnimations] ;
}

最后,在YourViewController.m中,建立 keyboardWillShow keyboardWillHide 由UIKeyboardWillShow / Hide通知调用。

And finally, in YourViewController.m, establish keyboardWillShow and keyboardWillHide to be called by UIKeyboardWillShow/Hide notifications.

- (void) viewWillAppear:(BOOL)animated {
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil] ;
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil] ;
}

- (void) viewWillDisappear:(BOOL)animated {
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil] ;
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil] ;
}

这篇关于单击时设置动画文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆