码迷,mamicode.com
首页 > 移动开发 > 详细

iOS全局处理键盘事件

时间:2016-06-07 23:57:23      阅读:282      评论:0      收藏:0      [点我收藏+]

标签:

转自:http://www.cnblogs.com/xinus/archive/2013/01/22/ios-keybord-notification.html

  1. 注册监听键盘事件的通知
    技术分享
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(keyboardWillShow:)
                                                     name:UIKeyboardWillShowNotification
                                                   object:nil];
        
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(keyboardShow:)
                                                     name:UIKeyboardDidShowNotification
                                                   object:nil];
        
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(keyboardWillHide:)
                                                     name:UIKeyboardWillHideNotification
                                                   object:nil];
        
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(keyboardHide:)
                                                     name:UIKeyboardDidHideNotification
                                                   object:nil];
    技术分享
  2. 在键盘将要出现和隐藏的回调中,加入动画。
    技术分享
    - (void)keyboardWillShow:(NSNotification *)notif {
        if (self.hidden == YES) {
            return;
        }
        
        CGRect rect = [[notif.userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
        CGFloat y = rect.origin.y;
        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationDuration:0.25];
        NSArray *subviews = [self subviews];
        for (UIView *sub in subviews) {
            
            CGFloat maxY = CGRectGetMaxY(sub.frame);
            if (maxY > y - 2) {
                sub.center = CGPointMake(CGRectGetWidth(self.frame)/2.0, sub.center.y - maxY + y - 2);
            }
        }
        [UIView commitAnimations];
    }
    
    - (void)keyboardShow:(NSNotification *)notif {
        if (self.hidden == YES) {
            return;
        }
    }
    
    - (void)keyboardWillHide:(NSNotification *)notif {
        if (self.hidden == YES) {
            return;
        }
        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationDuration:0.25];
        NSArray *subviews = [self subviews];
        for (UIView *sub in subviews) {
            if (sub.center.y < CGRectGetHeight(self.frame)/2.0) {
                sub.center = CGPointMake(CGRectGetWidth(self.frame)/2.0, CGRectGetHeight(self.frame)/2.0);
            }
        }
        [UIView commitAnimations];
    }
    
    - (void)keyboardHide:(NSNotification *)notif {
        if (self.hidden == YES) {
            return;
        }
    }
    技术分享

     

iOS全局处理键盘事件

标签:

原文地址:http://www.cnblogs.com/feiyu-mdm/p/5568562.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!