[OC]注册登录界面键盘监听通知与动画结合使用

原文链接:https://www.cnblogs.com/iosliu/p/4425024.html

- (void)viewDidLoad {

    [super viewDidLoad];

    //键盘监听通知

    //增加监听,当键盘出现或改变时收出消息

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];

    //增加监听,当键退出时收出消息

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

}

- (void)keyboardWillShow:(NSNotification*)aNotification { NSDictionary* info = [aNotification userInfo];

    //kbSize即為鍵盤尺寸 (有width, height)

    CGSize kbSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;

    //得到鍵盤的高度

    CGFloat keyboardhight = kbSize.height;

    //将UISCROLLVIEW上移动 hashKeyBoard=YES;

    //设置动画的名字

    [UIView beginAnimations:@"AnimationOpen" context:nil];

    //设置动画的间隔时间 [UIView setAnimationDuration:0.20];

    //??使用当前正在运行的状态开始下一段动画

    [UIView setAnimationBeginsFromCurrentState: YES];

    //设置视图移动的位移

    self.view.frame = CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y - keyboardhight, self.view.frame.size.width, self.view.frame.size.height);

    //设置动画结束

    [UIView commitAnimations];

}

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

    NSDictionary* info = [aNotification userInfo];

    //kbSize即為鍵盤尺寸 (有width, height)

    CGSize kbSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;

    //得到键盘的高度

    CGFloat keyboardhight = kbSize.height;

    [UIView beginAnimations:@"AnimationOpen" context:nil];

    //设置动画的间隔时间

    [UIView setAnimationDuration:0.20];

    //使用当前正在运行的状态开始下一段动画

    [UIView setAnimationBeginsFromCurrentState: YES];

    //设置视图移动的位移

    self.view.frame = CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y + keyboardhight, self.view.frame.size.width, self.view.frame.size.height);

    //设置动画结束

    [UIView commitAnimations];

}

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

推荐阅读更多精彩内容