zoukankan      html  css  js  c++  java
  • iOS键盘监听事件

    1.注册键盘通知事件

        NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
    
        // 键盘将出现事件监听
        [center addObserver:self selector:@selector(handleKeyboardWillShow:)
    
                       name:UIKeyboardWillShowNotification
    
                     object:nil];
    
        // 键盘将隐藏事件监听
        [center addObserver:self selector:@selector(handleKeyboardWillHide:)
    
                       name:UIKeyboardWillHideNotification
    
                     object:nil];

    2.处理动画

    // 键盘出现
    
    - (void)handleKeyboardWillShow:(NSNotification *)paramNotification
    {
        NSDictionary *userInfo = [paramNotification userInfo];
    
        NSValue *animationCurveObject = [userInfo valueForKey:UIKeyboardAnimationCurveUserInfoKey];
    
        NSValue *animationDurationObject = [userInfo valueForKey:UIKeyboardAnimationDurationUserInfoKey];
    
        NSValue *keyboardEndRectObject = [userInfo valueForKey:UIKeyboardFrameEndUserInfoKey];
    
        NSUInteger animationCurve = 0;
    
        double animationDuration = 0.0f;
    
        CGRect keyboardEndRect = CGRectMake(0,0, 0, 0);
    
        [animationCurveObject getValue:&animationCurve];
    
        [animationDurationObject getValue:&animationDuration];
    
        [keyboardEndRectObject getValue:&keyboardEndRect];
    
        UIWindow *window = [[[UIApplication sharedApplication] delegate] window];
    
        CGRect intersectionOfKeyboardRectAndWindowRect = CGRectIntersection(window.frame, keyboardEndRect);
    
        CGFloat bottomInset = intersectionOfKeyboardRectAndWindowRect.size.height;
    
        [UIView beginAnimations:@"changeViewContentInset" context:NULL];
    
        [UIView setAnimationDuration:animationDuration];
    
        [UIView setAnimationCurve:(UIViewAnimationCurve) animationCurve];
    
        CGRect tempRect = self.contentView.frame;
    
        tempRect.origin.y = Main_Screen_Height - bottomInset - 260 - 20;
    
        self.contentView.frame = tempRect;
    
        [UIView commitAnimations];
    }
    // 键盘消失
    
    - (void)handleKeyboardWillHide:(NSNotification *)paramNotification
    {
        NSDictionary *userInfo = [paramNotification userInfo];
    
        NSValue *animationCurveObject =[userInfo valueForKey:UIKeyboardAnimationCurveUserInfoKey];
    
        NSValue *animationDurationObject = [userInfo valueForKey:UIKeyboardAnimationDurationUserInfoKey];
    
        NSValue *keyboardEndRectObject =[userInfo valueForKey:UIKeyboardFrameEndUserInfoKey];
    
        NSUInteger animationCurve = 0;
    
        double animationDuration = 0.0f;
    
        CGRect keyboardEndRect = CGRectMake(0, 0, 0, 0);
    
        [animationCurveObject getValue:&animationCurve];
    
        [animationDurationObject getValue:&animationDuration];
    
        [keyboardEndRectObject getValue:&keyboardEndRect];
    
        [UIView beginAnimations:@"changeViewContentInset" context:NULL];
    
        [UIView setAnimationDuration:animationDuration];
    
        [UIView setAnimationCurve:(UIViewAnimationCurve)animationCurve];
    
        self.contentView.center = self.center;
    
        [UIView commitAnimations];
    }

     3.当然是别忘了注销通知哦

    [[NSNotificationCenter defaultCenter] removeObserver:self];
  • 相关阅读:
    20191003 尚硅谷Spring Cloud教学视频
    20190928 On Java8 第二十三章 注解
    C# 输出结果有System.Byte[]
    linux 命令
    用go run命令启动main package中的多个文件
    Docker使用入门
    Go实现mqtt服务
    MongoDB的Go语言驱动注意点
    Go实现发送解析GET与POST请求
    用Go实现RabbitMQ消息收发
  • 原文地址:https://www.cnblogs.com/Milo-CTO/p/6077632.html
Copyright © 2011-2022 走看看