iOS设备发出的系统通知

2017-11-06  本文已影响342人  半杯故事

UIDevice

UIDevice类提供了一个单粒对象,它代表着设备,通过它可以获得一些设备相关的信息,比如电池电量值(batteryLevel)、电池状态(batteryState)、设备的类型(model,比如iPod、iPhone等)、设备的系统(systemVersion)
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];
}
/**
 *  监听键盘的即将显示
 */
- (void)keyboardWillShow:(NSNotification *)note
{
     获得键盘的frame
    CGRect frame = [note.userInfo[UIKeyboardFrameBeginUserInfoKey] CGRectValue];
    
     修改底部约束
    self.bottn.constant = frame.size.height;
 }

程序相关

UIApplicationDidEnterBackgroundNotification 当程序被推送到后台时
UIApplicationWillEnterForegroundNotification  当程序从后台将要重新回到前台时
UIApplicationDidFinishLaunchingNotification  当程序完成载入后通知
UIApplicationDidBecomeActiveNotification   应用程序转为激活状态时
UIApplicationWillResignActiveNotification    用户按下主屏幕按钮调用通知,并未进入后台状态
UIApplicationDidReceiveMemoryWarningNotification   内存较低时通知
UIApplicationWillTerminateNotification   当程序将要退出时通知
UIApplicationSignificantTimeChangeNotification   当系统时间发生改变时通知
UIApplicationBackgroundRefreshStatusDidChangeNotification  后台下载状态发生改变时通知(iOS7.0以后可用)  
UIApplicationUserDidTakeScreenshotNotification   截屏通知
上一篇下一篇

猜你喜欢

热点阅读