【问题标题】:animating text box when clicked单击时动画文本框
【发布时间】:2012-12-31 19:31:46
【问题描述】:

我试图从 iPad 应用程序的屏幕底部移动一个文本框,以便键盘不会覆盖它。

我有以下工作。

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

但是 1 - 我想为运动设置动画 - 这可能吗?

2 - 我收到警告

Local declaration of 'observationComment' hides instance variable

有什么建议吗?也许这不是最好的方法。

【问题讨论】:

    标签: ios6 uitextfield


    【解决方案1】:

    我之前找到了the answer here,但我必须添加代码,以便当用户通过按下地球键 () 在键盘(如国际或表情符号)之间切换时,视图控制器的 view.frame 进行调整。

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

    @interface YourViewController : UIViewController {
        CGFloat keyboardHeightIfShowing ;
    }
    

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

    - (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 中,建立 keyboardWillShowkeyboardWillHide 以供 UIKeyboardWillShow/Hide 通知调用。

    - (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] ;
    }
    

    【讨论】:

      【解决方案2】:

      看看this tutorialthis stackoverflow.com question and its answers。这是一个复杂的问题,但这些应该能让您更好地了解您需要做什么。

      【讨论】:

        猜你喜欢
        • 2015-11-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多