|
//
// TPKeyboardController.m
// ThePaperDemo
//
// Created by scar1900 on 14/12/4.
// Copyright (c) 2014年 scar1900. All rights reserved.
//
#import "TPKeyboardController.h"
@interface TPKeyboardController ()
@end
@implementation TPKeyboardController
@synthesize responderHeight,keyboardOffset;
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap:)];
[self.keyboardView addGestureRecognizer:tapGesture];
[self.view addSubview:self.keyboardView];
//增加监听,当键盘出现或改变时收出消息
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification
object:nil];
//增加监听,当键退出时收出消息
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillHide:)
name:UIKeyboardWillHideNotification
object:nil];
}
- (UIScrollView *)keyboardView {
if (!_keyboardView) {
_keyboardView = [[UIScrollView alloc]initWithFrame:self.view.bounds];
_keyboardView.scrollEnabled = NO;
_keyboardView.backgroundColor = [UIColor clearColor];
_keyboardView.showsVerticalScrollIndicator = NO;
_keyboardView.showsHorizontalScrollIndicator = NO;
_keyboardView.autoresizingMask = AutoresizingFull;
}
return _keyboardView;
}
/**
* 触摸隐藏键盘
*
* @param sender 触摸手势
*/
- (void)tap:(UITapGestureRecognizer*)sender {
[self closeKeyBoard];
}
#pragma mark - keyBoard event
- (void)closeKeyBoard {
[[UIApplication sharedApplication] sendAction:@selector(resignFirstResponder) to:nil from:nil forEvent:nil];
}
//当键盘出现或改变时调用
- (void)keyboardWillShow:(NSNotification *)aNotification
{
//获取键盘的高度
NSDictionary *userInfo = [aNotification userInfo];
NSValue *aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
CGRect keyboardRect = [aValue CGRectValue];
CGFloat keyboardHeight = keyboardRect.size.height;
if (responderHeight>(CGRectGetHeight(self.keyboardView.frame)-keyboardHeight)) {
keyboardOffset = responderHeight- (CGRectGetHeight(self.keyboardView.frame)-keyboardHeight);
[self.keyboardView setContentOffset:CGPointMake(0, keyboardOffset)];
self.keyboardView.scrollEnabled = YES;
}
}
//当键退出时调用
- (void)keyboardWillHide:(NSNotification *)aNotification
{
CGPoint currentOffset = self.keyboardView.contentOffset;
currentOffset.y = currentOffset.y - keyboardOffset;
[self.keyboardView setContentOffset:currentOffset];
keyboardOffset = 0;
}
#pragma mark - textField Delegate
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
CGRect textRect = textField.frame;
responderHeight = textRect.origin.y+textRect.size.height;
return YES;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[self closeKeyBoard];
return YES;
}
- (void)dealloc {
//增加监听,当键盘出现或改变时收出消息
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIKeyboardWillShowNotification
object:nil];
//增加监听,当键退出时收出消息
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIKeyboardWillHideNotification
object:nil];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
|