微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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{

}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐