【问题标题】:Obj-c - Delayed animation when returning to original state?Obj-c - 返回原始状态时延迟动画?
【发布时间】:2018-07-08 12:57:01
【问题描述】:

当我的键盘被激活时,我正在使用下面的代码来移动一个视图和我的表格视图。然而,当键盘关闭时,upView 在键盘关闭后需要 2 秒才能返回到原来的位置(另一方面,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, 0, movement);
                [UIView setAnimationDidStopSelector:@selector(afterAnimationStops)];
                [UIView commitAnimations];

                self.tableView.frame = CGRectOffset(self.tableView.frame, 0, 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, 0, self.keyboardHeight); self.tableView.frame = CGRectOffset(self.tableView.frame, 0, 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, 0, -self.keyboardHeight); self.tableView.frame = CGRectOffset(self.tableView.frame, 0, -self.keyboardHeight); } completion:^(BOOL finished) {}];


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

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

}

【问题讨论】:

  • 您(某处)是否也在注册UIKeyboardWillHideNotification
  • @DonMag 查看上面编辑过的代码 - 我使用的是 keyboardWillChange 但不是,不是 willHideNotification。
  • 好的——你想添加一个函数来处理“键盘将隐藏”——然后基本上做你为“键盘将显示”所做的相反的事情。那里有很多很多例子。
  • 你能贴出调用animateTextView的代码吗?
  • @Brittany - 从 Apple 的文档开始:developer.apple.com/library/content/documentation/…

标签: ios objective-c xcode nsoperationqueue uianimation


【解决方案1】:

这个问题可能与动画持续时间有关,因此您可以从-(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{

}

【讨论】:

  • 这可以无缝运行,但有一个例外:由于某种原因,当我隐藏我的键盘时,它会执行两次 keyboardWillHideNotification?我在keyboardWillHide中放置了一个日志,控制台显示了它两次(换句话说,我的uiview比必要的移动得更远了?)我已经浏览了我的代码以寻找潜在的重复项,但没有其他内容存在:/
  • @Brittany 请检查您被添加了多少次 UIKeyboardWillHideNotification 通知?
  • 我只有一次 - 也就是说,我确实有 keyboardWillChange 通知以便在我的 viewDidLoad 中获取键盘的高度 - 这会影响它吗?我假设不会,因为它不会“显示”两次?
  • @Brittany 是的,这就是问题所在,你只能使用 UIKeyboardWillHideNotification 和 UIKeyboardWillShowNotification,使用这两个观察者很容易处理键盘和其他 UI 框架的变化。
【解决方案2】:

我认为您需要立即使用线程来调用动画...

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

    dispatch_async(dispatch_get_main_queue(), ^{
        [self animateTextView:YES];
    });
}

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

我不太了解你的问题,但试试这个,因为如果我们遇到这种类型的问题 dispatch_async() 将解决。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-12-16
    • 1970-01-01
    • 2019-01-30
    • 1970-01-01
    • 1970-01-01
    • 2015-08-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多