[英]iOS 5's Messages.app Keyboard Accessory View Behaviour
任何人都可以解釋在iOS 5的Messages.app顯示中如何最好地模擬行為“附件視圖”( 在倒置逗號中,因為我實際上並不認為它是附件視圖 ),因為我想要一個給人印象的視圖它固定在鍵盤的頂部,但是在關閉鍵盤后仍會顯示在屏幕上。
它可能是一個單獨的視圖,該視圖使用與鍵盤動畫具有相同持續時間的動畫進行重新定位。
嘗試觀察UIKeyboardWillShowNotification和UIKeyboardWillHideNotification,並在處理程序中獲取鍵盤的動畫持續時間和幀,並開始自己的動畫以重新放置視圖,以便其隨鍵盤一起移動。 我使用的一些類似代碼如下:
- (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];
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.