iOS 5的Messages.app键盘附件查看行为

时间:2012-01-18 12:45:28

标签: iphone objective-c ios uiview core-animation

Messages.app Screenshot

任何人都可以在iOS 5的Messages.app显示中解释如何最好地模仿“附件视图”(在倒置逗号中,因为我实际上并不认为它是附件视图)的行为,因为我想要一个视图,它给人的印象是它固定在键盘的顶部,但是当键盘被解除时它仍然在屏幕上。

1 个答案:

答案 0 :(得分:5)

这可能是一个单独的视图,使用与键盘动画具有相同持续时间的动画重新定位。

尝试观察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];
}