星空网 > 软件开发 > 操作系统

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键盘通知images/loading.gif' data-original="http://images2015.cnblogs.com/blog/869312/201601/869312-20160106084436293-2047431380.png" />

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

    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




原标题:UI键盘通知

关键词:

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

环球市场:https://www.goluckyvip.com/tag/11440.html
Shopee东:https://www.goluckyvip.com/tag/11442.html
WordP:https://www.goluckyvip.com/tag/11443.html
难以想象:https://www.goluckyvip.com/tag/11444.html
旺季将至:https://www.goluckyvip.com/tag/11445.html
谷仓海外仓:https://www.goluckyvip.com/tag/11446.html
在古巴做游轮 古巴旅游项目:https://www.vstour.cn/a/363194.html
西藏旅游攻略自驾游需要多少天 去西藏旅游自驾游要多久时间:https://www.vstour.cn/a/363195.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流