根据键盘的弹出隐藏自动调整View的位置

1.首先注册系统通知
动画

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

2.键盘将要改变时的相应处理  spa

-(void)keyboardWillChangeFrameNotification:(NSNotification *)note{
  
    //取出键盘动画的时间(根据userInfo的key----UIKeyboardAnimationDurationUserInfoKey)
    CGFloat duration = [note.userInfo[UIKeyboardAnimationDurationUserInfoKey] floatValue];
    
    //取得键盘最后的frame(根据userInfo的key----UIKeyboardFrameEndUserInfoKey = "NSRect: {{0, 227}, {320, 253}}";)
    CGRect keyboardFrame = [note.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
    
    //计算控制器的view须要平移的距离
    CGFloat transformY = keyboardFrame.origin.y - self.view.frame.size.height;
    
    //执行动画
    [UIView animateWithDuration:duration animations:^{
        //平移
        self.view.transform = CGAffineTransformMakeTranslation(0, transformY);
    }];
}

3.这样其实还没完(存在内存隐患)还须要重写dealloc方法3d

-(void)dealloc{code

    //使用通知中心后必须重写dealloc方法,进行释放(ARC)(ARC还须要写上[super dealloc];)orm

    //removeObserver和 addObserver相对应.
server

    [[NSNotificationCenter defaultCenter] removeObserver:self];内存

}rem

相关文章
相关标签/搜索