iOS开发

iOS开发-键盘正确的弹出姿势

2019-01-10  本文已影响0人  037e3257fa3b

最近对项目中的键盘弹出做了一点优化,在此分享。

思考:正常的键盘弹出姿势是什么样的?是否只需要向上移动适当的高度满足用户操作即可。我认为应该是对于键盘没有挡住输入框则无需让视图向上移动,键盘挡住了输入框则让输入移动至键盘上方即可。

GIF如下:

keyboard.gif

demo 视图层次说明

baseView,firstTextfield,secodeTextfield三个视图

baseView.png

思路

弹出键盘时,挡住当前响应的控件则视图上移,未挡住则不上移

description.png

核心代码

监听键盘弹出隐藏

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    //监听键盘frame改变
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
   
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
    
}


//键盘将要弹出
- (void)keyboardWillShow:(NSNotification *)notification
{
    //获取键盘高度 keyboardHeight
    NSDictionary *userInfo = [notification userInfo];
    NSValue *aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
    CGRect keyboardRect = [aValue CGRectValue];
    CGFloat keyboardHeight = keyboardRect.size.height;
    
    //当前屏幕高度,无论横屏竖屏
    CGFloat screenH = [UIScreen mainScreen].bounds.size.height;
    
    //主窗口
    UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
    //获取当前响应者
    UIView *firstResponder = [keyWindow performSelector:@selector(firstResponder)];
    NSLog(@"firstResponder:%@",firstResponder);

  // Twitter 网络登录时上移问题
    Class clName = NSClassFromString(@"UIWebBrowserView");
    if ([firstResponder isKindOfClass:clName]) return;
    if (![firstResponder respondsToSelector:@selector(center)]) return;

    //当前响应者在其父视图的中点(x居中 y最下点)
    CGPoint firstCPoint = CGPointMake(firstResponder.center.x, CGRectGetMaxY(firstResponder.frame));
    //当前响应者在屏幕中的point
    CGPoint convertPoint = [keyWindow convertPoint:firstCPoint fromView:firstResponder.superview];
    //[firstResponder convertPoint:firstCPoint toView:keyWindow];
    //当前响应者的最大y值
    CGFloat firstRespHeight = convertPoint.y;
    
    //键盘最高点的y值
    CGFloat topHeighY = screenH - keyboardHeight;//顶部高度的Y值
    if (topHeighY < firstRespHeight) { //键盘挡住了当前响应的控件 需要上移
        CGFloat spaceHeight = firstRespHeight - topHeighY;
        self.baseView.center = CGPointMake(self.baseView.center.x, self.baseView.center.y - spaceHeight);
        
        CGFloat duration = [userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
        [UIView animateWithDuration:duration animations:^{
            [self.view layoutIfNeeded];
        }];
        
    }else{
        NSLog(@"键盘未挡住输入框");
    }
    
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    //结束编辑
    [self.view endEditing:YES];
}

//键盘将要隐藏
- (void)keyboardWillHide:(NSNotification *)notification{
    
    self.baseView.center = self.view.center;
    
    NSDictionary *userInfo = [notification userInfo];
    CGFloat duration = [userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    [UIView animateWithDuration:duration animations:^{
        [self.view layoutIfNeeded];
    }];
}

去掉警告⚠️

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wundeclared-selector"
          UIView *firstResponder = [keyWindow performSelector:@selector(firstResponder)];
#pragma clang diagnostic pop

#pragma clang diagnostic

上一篇下一篇

猜你喜欢

热点阅读