对 键盘 事件 监听NSNotification 处理相应页面 变化UIKeyboardAnimation

转载请说明(谢谢)

http://blog.csdn.net/a21064346/article/details/7852684

点击打开链接


以下是通知的收发,直接copy到你的项目中即可。应该不会有什么bug。

自己搭建 app delegate,window =viewController.View即可


//

//  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




  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值