IOS键盘通知中心方法

  • (void)dealloc
    {
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
    }

  • (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    _chatView = [[UIView alloc] initWithFrame:CGRectMake(0, 440, 320, 40)];
    _chatView.backgroundColor = [UIColor grayColor];
    [self.view addSubview:_chatView];

    UITextField * f = [[UITextField alloc] initWithFrame:CGRectMake(20, 5, 240, 30)];
    f.borderStyle = UITextBorderStyleRoundedRect;
    [_chatView addSubview:f];

    UIButton * b = [UIButton buttonWithType:UIButtonTypeSystem];
    [b setTitle:@”发送” forState:UIControlStateNormal];
    b.frame = CGRectMake(270,5,40,30);
    [_chatView addSubview:b];

    // NSNotificationCenter 通知中心
    // 注册
    /*
    参数1:观察者对象
    参数2:方法选择器
    参数3:向通知中心注册的字符串
    参数4:参数
    向通知中心注册一个观察者对象
    */
    // 当键盘即将抬起时 触发当前对象中的keyboardWillShow方法
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    // 必须在delloc中移除观察者身份
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHidden:) name:UIKeyboardWillHideNotification object:nil];

    // UITextView 折行的UITextField
    // 高仿 QQ聊天的下半部分界面
    }

  • (void)keyboardWillShow:(id)note
    {
    // NSLog(@”note is %@”,note);
    // 动画持续时间 键盘高度
    // userInfo = {
    // UIKeyboardAnimationCurveUserInfoKey = 0;
    // UIKeyboardAnimationDurationUserInfoKey = 0;
    // UIKeyboardBoundsUserInfoKey = “NSRect: {{0, 0}, {320, 216}}”;
    // UIKeyboardCenterBeginUserInfoKey = “NSPoint: {160, 353.5}”;
    // UIKeyboardCenterEndUserInfoKey = “NSPoint: {160, 372}”;
    // UIKeyboardFrameBeginUserInfoKey = “NSRect: {{0, 227}, {320, 253}}”;
    // UIKeyboardFrameEndUserInfoKey = “NSRect: {{0, 264}, {320, 216}
    // }

    // 获取键盘高度 高度为动态
    // 字典中拿到的值是NSRect 转化为 CGRect 方法:CGRectValue
    CGFloat height = [[[note userInfo] objectForKey:@”UIKeyboardFrameEndUserInfoKey”] CGRectValue].size.height;
    // 动画调整_chatView的位置 与键盘同步
    [UIView animateWithDuration:0.25 animations:^{
    _chatView.frame = CGRectMake(0, 480-height-40, 320, 40);
    }];
    }

  • (void)keyboardWillHidden:(NSNotification *)note
    {
    [UIView animateWithDuration:0.25 animations:^{
    _chatView.frame = CGRectMake(0, 480-40, 320, 40);
    }];
    }

  • (void)touchesEnded:(NSSet )touches withEvent:(UIEvent )event
    {
    [self.view endEditing:YES];
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值