当键盘动画到一半以完全显示/隐藏时,是否有任何代码/方法可以捕获?在调用 keyboardDidShow 通知之前,当键盘半可见时,我需要执行一些操作。非常感谢任何帮助。提前致谢。
问问题
650 次
1 回答
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];
}
于 2013-08-20T01:36:12.350 回答