zoukankan      html  css  js  c++  java
  • iOS全局处理键盘事件

    转自:http://www.cnblogs.com/xinus/archive/2013/01/22/ios-keybord-notification.html

    1. 注册监听键盘事件的通知
      复制代码
          [[NSNotificationCenter defaultCenter] addObserver:self
                                                   selector:@selector(keyboardWillShow:)
                                                       name:UIKeyboardWillShowNotification
                                                     object:nil];
          
          [[NSNotificationCenter defaultCenter] addObserver:self
                                                   selector:@selector(keyboardShow:)
                                                       name:UIKeyboardDidShowNotification
                                                     object:nil];
          
          [[NSNotificationCenter defaultCenter] addObserver:self
                                                   selector:@selector(keyboardWillHide:)
                                                       name:UIKeyboardWillHideNotification
                                                     object:nil];
          
          [[NSNotificationCenter defaultCenter] addObserver:self
                                                   selector:@selector(keyboardHide:)
                                                       name:UIKeyboardDidHideNotification
                                                     object:nil];
      复制代码
    2. 在键盘将要出现和隐藏的回调中,加入动画。
      复制代码
      - (void)keyboardWillShow:(NSNotification *)notif {
          if (self.hidden == YES) {
              return;
          }
          
          CGRect rect = [[notif.userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
          CGFloat y = rect.origin.y;
          [UIView beginAnimations:nil context:nil];
          [UIView setAnimationDuration:0.25];
          NSArray *subviews = [self subviews];
          for (UIView *sub in subviews) {
              
              CGFloat maxY = CGRectGetMaxY(sub.frame);
              if (maxY > y - 2) {
                  sub.center = CGPointMake(CGRectGetWidth(self.frame)/2.0, sub.center.y - maxY + y - 2);
              }
          }
          [UIView commitAnimations];
      }
      
      - (void)keyboardShow:(NSNotification *)notif {
          if (self.hidden == YES) {
              return;
          }
      }
      
      - (void)keyboardWillHide:(NSNotification *)notif {
          if (self.hidden == YES) {
              return;
          }
          [UIView beginAnimations:nil context:nil];
          [UIView setAnimationDuration:0.25];
          NSArray *subviews = [self subviews];
          for (UIView *sub in subviews) {
              if (sub.center.y < CGRectGetHeight(self.frame)/2.0) {
                  sub.center = CGPointMake(CGRectGetWidth(self.frame)/2.0, CGRectGetHeight(self.frame)/2.0);
              }
          }
          [UIView commitAnimations];
      }
      
      - (void)keyboardHide:(NSNotification *)notif {
          if (self.hidden == YES) {
              return;
          }
      }
      复制代码
  • 相关阅读:
    Flink实例(十九):FLINK 异步IO (四)实例 (二) MySQL
    Flink实例(十八):FLINK 异步IO (三)实例 (一)
    Flink实例(十七):FLINK 异步IO (二)原理
    kata镜像
    golang no Go files
    docker命令
    golang 编译安装kata container (二)
    golang代理
    golang 安装依赖
    golang
  • 原文地址:https://www.cnblogs.com/feiyu-mdm/p/5568562.html
Copyright © 2011-2022 走看看