• 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];
  • 相关阅读:
    字王20年
    [转]Birdfont 2.10 发布,字体编辑器
    vs2013编译boost库
    c++ 完成端口资料
    获取输入法输入内容及后选项的钩子
    unicode string和ansi string的转换函数及获取程序运行路径的代码
    c++实现输入法窗口自定义的代码
    在固定长方形里产生渐变
    监听某个div或其它标签的大小改变来执行相应的处理
    64位SqlServer通过链接服务器与32位oracle通讯
  • 原文地址:https://www.cnblogs.com/Milo-CTO/p/6077632.html
Copyright © 2020-2023  润新知