iphone - iOS 5 的 Messages.app 键盘附件 View 行为

标签 iphone objective-c ios uiview core-animation

Messages.app Screenshot

谁能解释如何最好地模拟 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/

相关文章:

iphone - 动态数据的 PDF 生成

在 FinishedLaunching 中设置 RootViewController 时,iOS 应用程序不会旋转

带有32位第三方库的iOS arm64项目

ios - 使用 UISearchBar 时 UITableView contentSize 错误

ios - 在 iOS 中对 pdf 文档进行数字签名

ios - NSNotification中心观察应用是否通过URL Scheme打开

iphone - 如何在 iPhone 中解析 gdata xml?

ios - 推送通知未发送,因为它在后台运行,parse.com

ios - 使用未声明的类型使用 CoreData Extension 错误

iphone - 在相机 View 上叠加一个框架,然后保存并使用生成的照片(捕获的内容和叠加框架)