对 键盘 事件 监听NSNotification 处置相应页面 变化UIKeyboardAnimation
//
// ViewController.h
// UIKeyboardTextNotification
//
// Created by tuxiangqi on 12-8-10.
// Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
@interface ViewController : UIViewController <UITableViewDelegate, UITableViewDataSource, UITextFieldDelegate> {
}
@property (nonatomic, strong) UITableView *myTableView;
@end
//
// ViewController.m
// UIKeyboardTextNotification
//
// Created by tuxiangqi on 12-8-10.
// Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//
#import "ViewController.h"
@implementation ViewController
@synthesize myTableView;
-(void)viewDidLoad{
[super viewDidLoad];
self.myTableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
self.myTableView.delegate = self;
self.myTableView.dataSource = self;
self.myTableView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[self.view addSubview:self.myTableView];
}
-(void)viewDidUnload{
[self setMyTableView:nil];
[super viewDidUnload];
}
//页面出现前,添加监听 键盘事件
- (void) viewDidAppear:(BOOL)paramAnimated{
[super viewDidAppear:paramAnimated];
NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
[center addObserver:self selector:@selector(handleKeyboardWillShow:)
name:UIKeyboardWillShowNotification //键盘将出现 事件监听
object:nil];
[center addObserver:self selector:@selector(handleKeyboardWillHide:)
name:UIKeyboardWillHideNotification //键盘将隐藏 事件监听
object:nil];
}
- (void) viewDidDisappear:(BOOL)paramAnimated {
[super viewDidDisappear:paramAnimated];
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
/* Make sure the Done button on the keyboard for each text field (accessory views of each cell) dismisses the keyboard */ [textField resignFirstResponder];
return YES;
}
- (void) handleKeyboardWillShow:(NSNotification *)paramNotification{
NSDictionary *userInfo = [paramNotification userInfo];
NSValue *animationCurveObject =[userInfo valueForKey:UIKeyboardAnimationCurveUserInfoKey];
NSValue *animationDurationObject =[userInfo valueForKey:UIKeyboardAnimationDurationUserInfoKey];
NSValue *keyboardEndRectObject =[userInfo valueForKey:UIKeyboardFrameEndUserInfoKey];
NSUInteger animationCurve = 0;
double animationDuration = 0.0f;
CGRect keyboardEndRect = CGRectMake(0, 0, 0, 0);
[animationCurveObject getValue:&animationCurve];
[animationDurationObject getValue:&animationDuration];
[keyboardEndRectObject getValue:&keyboardEndRect];
[UIView beginAnimations:@"changeTableViewContentInset"
context:NULL];
[UIView setAnimationDuration:animationDuration];
[UIView setAnimationCurve:(UIViewAnimationCurve)animationCurve];
UIWindow *window = [[[UIApplication sharedApplication] delegate] window];
CGRect intersectionOfKeyboardRectAndWindowRect = CGRectIntersection(window.frame, keyboardEndRect);
CGFloat bottomInset = intersectionOfKeyboardRectAndWindowRect.size.height;
self.myTableView.contentInset = UIEdgeInsetsMake(0.0f,0.0f,bottomInset,0.0f);
NSIndexPath *indexPathOfOwnerCell = nil;
/* Also, make sure the selected text field is visible on the screen */
NSInteger numberOfCells = [self.myTableView.dataSource tableView:self.myTableView
numberOfRowsInSection:0];
/* So let's go through all the cells and find their accessory text fields.
Once we have the refernece to those text fields, we can see which one of
them is the first responder (has the keyboard) and we will make a call
to the table view to make sure after the keyboard is displayed,
that specific cell is NOT obstructed by the keyboard */
for (NSInteger counter = 0;counter < numberOfCells;counter++){
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:counter inSection:0];
UITableViewCell *cell = [self.myTableView cellForRowAtIndexPath:indexPath];
UITextField *textField = (UITextField *)cell.accessoryView;
if ([textField isKindOfClass:[UITextField class]] == NO)
{
continue;
}
if ([textField isFirstResponder])
{
indexPathOfOwnerCell = indexPath;
break;
}
}
[UIView commitAnimations];
if (indexPathOfOwnerCell != nil){
[self.myTableView scrollToRowAtIndexPath:indexPathOfOwnerCell
atScrollPosition:UITableViewScrollPositionMiddle
animated:YES];
}
}
- (void) handleKeyboardWillHide:(NSNotification *)paramNotification{
if (UIEdgeInsetsEqualToEdgeInsets(self.myTableView.contentInset, UIEdgeInsetsZero))
{
/* Our table view's content inset is intact so no need to reset it */
return;
}
NSDictionary *userInfo = [paramNotification userInfo];
NSValue *animationCurveObject =[userInfo valueForKey:UIKeyboardAnimationCurveUserInfoKey];
NSValue *animationDurationObject = [userInfo valueForKey:UIKeyboardAnimationDurationUserInfoKey];
NSValue *keyboardEndRectObject =[userInfo valueForKey:UIKeyboardFrameEndUserInfoKey];
NSUInteger animationCurve = 0;double animationDuration = 0.0f;
CGRect keyboardEndRect = CGRectMake(0, 0, 0, 0);
[animationCurveObject getValue:&animationCurve];
[animationDurationObject getValue:&animationDuration];
[keyboardEndRectObject getValue:&keyboardEndRect];
[UIView beginAnimations:@"changeTableViewContentInset" context:NULL];
[UIView setAnimationDuration:animationDuration];
[UIView setAnimationCurve:(UIViewAnimationCurve)animationCurve]; self.myTableView.contentInset = UIEdgeInsetsZero;[UIView commitAnimations];
}
- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 100;
}
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *result = nil;
static NSString *CellIdentifier = @"CellIdentifier";
result = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (result == nil){
result = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier];
result.selectionStyle = UITableViewCellSelectionStyleNone;
}
result.textLabel.text = [NSString stringWithFormat:@"Cell %ld", (long)indexPath.row];
CGRect accessoryRect = CGRectMake(0.0f,
0.0f,
150.0f,
31.0f);
UITextField *accesssory = [[UITextField alloc] initWithFrame:accessoryRect];
accesssory.borderStyle = UITextBorderStyleRoundedRect;
accesssory.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
accesssory.placeholder = @"Enter Text";
accesssory.delegate = self;
result.accessoryView = accesssory;
return result;
}
@end
- 1楼lixing333前天 14:52
- 系统自带的Notification Name,除了关于键盘的,还有没有其他的Notification?我没有找到相关资料
- Re: a21064346昨天 10:02
- 回复lixing333nn官方的Documentation 里面涉及到的控件Notification 很少。除了键盘,还有一个是UIWindows.当然,还有一些其他的系统里又的notification,这个需要进行相关Document搜索。不过常用的就前面说的两个。一般 Notification 都是自己去自定义和管理收与发以及撤销。