你的位置:首页 > 软件开发 > 操作系统 > UI键盘通知

UI键盘通知

发布时间:2016-01-06 09:00:14
#import "ViewController.h" @interface ViewController ()@property (nonatomic,strong) UITextField *tf;@end @implementation ViewC ...

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);

 UI键盘通知

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

    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


 

海外公司注册、海外银行开户、跨境平台代入驻、VAT、EPR等知识和在线办理:https://www.xlkjsw.com

原标题:UI键盘通知

关键词:

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

可能感兴趣文章

我的浏览记录