IOS键盘隐藏和显示调用
2013-04-28 22:09
274 查看
//
// 动画和事件综合例子-键盘处理
#import "ScrollView.h"
@interface MJScrollView () {
CGPoint _lastOffset;
}
@end
@implementation MJScrollView
#pragma mark - 生命周期方法
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self initial];
}
return self;
}
- (id)init {
if (self = [super init]) {
[self initial];
}
return self;
}
#pragma mark 当ScrollView从xib中创建完毕后会调用这个方法
- (void)awakeFromNib {
[self initial];
}
- (void)dealloc {
NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
// 注意:记得要移除
[center removeObserver:self];
[super dealloc];
}
#pragma mark 初始化
- (void)initial {
self.contentSize = self.bounds.size;
NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
// 注册键盘显示的通知
[center addObserver:self selector:@selector(keybordWillShow:) name:UIKeyboardWillShowNotification object:nil];
// 注册键盘隐藏的通知
[center addObserver:self selector:@selector(keybordWillHide:) name:UIKeyboardWillHideNotification object:nil];
}
#pragma mark 键盘显示出来的时候调用
- (void)keybordWillShow:(NSNotification *)notification{
//NSLog(@"keybordWillShow,%@", notification);
CGRect keyboardRect = [[notification.userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
UITextField *textField = [self findFistResponder:self];
// toView用nil值,代表UIWindow
CGRect convertRect = [textField convertRect:textField.bounds toView:nil];
CGFloat distance = keyboardRect.origin.y - (convertRect.origin.y + convertRect.size.height + 10);
if (distance < 0) { // 说明键盘挡住了文本框
[self animationWithUserInfo:notification.userInfo block:^{
CGPoint offset = _lastOffset = self.contentOffset;
offset.y -= distance;
self.contentOffset = offset;
}];
}
}
#pragma mark 键盘隐藏的时候调用
- (void)keybordWillHide:(NSNotification *)notification {
[self animationWithUserInfo:notification.userInfo block:^{
self.contentOffset = _lastOffset;
}];
}
#pragma mark 抽出一个方法来执行动画
- (void)animationWithUserInfo:(NSDictionary *)userInfo
block:(void (^)(void))block {
// 取出键盘弹出的时间
CGFloat duration = [[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue];
// 取出键盘弹出的速率节奏
int curve = [[userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey] intValue];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:duration];
[UIView setAnimationCurve:curve];
// 调用block
block();
[UIView commitAnimations];
}
#pragma mark 递归找出第一响应者
- (UITextField *)findFistResponder:(UIView *)view {
for (UIView *child in view.subviews) {
if ([child respondsToSelector:@selector(isFirstResponder)]
&&
[child isFirstResponder]) {
return (UITextField *)child;
}
UITextField *field = [self findFistResponder:child];
if (field) {
return field;
}
}
return nil;
}
#pragma mark 监听scrollview点击
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
// 退出键盘
[self endEditing:YES];
}
@end
// 动画和事件综合例子-键盘处理
#import "ScrollView.h"
@interface MJScrollView () {
CGPoint _lastOffset;
}
@end
@implementation MJScrollView
#pragma mark - 生命周期方法
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self initial];
}
return self;
}
- (id)init {
if (self = [super init]) {
[self initial];
}
return self;
}
#pragma mark 当ScrollView从xib中创建完毕后会调用这个方法
- (void)awakeFromNib {
[self initial];
}
- (void)dealloc {
NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
// 注意:记得要移除
[center removeObserver:self];
[super dealloc];
}
#pragma mark 初始化
- (void)initial {
self.contentSize = self.bounds.size;
NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
// 注册键盘显示的通知
[center addObserver:self selector:@selector(keybordWillShow:) name:UIKeyboardWillShowNotification object:nil];
// 注册键盘隐藏的通知
[center addObserver:self selector:@selector(keybordWillHide:) name:UIKeyboardWillHideNotification object:nil];
}
#pragma mark 键盘显示出来的时候调用
- (void)keybordWillShow:(NSNotification *)notification{
//NSLog(@"keybordWillShow,%@", notification);
CGRect keyboardRect = [[notification.userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
UITextField *textField = [self findFistResponder:self];
// toView用nil值,代表UIWindow
CGRect convertRect = [textField convertRect:textField.bounds toView:nil];
CGFloat distance = keyboardRect.origin.y - (convertRect.origin.y + convertRect.size.height + 10);
if (distance < 0) { // 说明键盘挡住了文本框
[self animationWithUserInfo:notification.userInfo block:^{
CGPoint offset = _lastOffset = self.contentOffset;
offset.y -= distance;
self.contentOffset = offset;
}];
}
}
#pragma mark 键盘隐藏的时候调用
- (void)keybordWillHide:(NSNotification *)notification {
[self animationWithUserInfo:notification.userInfo block:^{
self.contentOffset = _lastOffset;
}];
}
#pragma mark 抽出一个方法来执行动画
- (void)animationWithUserInfo:(NSDictionary *)userInfo
block:(void (^)(void))block {
// 取出键盘弹出的时间
CGFloat duration = [[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue];
// 取出键盘弹出的速率节奏
int curve = [[userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey] intValue];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:duration];
[UIView setAnimationCurve:curve];
// 调用block
block();
[UIView commitAnimations];
}
#pragma mark 递归找出第一响应者
- (UITextField *)findFistResponder:(UIView *)view {
for (UIView *child in view.subviews) {
if ([child respondsToSelector:@selector(isFirstResponder)]
&&
[child isFirstResponder]) {
return (UITextField *)child;
}
UITextField *field = [self findFistResponder:child];
if (field) {
return field;
}
}
return nil;
}
#pragma mark 监听scrollview点击
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
// 退出键盘
[self endEditing:YES];
}
@end
相关文章推荐
- ios 利用通知处理键盘的显示与隐藏
- iOS - 模拟器键盘弹出、隐藏、不显示
- EditText的换行输入、输入起始位置、调用指定格式的系统键盘(显示和隐藏),光标颜色,密码动态设置可见性
- iOS 键盘处理(改变键盘为完成键),UITextField键盘显示隐藏,弹出,回弹
- IOS开发键盘显示、隐藏事件(用于…
- iOS、Xcode监测键盘的显示和隐藏变化,并获得键盘高度,改变tableView的frame和偏移
- iOS开发中的键盘显示和隐藏
- iOS -键盘显示与隐藏
- iOS中监控软键盘显示或隐藏的可靠方法
- iOS、Xcode监测键盘的显示和隐藏变化,并获得键盘高度,改变tableView的frame和偏移
- iOS中监控软键盘显示或隐藏的可靠方法
- iOS中监控软键盘显示或隐藏的可靠方法
- IOS开发——键盘显示/隐藏事件监控
- iOS中键盘显示与隐藏的套路模版
- ios 深入讲解iOS键盘一:控制键盘隐藏显示
- 系统键盘调用 显示与隐藏
- iOS之 利用通知(NSNotificationCenter)获取键盘的高度,以及显示和隐藏键盘时修改界面的注意事项
- iOS强制横屏+键盘横屏显示+中途退出再进入也是横屏
- iOS隐藏键盘代码
- react native ios 解决键盘无法隐藏