测试已测试 | ✗ |
Lang语言 | Obj-CObjective C |
许可证 | MIT |
Released最后发布 | 2016年10月 |
由 Valery Fomenko 维护。
一种观察键盘通知和键盘属性的简单方法。
// Create new observer, or use shared instance
[[VFKeyboardObserver sharedKeyboardObserver] start];
[[VFKeyboardObserver sharedKeyboardObserver] stop];
适配 VFKeyboardObserverDelegate
协议并添加一个对象作为代理。
VFKeyboardObserver 可以有任意数量的代理。代理不会被保留。
@protocol VFKeyboardObserverDelegate <NSObject>
@optional
- (void)keyboardObserver:(VFKeyboardObserver *)keyboardObserver keyboardWillShowWithProperties:(VFKeyboardProperties)keyboardProperties interfaceOrientationWillChange:(BOOL)interfaceOrientationWillChange;
- (void)keyboardObserver:(VFKeyboardObserver *)keyboardObserver keyboardDidShowWithProperties:(VFKeyboardProperties)keyboardProperties;
- (void)keyboardObserver:(VFKeyboardObserver *)keyboardObserver keyboardWillHideWithProperties:(VFKeyboardProperties)keyboardProperties;
- (void)keyboardObserver:(VFKeyboardObserver *)keyboardObserver keyboardDidHideWithProperties:(VFKeyboardProperties)keyboardProperties;
@end
[[VFKeyboardObserver sharedKeyboardObserver] addDelegate:aDelegate];
[[VFKeyboardObserver sharedKeyboardObserver] removeDelegate:aDelegate];
当键盘将要出现或将要消失时更新 UI 是常见的。
- (void)keyboardObserver:(VFKeyboardObserver *)keyboardObserver keyboardWillShowWithProperties:(VFKeyboardProperties)keyboardProperties interfaceOrientationWillChange:(BOOL)interfaceOrientationWillChange {
[keyboardObserver animateWithKeyboardProperties:^{
// ...
}];
}
- (void)keyboardObserver:(VFKeyboardObserver *)keyboardObserver keyboardWillHideWithProperties:(VFKeyboardProperties)keyboardProperties {
[keyboardObserver animateWithKeyboardProperties:^{
// ...
}];
}
VFKeyboardObserver 有一个方便的方法 animateWithKeyboardProperties:
,它可以执行与键盘动画同步的动画。
如果我们对 UI 进行动画以调整键盘,例如,将 UITextField
附接到键盘顶部,当界面方向改变时会出现一个问题。当界面方向动画时,我们的 UITextField
不会保持附着。请参阅此链接了解详情:http://smnh.me/synchronizing-rotation-animation-between-the-keyboard-and-the-attached-view/
为了解决这个问题,VFKeyboardObserver 有一个 interfaceOrientationWillChange
方法。从 UIViewController 的 viewWillTransitionToSize:withTransitionCoordinator:
调用它。
- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator {
[super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
[[VFKeyboardObserver sharedKeyboardObserver] interfaceOrientationWillChange];
}
在这种情况下,(仅在界面方向改变时)
VFKeyboardObserverDelegate
将只会收到两条消息:keyboardWillShow 和 keyboardDidShow(hide 方法将被忽略)。keyboardWillShow
代理方法的 interfaceOrientationWillChange
参数将为 YES,因此在自定义 UI 调整动画的情况下,您可以在此方法中不执行动画。animateWithKeyboardProperties:
方法自动执行 animations
和 completion
块(如果使用它,则不需要进行更改)。VFKeyboardObserver 通过 CocoaPods 提供。要安装它,只需将以下行添加到您的 Podfile 中:
pod "VFKeyboardObserver"
Valery Fomenko,[email protected]
VFKeyboardObserver遵从MIT许可证。有关更多信息,请参见LICENSE文件。