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];
  • 相关阅读:
    端口服务
    系统设计的主要原则是什么?
    Dynamics CRM2015 Custom Code Validation Tool工具的使用
    CONFIGURE ADFS 3.0 WITH SHAREPOINT 2013
    Sharepoint Solution Gallery Active Solution时激活按钮灰色不可用的解决方法
    Dynamics CRM 2015Online Update1 new feature之 插件跟踪日志
    Dynamics CRM2013/2015 Plugin注册工具Register New Assembly时无法看到注册按钮的解决办法
    Dynamics CRM 2015 站点地图公告配置实体显示名称的变更
    Dynamics CRM 2015 Online Update1 UI界面的更新变化
    SQL Server2012 AlwaysOn 无法将数据库联接到可用性组 针对主副本的连接未处于活动状态
  • 原文地址:https://www.cnblogs.com/Milo-CTO/p/6077632.html
Copyright © 2011-2022 走看看