iOS应用程序中的iMessage样式后退键盘 [英] iMessage Style Receding Keyboard in an iOS App

查看:155
本文介绍了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

I added the above code to -(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测试了这个。我没有用早先的测试过iOS版本。

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

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样式后退键盘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆