ios – Obj-c – 返回原始状态时延迟动画?

当我的键盘被激活时,我正在使用以下代码来移动视图和我的tableview.然而,当键盘关闭时,在键盘关闭后2秒内将upView固定为实时(另一方面,tableView是即时的).为什么会这样?

- (void)viewDidLoad {
            [super viewDidLoad];

           [[NSNotificationCenter defaultCenter] addobserver:self selector:@selector(keyboardWillChange:) name:UIKeyboardWillShowNotification object:nil];


        }

        - (void)keyboardWillChange:(NSNotification *)notification {


            NSDictionary* keyboardInfo = [notification userInfo];

            NSValue* keyboardFrameBegin = [keyboardInfo valueForKey:UIKeyboardFrameEndUserInfoKey];

            CGRect keyboardFrameBeginRect = [keyboardFrameBegin CGRectValue];

            UITabBarController *tabBarController = [UITabBarController new];
            CGFloat tabBarHeight = tabBarController.tabBar.frame.size.height;


            self.keyboardHeight = keyboardFrameBeginRect.size.height - tabBarHeight;

        }


        - (void) animateTextView:(BOOL) up
         {

                const int movementdistance = self.keyboardHeight;

                const float movementDuration = 0.2f;
                int movement= movement = (up ? -movementdistance : movementdistance);


                [UIView beginAnimations: @"anim" context: nil];
                [UIView setAnimationBeginsFromCurrentState: YES];
                [UIView setAnimationDuration: movementDuration];

                self.upView.frame = CGRectOffset(self.upView.frame,movement);
                [UIView setAnimationDidStopSelector:@selector(afteranimationStops)];
                [UIView commitAnimations];

                self.tableView.frame = CGRectOffset(self.tableView.frame,movement);
                [UIView setAnimationDidStopSelector:@selector(afteranimationStops)];
                [UIView commitAnimations];

        }

    - (void)textViewDidBeginEditing:(UITextView *)textView
    {

     [self animateTextView:YES];

    }

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

更新的代码

.M

- (void)handleKeyboard:(NSNotification*)aNotification{
    NSDictionary* info = [aNotification userInfo];
    NSValue* value = [info objectForKey:UIKeyboardAnimationDurationUserInfoKey];
    NSTimeInterval duration = 3;
    [value getValue:&duration];
    if (aNotification.name == UIKeyboardWillHideNotification) {
        /** KEYBOARD HIDE **/

       [UIView animateWithDuration:0 animations:^{ self.upView.frame = CGRectOffset(self.upView.frame,self.keyboardHeight); self.tableView.frame = CGRectOffset(self.tableView.frame,self.keyboardHeight); } completion:^(BOOL finished) {}];


        [self moveCustomView:NO duration:duration];
        NSLog(@"CLOSED!");
    }

    if (aNotification.name == UIKeyboardWillShowNotification) {
        /** KEYBOARD SHOW **/

 [UIView animateWithDuration:0 animations:^{ self.upView.frame = CGRectOffset(self.upView.frame,-self.keyboardHeight); self.tableView.frame = CGRectOffset(self.tableView.frame,-self.keyboardHeight); } completion:^(BOOL finished) {}];


        [self moveCustomView:YES duration:duration];
    }
}

- (void)moveCustomView:(BOOL)move duration:(NSTimeInterval)time{

}

解决方法

此问题可能与动画持续时间有关,因此您可以从 – (void)handleKeyboard:(NSNotification *)notification {}获取键盘显示和隐藏动画持续时间

并处理在同一个函数显示和隐藏自定义视图.将以下代码添加到viewDidLoad函数

[[NSNotificationCenter defaultCenter] addobserver:self selector:@selector(handleKeyboard:) name:UIKeyboardWillHideNotification object:nil];
[[NSNotificationCenter defaultCenter] addobserver:self selector:@selector(handleKeyboard:) name:UIKeyboardWillShowNotification object:nil];

处理键盘操作和UI更改

- (void)handleKeyboard:(NSNotification*)aNotification{
    NSDictionary* info = [aNotification userInfo];
    NSValue* value = [info objectForKey:UIKeyboardAnimationDurationUserInfoKey];
    NSTimeInterval duration = 0;
    [value getValue:&duration];
    if (aNotification.name == UIKeyboardWillHideNotification) {
        /** KEYBOARD HIDE **/

        //calculate your view frames and handle UI changes
        /*
         .
         .
         .
         .
         .
         */
        [self moveCustomView:NO duration:duration];
    }

    if (aNotification.name == UIKeyboardWillShowNotification) {
        /** KEYBOARD SHOW **/

        //calculate your view frames and handle UI changes
        /*
         .
         .
         .
         .
         .
         */
        [self moveCustomView:YES duration:duration];
    }
}

- (void)moveCustomView:(BOOL)move duration:(NSTimeInterval)time{

}

相关文章

UITabBarController 是 iOS 中用于管理和显示选项卡界面的一...
UITableView的重用机制避免了频繁创建和销毁单元格的开销,使...
Objective-C中,类的实例变量(instance variables)和属性(...
从内存管理的角度来看,block可以作为方法的传入参数是因为b...
WKWebView 是 iOS 开发中用于显示网页内容的组件,它是在 iO...
OC中常用的多线程编程技术: 1. NSThread NSThread是Objecti...