谁能解释如何最好地模拟 iOS 5 的 Messages.app 显示中的“辅助 View ”(用引号括起来,因为我实际上并不认为它是辅助 View )的行为,因为我想要一个给人印象的 View 它固定在键盘的顶部,但在键盘关闭时仍保留在屏幕上。
最佳答案
它可能是一个单独的 View ,它使用与键盘动画具有相同持续时间的动画重新定位。
尝试观察 UIKeyboardWillShowNotification 和 UIKeyboardWillHideNotification 并在您的处理程序中获取键盘的动画持续时间和帧并启动您自己的动画以重新定位 View ,使其看起来与键盘一起移动。我使用的一些类似代码如下:
- (void)registerKeyboardNotifications {
// Register for keyboard notifications
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillHide:)
name:UIKeyboardWillHideNotification
object:nil];
}
- (void)unregisterKeyboardNotifications {
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIKeyboardWillShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIKeyboardWillHideNotification
object:nil];
}
- (void)keyboardWillShow:(NSNotification*)aNotification {
NSDictionary* info = [aNotification userInfo];
CGRect kbFrameBeginFrame = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue];
CGRect kbFrameEndFrame = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
NSTimeInterval animDuration = [[info objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
UIViewAnimationCurve animCurve = [[info objectForKey:UIKeyboardAnimationCurveUserInfoKey] intValue];
NSLog(@"\nFrame Begin = %@\nFrame End = %@\nAnimation Duration = %f\nAnimation Curve = %i",
NSStringFromCGRect(kbFrameBeginFrame), NSStringFromCGRect(kbFrameEndFrame), animDuration, animCurve);
_showKeyboard = YES;
[self adjustUIForKeyboard:kbFrameEndFrame.size animDuration:animDuration];
}
- (void)keyboardWillHide:(NSNotification*)aNotification {
NSDictionary* info = [aNotification userInfo];
CGRect kbFrameBeginFrame = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue];
CGRect kbFrameEndFrame = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
NSTimeInterval animDuration = [[info objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
UIViewAnimationCurve animCurve = [[info objectForKey:UIKeyboardAnimationCurveUserInfoKey] intValue];
NSLog(@"\nFrame Begin = %@\nFrame End = %@\nAnimation Duration = %f\nAnimation Curve = %i",
NSStringFromCGRect(kbFrameBeginFrame), NSStringFromCGRect(kbFrameEndFrame), animDuration, animCurve);
_showKeyboard = NO;
[self adjustUIForKeyboard:kbFrameEndFrame.size animDuration:animDuration];
}
/**
* Adjust the UI elements so that views are visible when keyboard is visible or hidden
*/
- (void)adjustUIForKeyboard:(CGSize)keyboardSize animDuration:(NSTimeInterval)duration {
[UIView animateWithDuration:duration
animations:^(void) {
// When keyboard is showing we adjust up and vice versa for a hidden keyboard
if (_showKeyboard) {
// Set your view's frame values
} else {
// Set your view's frame values
}
}
completion:NULL];
}
关于iphone - iOS 5 的 Messages.app 键盘附件 View 行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8910301/