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];
  • 相关阅读:
    Game of War
    Unreal Engine 4 性能优化工具(Profiler Tool)
    触屏设备上的多点触碰检测C++代码实现
    独立游戏设计流程:从概念到写码的13个步骤
    ue4 多相机分屏与小地图效果实现教程
    Unreal Engine 4 笔记 2
    3dsMax模型转UE4
    以《西游记》为例 详解游戏设计归纳演绎法
    假期关于产品-设计-逻辑-市场-团队思考节选30篇
    Unreal Engine 4 笔记
  • 原文地址:https://www.cnblogs.com/Milo-CTO/p/6077632.html
Copyright © 2011-2022 走看看