通知笔记

2017-11-22  本文已影响0人  松n_n鼠

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的高度减去键盘的高度,注意细节
上一篇 下一篇

猜你喜欢

热点阅读