当键盘设置为完全显示/隐藏的一半时,是否有任何代码/方法可以捕获?我需要在键盘在调用keyboardDidShow通知之前一半可见时执行一些操作。 任何帮助都非常感谢。 提前谢谢。
答案 0 :(得分:0)
没有内置的方法。但是,正如@Aaron Brager指出的那样,您可以使用UIKeyboardAnimationDurationUserInfoKey
通知的用户信息中的UIKeyboardWillShowNotification
来获取键盘演示动画的持续时间。然后,您所要做的就是将该值除以2并将执行动作延迟该数量。这是一个例子:
- (void)keyBoardWillShow:(NSNotification *)notification
{
NSTimeInterval duration = [self keyboardAnimationDurationForNotification:notification];
double delayInSeconds = duration / 2.0;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
});
}
- (NSTimeInterval)keyboardAnimationDurationForNotification:(NSNotification*)notification
{
NSDictionary *info = [notification userInfo];
NSValue *value = [info objectForKey:UIKeyboardAnimationDurationUserInfoKey];
NSTimeInterval duration = 0;
[value getValue:&duration];
return duration;
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyBoardWillShow:) name:UIKeyboardWillShowNotification object:nil];
}
- (void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
}