IOS8下利用自动布局实现键盘的弹出效果

举报
清雨小竹 发表于 2022/09/24 23:53:47 2022/09/24
【摘要】 -(void)keyboardChange:(NSNotification*)notice { NSValue *value = [[notice userInfo] objectForKey:@"UIKeyboardFrameEndUserInfoKey"]; float keyEnd_y = [value CGRec...
-(void)keyboardChange:(NSNotification*)notice
{
    NSValue *value = [[notice userInfo] objectForKey:@"UIKeyboardFrameEndUserInfoKey"];
    float keyEnd_y = [value CGRectValue].origin.y;
    float animationDuration = [[notice userInfo][@"UIKeyboardAnimationDurationUserInfoKey"] floatValue];
    
    CGRect frame = [[UIScreen mainScreen]bounds];//屏幕尺寸
    int SCREEN_WIDTH = frame.size.width;
    int SCREEN_HEIGHT = frame.size.height;
    
    if(!shouldResignFirstResponder){
        UIView *alphaCoverView = [[[UIApplication sharedApplication].delegate window] viewWithTag:10001];
        if (!alphaCoverView) {
            alphaCoverView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, keyEnd_y - 40)];
            [alphaCoverView setBackgroundColor:[UIColor blackColor]];
            alphaCoverView.alpha = 0.0;
            alphaCoverView.tag = 10001;
            UITapGestureRecognizer *tapToResignirstResponder = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(textFielfWillResignFirstResponder)];
            [alphaCoverView addGestureRecognizer:tapToResignirstResponder];
            [[[UIApplication sharedApplication].delegate window] addSubview:alphaCoverView];
        }
        [UIView animateWithDuration:animationDuration animations:^{
            alphaCoverView.alpha = 0.4;
            alphaCoverView.frame = CGRectMake(0, 0, SCREEN_WIDTH, keyEnd_y - 40);//40是编辑框视图的高
        }];
    }
    float constant = 0.0;
    if (shouldResignFirstResponder) {
        //收回键盘时,将约束的值复原
        constant = SCREEN_HEIGHT - keyEnd_y;
        [UIView animateWithDuration:animationDuration animations:^{
            _m_spaceToBottom.constant = constant;
            [self.view layoutIfNeeded];
        }];
        shouldResignFirstResponder = NO;
    }else{
        constant = SCREEN_HEIGHT - keyEnd_y-46;//46是编辑框距离屏幕底部的距离
        [UIView animateWithDuration:animationDuration animations:^{
            _m_spaceToBottom.constant = constant;
            [self.view layoutIfNeeded];
        }];
    }
}
- (void)textFielfWillResignFirstResponder{
    UIView *view = [[[UIApplication sharedApplication].delegate window] viewWithTag:10001];
    [view removeFromSuperview];
    shouldResignFirstResponder = YES;
    [self.view endEditing:YES];
}


//键盘变化监听
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardChange:) name:@"UIKeyboardWillChangeFrameNotification" object:nil];

借鉴文章:

http://blog.csdn.net/u013016828/article/details/42102411

文章来源: zzzili.blog.csdn.net,作者:清雨小竹,版权归原作者所有,如需转载,请联系作者。

原文链接:zzzili.blog.csdn.net/article/details/42387001

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。