iOS 键盘弹出与回收,界面上移/下移

   [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(transformView:) name:UIKeyboardWillChangeFrameNotification object:nil];//键盘的弹出

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];

//键盘的消失

//弹出键盘UIView

-(void)transformView:(NSNotification*)aNSNotification{


    //获取键盘弹出前的Rect

    NSValue*keyBoardBeginBounds=[[aNSNotification userInfo]objectForKey:UIKeyboardFrameBeginUserInfoKey];

    CGRect beginRect=[keyBoardBeginBounds CGRectValue];


    //获取键盘弹出后的Rect

    NSValue*keyBoardEndBounds=[[aNSNotification userInfo]objectForKey:UIKeyboardFrameEndUserInfoKey];

    CGRect  endRect=[keyBoardEndBounds CGRectValue];


    //获取键盘位置变化前后纵坐标Y的变化值

    CGFloat deltaY=endRect.origin.y-beginRect.origin.y;

    NSLog(@"看看这个变化的Y值:%f",deltaY);


    //在0.25s内完成self.view的Frame的变化,等于是给self.view添加一个向上移动deltaY的动画

    [UIView animateWithDuration:0.25f animations:^{

        self.popView.frame = CGRectMake(0, -50, kScreenWidth, kScreenHeight);

    }];


}

//退出键盘UIView

-(void)keyboardWillHide:(NSNotification*)aNSNotification{


    [UIView beginAnimations:nil context:nil];


    [UIView setAnimationDuration:0.25];


    [UIView setAnimationCurve:7];


    self.popView.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight);


    [UIView commitAnimations];


}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容