Listening for and Reacting to Keyboard Notifications(键盘通知)

来源:互联网 发布:淘宝买家退款率从哪看 编辑:程序博客网 时间:2024/04/29 00:07

  在输入文本时,键盘弹出,遮挡了视图内容,此时可通过键盘通知来使你的UI组件向上或向移动,或对它们进行重组。

  各键盘通知:

     UIKeyboardWillShowNotification:包含user-info dictionary

     UIKeyboardDidShowNotification

     UIKeyboardWillHideNotification:包含user-info dictionary

     UIKeyboardDidHideNotification

 user-info dictionary:

     UIKeyboardAnimationCurveUserInfoKey: 包含NSUInteger类型的NSNumber,封装在NSValue中

     UIKeyboardAnimationDurationUserInfoKey:包含double类型的NSNumber,封装在NSValue中

     UIKeyboardFrameBeginUserInfoKey:CGRect类型封装在NSValue中,标识动画开始之前的keyboard的frame,使用前进行坐标转换

     UIKeyboardFrameEndUserInfoKey:CGRect类型封装在NSValue中,标识动画开始之后的keyboard的frame,使用前进行坐标转换


e.g.

@interface ViewController () <UITextFieldDelegate>

//View里包含ScrollView,ScrollView里包含ImageView,TextField

@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;

@property (weak, nonatomic) IBOutlet UITextField *textField; //此textField的delegate是自身ViewController

@property (weak, nonatomic) IBOutlet UIImageView *imageView;

@end


@implementation ViewController

//UITextFieldDelegte:return Button

- (BOOL) textFieldShouldReturn:(UITextField *)paramTextField{

    [paramTextField  resignFirstResponder];   

    return YES;

}

- (void) viewWillAppear:(BOOL)paramAnimated{

    [super viewWillAppear: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)viewWillDisappear:(BOOL)paramAnimated{

    [super viewWillDisappear:paramAnimated];

    //取消注册

   [[NSNotificationCenter defaultCenter] removeObserver:self];

}

- (void) handleKeyboardWillShow:(NSNotification *)paramNotification{

    

   NSDictionary *userInfo = paramNotification.userInfo;

    

    //获取键盘弹出的动画持续时间,以便后续我们使用相同的时间进行动画呈现内容

    NSValue *animationDurationObject =  userInfo[UIKeyboardAnimationDurationUserInfoKey];

    //获取键盘弹出结束时的frame,是以屏幕坐标形式表示,因此后续使用需要转换成窗体坐标 convertRect:fromView:

    NSValue *keyboardEndRectObject =userInfo[UIKeyboardFrameEndUserInfoKey];

    

    double animationDuration = 0.0;

    CGRect keyboardEndRect = CGRectMake(0.0f, 0.0f, 0.0f, 0.0f);    

    [animationDurationObjectgetValue:&animationDuration];

    [keyboardEndRectObjectgetValue:&keyboardEndRect];

    

    UIWindow *window = [UIApplication sharedApplication].keyWindow;

    keyboardEndRect = [self.view convertRect:keyboardEndRect fromView:window];

    

    /* 键盘覆盖的部分*/

   CGRect intersectionOfKeyboardRectAndWindowRect =  CGRectIntersection(self.view.frame, keyboardEndRect);

    

    /* 滚动视图以显示遮挡内容*/

   [UIView animateWithDuration:animationDuration animations:^{      

       self.scrollView.contentInset = UIEdgeInsetsMake(0.0f,   //上

                                                        0.0f,   //左

                                                        intersectionOfKeyboardRectAndWindowRect.size.height, //下

                                                        0.0f);  //右

    //滚动视图使标识的rect刚好可见

    [self.scrollView  scrollRectToVisible:self.textField.frame animated:NO];

    }];   

}

- (void) handleKeyboardWillHide:(NSNotification *)paramSender{

    

    NSDictionary *userInfo = [paramSender userInfo];  

    NSValue *animationDurationObject =  [userInfo valueForKey:UIKeyboardAnimationDurationUserInfoKey];

   

    double animationDuration = 0.0;   

    [animationDurationObject getValue:&animationDuration];

    

    [UIView animateWithDuration:animationDuration animations:^{

        self.scrollView.contentInset = UIEdgeInsetsZero; //0,0,0,0

    }];   

}


0 0
原创粉丝点击