美文网首页
通知笔记

通知笔记

作者: 松n_n鼠 | 来源:发表于2017-11-22 14:23 被阅读0次

UIDevice通知

UIDevice类提供了一个单例对象,它代表着设备,通过它可以获得一些设备相关的信息,比如电池电量值(batteryLevel)、电池状态(batteryState)、设备的类型(model,比如iPod、iPhone等)、设备的系统(systemVersion)

通过[UIDevice currentDevice]可以获取这个单例对象(一般用来做系统适配)

UIDevice对象会不间断地发布一些通知,下列是UIDevice对象所发布通知的名称常量:

UIDeviceOrientationDidChangeNotification  设备旋转

UIDeviceBatteryStateDidChangeNotification  电池状态改变

UIDeviceBatteryLevelDidChangeNotification  电池电量改变

UIDeviceProximityStateDidChangeNotification  近距离传感器(比如设备贴近了使用者的脸部)

键盘通知

我们经常需要在键盘弹出或者隐藏的时候做一些特定的操作,因此需要监听键盘的状态键盘状态改变的时候,系统会发出一些特定的通知

UIKeyboardWillShowNotification  键盘即将显示

UIKeyboardDidShowNotification  键盘显示完毕

UIKeyboardWillHideNotification  键盘即将隐藏

UIKeyboardDidHideNotification  键盘隐藏完毕

UIKeyboardWillChangeFrameNotification  键盘的位置尺寸即将发生改变

UIKeyboardDidChangeFrameNotification  键盘的位置尺寸改变完毕

系统发出键盘通知时,会附带一下跟键盘有关的额外信息(字典),字典常见的key如下:

UIKeyboardFrameBeginUserInfoKey  键盘刚开始的frame

UIKeyboardFrameEndUserInfoKey  键盘最终的frame(动画执行完毕后)

UIKeyboardAnimationDurationUserInfoKey  键盘动画的时间

UIKeyboardAnimationCurveUserInfoKey  键盘动画的执行节奏(快慢)
- (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 *)note

{

     获得键盘的frame

    CGRect frame = [note.userInfo[UIKeyboardFrameBeginUserInfoKey] CGRectValue];


     修改底部约束

    self.bottn.constant = frame.size.height;


      执行动画

    CGFloat duration = [note.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];

    [UIView animateWithDuration:duration animations:^{

        [self.view layoutIfNeeded];

    }];

}


/**

 *  监听键盘的即将隐藏

 */

- (void)keyboardWillHide:(NSNotification *)note

{

     修改底部约束

    self.bottn.constant = 0;


      执行动画

    CGFloat duration = [note.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];

    [UIView animateWithDuration:duration animations:^{

        [self.view layoutIfNeeded];

    }];

}


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

{

     文本框不再是第一响应者,就会退出键盘

        [self.textField resignFirstResponder];


        [self.textField endEditing:YES];


    [self.view endEditing:YES];

}
// 注:监听键盘的显示和隐藏,最重要的思想就是拿整个view的高度减去键盘的高度,注意细节

相关文章

  • 通知笔记

    UIDevice通知 UIDevice类提供了一个单例对象,它代表着设备,通过它可以获得一些设备相关的信息,比如电...

  • 恋爱心理学指南团每日日报2018/6/29

    【运营执行组】 已完成工作: 1、小果冻通知更新(晒笔记终版、福利集锦) 2、巧玲通知更新(爱情树洞、晒笔记初版)...

  • 学习笔记-通知

    关于通知 1.消息源 创建通知NSNotification *notifiction = [NSNotificat...

  • Android通知笔记

    参考: 通知 TaskStackBuilder的用法 PendingIntent的用法 http://www.ro...

  • Android通知笔记

    RemoteViews只支持部分布局和控件 当我们自定义通知布局的时候,需要通过NotificationCompa...

  • Spring Event事件通知机制 源码学习

    笔记简述本学习笔记主要是介绍Spring中的事件通知是如何实现的,同步和异步事件通知的用法和实现细节以及Sprin...

  • 民诉错题笔记2020-09-15

    11503048. 笔记内容: “”法院向王旭送达应诉通知书“”,说明已经立案。 11203040. 笔记内容: ...

  • 【🎨熙绘画月记录·5月】只有感觉好才能做得好

    2018.5.5 通知通知: 家长们一定仔细阅读以下信息哦❤ 【本周️户外️课程】 自然笔记2 | 感受观察.采...

  • iOS笔记之通知

    通知中心(NotificationCenter)和通知(UILocalNotification)是雷锋和雷峰塔的关...

  • 通知(印象笔记迁移)

    **当页面创建之后 才可以收到通知 3种通知: 1、不传递参数, 最常用的一种 // 发送通知-(void)btn...

网友评论

      本文标题:通知笔记

      本文链接:https://www.haomeiwen.com/subject/nfwnvxtx.html