UI键盘通知

#import "ViewController.h"

 

@interface ViewController ()

@property (nonatomic,strong) UITextField *tf;

@end

 

@implementation ViewController

 

- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

    _tf = [[UITextField alloc]initWithFrame:CGRectMake(10, self.view.bounds.size.height - 40, 300, 30)];

    _tf.borderStyle = UITextBorderStyleRoundedRect;

    [self.view addSubview:_tf];

    

    /*

    通知中心(单例)

    通知中心是一对多的,即同一个广播可以被多个收音机接收

     代理是一对一

     作用:1.用来接收广播和发起广播

                 2.用通知的名字作为频道

     */

    /*

    参数一:响应的类

     参数二:类中响应的方法

     参数三:通知的名字(即频道)

     参数四:接收类型 [注意]nil代表任何类型

     */

    //:UIKeyboardWillShowNotification接收键盘将要显示的通知

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

    

    

    //UIKeyboardWillHideNotification接收键盘将要隐藏的通知

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

    

}

#pragma mark - UIKeyboardWillHideNotification

//键盘将要隐藏

-(void)keyBoardWillHide:(NSNotification *)noti

{

     //noti.userInfo是一个字典,大家可以输出来看看字典里面包含了什么

    //NSLog(@"%@",noti.userInfo);

 

    //获得弹下去后的坐标[注意是弹下后的]

    CGRect keyBoardEndFrame = [[noti.userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];

    //NSLog(@"--%@",NSStringFromCGRect(keyBoardEndFrame));

    [UIView animateWithDuration:2 animations:^{

        //改变文本框的位置,让它跟着键盘一起弹起来

        CGRect tfRext = _tf.frame;

        tfRext.origin.y = keyBoardEndFrame.origin.y-_tf.bounds.size.height-10;

        _tf.frame = tfRext;

    }];

}

 

#pragma mark - UIKeyBoard notification

//接收到的是通知,所以参数用NSNotification

//键盘将要显示

-(void)keyBoardWillShow:(NSNotification *)noti

{

    CGRect keyBoardFrame = [[noti.userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];

    //可以通过以下方法输出CGRect

    //NSStringFromCGRect将CGRect转化为字符串的方式获取Frame

    //NSLog(@"%@",NSStringFromCGRect(keyBoardFrame));

    

    //获取键盘的动画持续时间

    //CGFloat keyBoardDuration = [noti.userInfo[UIKeyboardAnimationDurationUserInfoKey] floatValue];

    

    [UIView animateWithDuration:2 animations:^{

        CGRect tfRect = _tf.frame;

        tfRect.origin.y = keyBoardFrame.origin.y - _tf.bounds.size.height -10;

        _tf.frame = tfRect;

    }];

    

}

#pragma mark - 当手指触碰屏幕任何一地方时被调用

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event

{

    //利用结束编辑来隐藏键盘

    [self.view endEditing:YES];

}

 

 

@end

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

荣华富贵8

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值