1
誰能解釋如何最好地模仿行爲「附屬視圖」(在倒逗號,因爲我實際上不認爲它是附屬視圖 )在iOS 5的Messages.app顯示中,我想要一個視圖,它給人的印象是它固定在鍵盤的頂部,但當鍵盤被解散時它仍然保留在屏幕上。
誰能解釋如何最好地模仿行爲「附屬視圖」(在倒逗號,因爲我實際上不認爲它是附屬視圖 )在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];
}
非常棒!我在這裏的關鍵是我沒有意識到你可以從通知中提取鍵盤動畫細節。謝謝你。 – 2012-01-18 13:25:46
感謝您的「拼出來」,可以這麼說。 – Morkrom 2013-11-21 01:43:43