• IOS 监听键盘的通知(NSNotificationCenter)


    通知方法:

    /**
     *  当键盘改变了frame(位置和尺寸)的时候调用
     */
    - (void)keyboardWillChangeFrame:(NSNotification *)note
    {
        // 设置窗口的颜色
        self.view.window.backgroundColor = self.tableView.backgroundColor;
        
        // 0.取出键盘动画的时间
        CGFloat duration = [note.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
        
        // 1.取得键盘最后的frame
        CGRect keyboardFrame = [note.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
        
        // 2.计算控制器的view需要平移的距离
        CGFloat transformY = keyboardFrame.origin.y - self.view.frame.size.height;
        
        // 3.执行动画
        [UIView animateWithDuration:duration animations:^{
            self.view.transform = CGAffineTransformMakeTranslation(0, transformY);
        }];
    }
    
    /*
     UIKeyboardAnimationCurveUserInfoKey = 7;  // 动画的执行节奏(速度)
     UIKeyboardAnimationDurationUserInfoKey = "0.25"; // 键盘弹出隐藏动画所需要的时间
     UIKeyboardBoundsUserInfoKey = "NSRect: {{0, 0}, {320, 216}}";
     UIKeyboardCenterBeginUserInfoKey = "NSPoint: {160, 588}";
     UIKeyboardCenterEndUserInfoKey = "NSPoint: {160, 372}";
     UIKeyboardFrameChangedByUserInteraction = 0;
     
     // 键盘弹出
     UIKeyboardFrameBeginUserInfoKey = "NSRect: {{0, 480}, {320, 216}}";// 键盘刚出来那一刻的frame
     UIKeyboardFrameEndUserInfoKey = "NSRect: {{0, 264}, {320, 216}}"; //  键盘显示完毕后的frame
     
     // 键盘隐藏
     UIKeyboardFrameBeginUserInfoKey = "NSRect: {{0, 264}, {320, 216}}";
     UIKeyboardFrameEndUserInfoKey = "NSRect: {{0, 480}, {320, 216}}";
     */

    调用通知:

        // 2.监听键盘的通知
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChangeFrame:) name:UIKeyboardWillChangeFrameNotification object:nil];

    移出监听 ARC的情况

    -(void)dealloc
    {
    
        //[super dealloc]; 非ARC的情况需要这一步
        [[NSNotificationCenter defaultCenter]removeObserver:self];
    }
  • 相关阅读:
    【设计】概要设计-详细设计-到底需要输出什么???
    【Java】Eclipse代码格式化-代码模板
    【Scala】Scala学习资料
    【Java】阿里巴巴Java开发手册(纪念版)
    【Storm】学习笔记
    【HBase】学习笔记
    【Hadoop】Combiner的本质是迷你的reducer,不能随意使用
    【Hadoop】mapreduce采用多进程与spark采用多线程比较
    【ES】elasticsearch学习笔记
    【MySQL】MySQL统计NULL字段处理
  • 原文地址:https://www.cnblogs.com/liuwj/p/6480669.html
Copyright © 2020-2023  润新知