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];
  • 相关阅读:
    SpringBoot SpringSession redis 共享 SESSION
    SpringBoot application.yml logback.xml,多环境配置,支持 java -jar --spring.profiles.active
    SpringBoot CGLIB AOP解决Spring事务,对象调用自己方法事务失效.
    SpringBoot整合Mybatis,多数据源,事务,支持java -jar 启动.
    SpringBoot整合SpringSecurity,SESSION 并发管理,同账号只允许登录一次
    SpringBoot idea maven打包war
    动态添加数据源,根据用户登录切换数据库.编程式Spring事务.
    MYSQL,触发器,实现两个表共用ID不重复
    试着简单易懂记录synchronized this object Class的区别,模拟ConcurrentHashMap
    Enum枚举写的一个简单状态机
  • 原文地址:https://www.cnblogs.com/Milo-CTO/p/6077632.html
Copyright © 2011-2022 走看看