iOS 应用程序中的 iMessage 样式后退键盘

时间:2023-03-08
本文介绍了iOS 应用程序中的 iMessage 样式后退键盘的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直想知道是否可以在消息应用程序中复制 Apple iOS5 键盘的行为,而无需使用任何私有 API 调用.当您向下滚动到消息应用程序中的键盘时,键盘将折叠,留下更多空间来查看消息 - 试试看.

I've been wondering if it is possible to replicate the behavior of Apple's iOS5 keyboard in the messages app, without using any private API calls. When you scroll down past the keyboard in the messages app, the keyboard will collapse leaving more room to see messages - try it to see.

我找不到任何指向制作这个的东西,而不必开始跳过一些严肃的圈子来获取键盘视图的实例.而且我很确定 Apple 不会对此感到满意.

I couldn't find anything that points towards making this without having to start jumping through some serious hoops to get an instance of the Keyboard's View. And I'm pretty sure Apple wouldn't be happy with that.

除了下面给出的答案之外,您还可以在此处看到我的实现的完整 xcode 项目:https://github.com/orta/iMessage-Style-Receding-Keyboard

In addition to the answer given below you can see a fully baked xcode project of my implementation here: https://github.com/orta/iMessage-Style-Receding-Keyboard

推荐答案

这是一个不完整的解决方案,但它应该给你一个很好的起点.

This is an incomplete solution, however it should give you a good starting point.

将以下 ivars 添加到您的 UIViewController:

Add the following ivars to your UIViewController:

CGRect        keyboardSuperFrame; // frame of keyboard when initially displayed
UIView      * keyboardSuperView;  // reference to keyboard view

将 inputAccessoryView 添加到您的文本控制器.我创建了一个小视图作为附件视图插入:

Add an inputAccessoryView to your text controller. I created an small view to insert as the accessoryView:

accView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
accView.backgroundColor = [UIColor clearColor];
textField.inputAccessoryView = accView;

我将上面的代码添加到-(void)loadView

在视图加载时注册接收 UIKeyboardDidShowNotification 和 UIKeyboardDidHideNotification:

Register to receive UIKeyboardDidShowNotification and UIKeyboardDidHideNotification when view is loaded:

- (void)viewDidLoad
{
    [super viewDidLoad];
    [[NSNotificationCenter defaultCenter] addObserver:self
        selector:@selector(keyboardWillShow:)
        name:UIKeyboardWillShowNotification
        object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self
        selector:@selector(keyboardDidShow:)
        name:UIKeyboardDidShowNotification
        object:nil];
    return;
}

添加方法以指定为通知的选择器:

Add methods to specified as the selectors for the notifications:

// method is called whenever the keyboard is about to be displayed
- (void)keyboardWillShow:(NSNotification *)notification
{
    // makes keyboard view visible incase it was hidden
    keyboardSuperView.hidden = NO;
    return;
}
// method is called whenever the keyboard is displayed
- (void) keyboardDidShow:(NSNotification *)note
{
    // save reference to keyboard so we can easily determine
    // if it is currently displayed
    keyboardSuperView  = textField.inputAccessoryView.superview;

    // save current frame of keyboard so we can reference the original position later
    keyboardSuperFrame = textField.inputAccessoryView.superview.frame;
    return;
}

添加跟踪触摸和更新键盘视图的方法:

Add methods to track touched and update keyboard view:

// stops tracking touches to divider
- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    CGRect newFrame;
    CGRect bounds = [[UIScreen mainScreen] bounds];

    newFrame = keyboardSuperFrame;
    newFrame.origin.y = bounds.size.height;  

    if ((keyboardSuperView.superview))
        if (keyboardSuperFrame.origin.y != keyboardSuperView.frame.origin.y)
            [UIView animateWithDuration:0.2
                    animations:^{keyboardSuperView.frame = newFrame;}
                    completion:^(BOOL finished){
                                keyboardSuperView.hidden = YES;
                                keyboardSuperView.frame = keyboardSuperFrame;
                                [textField resignFirstResponder]; }];
    return;
}


// updates divider view position based upon movement of touches
- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
   UITouch  * touch;
   CGPoint    point;
   CGFloat    updateY;

   if ((touch = [touches anyObject]))
   {
      point   = [touch locationInView:self.view];
      if ((keyboardSuperView.superview))
      {
         updateY = keyboardSuperView.frame.origin.y;
         if (point.y < keyboardSuperFrame.origin.y)
            return;
         if ((point.y > updateY) || (point.y < updateY))
            updateY = point.y;
         if (keyboardSuperView.frame.origin.y != updateY)
            keyboardSuperView.frame = CGRectMake(keyboardSuperFrame.origin.x,
                                                 point.y,
                                                 keyboardSuperFrame.size.width,
                                                 keyboardSuperFrame.size.height);
      };
   };
   return;
}

免责声明:

  • 当第一次响应退出时,键盘会在滑出屏幕之前移回原来的位置.为了使关闭键盘更加流畅,您首先需要创建一个动画以将键盘移出屏幕,然后隐藏视图.我将把这部分作为练习留给读者.
  • 我只在 iOS 5 模拟器和装有 iOS 5 的 iPhone 上进行了测试.我没有在早期版本的 iOS 上测试过.

我为测试这个概念而创建的 SlidingKeyboard 项目可从 GitHub 的 BindleKit 示例目录中获得:

The SlidingKeyboard project I created to test this concept is available from GitHub in the examples directory of BindleKit:

https://github.com/bindle/BindleKit

更新示例以解决第一个免责声明.

Updating example to address first disclaimer.

这篇关于iOS 应用程序中的 iMessage 样式后退键盘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

上一篇:如何制作 Android EditView“完成"按钮并在单击时 下一篇:启动 Activity 时自动弹出键盘

相关文章

最新文章