对 键盘 事件 监听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{
[superviewDidLoad];
self.myTableView = [[UITableViewalloc] initWithFrame:self.view.boundsstyle:UITableViewStyleGrouped];
self.myTableView.delegate =self;
self.myTableView.dataSource =self;
self.myTableView.autoresizingMask =UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[self.viewaddSubview:self.myTableView];
}
-(void)viewDidUnload{
[selfsetMyTableView:nil];
[superviewDidUnload];
}
//页面出现前,添加监听键盘事件
- (void) viewDidAppear:(BOOL)paramAnimated{
[superviewDidAppear:paramAnimated];
NSNotificationCenter *center = [NSNotificationCenterdefaultCenter];
[center addObserver:self selector:@selector(handleKeyboardWillShow:)
name:UIKeyboardWillShowNotification //键盘将出现事件监听
object:nil];
[center addObserver:self selector:@selector(handleKeyboardWillHide:)
name:UIKeyboardWillHideNotification //键盘将隐藏事件监听
object:nil];
}
- (void) viewDidDisappear:(BOOL)paramAnimated {
[superviewDidDisappear:paramAnimated];
[[NSNotificationCenterdefaultCenter] 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 */ [textFieldresignFirstResponder];
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);
[animationCurveObjectgetValue:&animationCurve];
[animationDurationObjectgetValue:&animationDuration];
[keyboardEndRectObjectgetValue:&keyboardEndRect];
[UIViewbeginAnimations:@"changeTableViewContentInset"
context:NULL];
[UIViewsetAnimationDuration:animationDuration];
[UIViewsetAnimationCurve:(UIViewAnimationCurve)animationCurve];
UIWindow *window = [[[UIApplicationsharedApplication] 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.dataSourcetableView: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.myTableViewcellForRowAtIndexPath:indexPath];
UITextField *textField = (UITextField *)cell.accessoryView;
if ([textField isKindOfClass:[UITextFieldclass]] == NO)
{
continue;
}
if ([textField isFirstResponder])
{
indexPathOfOwnerCell = indexPath;
break;
}
}
[UIViewcommitAnimations];
if (indexPathOfOwnerCell != nil){
[self.myTableViewscrollToRowAtIndexPath: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);
[animationCurveObjectgetValue:&animationCurve];
[animationDurationObjectgetValue:&animationDuration];
[keyboardEndRectObjectgetValue:&keyboardEndRect];
[UIViewbeginAnimations:@"changeTableViewContentInset"context:NULL];
[UIViewsetAnimationDuration:animationDuration];
[UIViewsetAnimationCurve:(UIViewAnimationCurve)animationCurve];self.myTableView.contentInset =UIEdgeInsetsZero;[UIViewcommitAnimations];
}
- (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 = [tableViewdequeueReusableCellWithIdentifier:CellIdentifier];
if (result == nil){
result = [[UITableViewCellalloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier];
result.selectionStyle =UITableViewCellSelectionStyleNone;
}
result.textLabel.text = [NSStringstringWithFormat:@"Cell %ld", (long)indexPath.row];
CGRect accessoryRect = CGRectMake(0.0f,
0.0f,
150.0f,
31.0f);
UITextField *accesssory = [[UITextFieldalloc] 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 都是自己去自定义和管理收与发以及撤销。