获取键盘移动动画,制作控件跟随特效。

此文为转载。仅供收藏

 

//收到键盘弹出通知后的响应

- (void)keyboardWillShow:(NSNotification *)info {

    //保存info

    NSDictionary *dict = info.userInfo;

    //得到键盘的显示完成后的frame

    CGRect keyboardBounds = [dict[UIKeyboardFrameEndUserInfoKey] CGRectValue];

    //得到键盘弹出动画的时间

    double duration = [dict[UIKeyboardAnimationDurationUserInfoKey] doubleValue];

    //坐标系转换

    //由于取出的位置信息是绝对的,所以要将其转换为对应于当前view的位置,否则位置信息会出错!

    CGRect keyboardBoundsRect = [self.view convertRect:keyboardBounds toView:nil];

    //得到键盘的高度,即输入框需要移动的距离

    double offsetY = keyboardBoundsRect.size.height;

    //得到键盘动画的曲线信息,按原作的话说此处是难点stackoverflow网站里找到的

    UIViewAnimationOptions options = [dict[UIKeyboardAnimationCurveUserInfoKey] integerValue] << 16;

    //添加动画

    [UIView animateWithDuration:duration delay:0 options:options animations:^{

        _textField.transform = CGAffineTransformMakeTranslation(0, -offsetY);

    } completion:nil];

   

}

 

//隐藏键盘通知的响应

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

    //输入框回去的时候就不需要高度了,直接取动画时间和曲线还原回去

    NSDictionary *dict = info.userInfo;

    double duration = [dict[UIKeyboardAnimationDurationUserInfoKey] doubleValue];

    UIViewAnimationOptions options = [dict[UIKeyboardAnimationCurveUserInfoKey] integerValue] << 16;

    //CGAffineTransformIdentity是置位,可将改变的transform还原

    [UIView animateWithDuration:duration delay:0 options:options animations:^{

        _textField.transform = CGAffineTransformIdentity;

    } completion:nil];

}

posted @ 2015-10-28 11:12  红纸  阅读(495)  评论(0编辑  收藏  举报