现在的位置: 首页 > 综合 > 正文

监听键盘事件

2014年06月07日 ⁄ 综合 ⁄ 共 2419字 ⁄ 字号 评论关闭
  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;
        }
    }
    
  3. 监听键盘高度变化
     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWasChange:) name:UIKeyboardDidChangeFrameNotification object:nil];
    
    
       
    - (void)keyboardWasChange:(NSNotification *)aNotification {
    
        NSLog(@"Keyboard change");
    
        NSString *str=[[UITextInputMode currentInputMode] primaryLanguage];
    
        NSLog(@"shurufa--------------%@",str);
    
    //    if ([str isEqualToString:@"zh-Hans"]) {
    
    //        ReplayView.frame = CGRectMake(0, HEIGHT.height-216-125, 320, 45);
    
    //    }else
    
    //    {
    
    //        ReplayView.frame = CGRectMake(0, HEIGHT.height-216-89, 320, 45);
    
    //
    
    //    
    
    //    }
    
        
    
        NSDictionary *info = [aNotification userInfo];
    
        CGSize kbSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;
    
       // CGRect frame = self.search.frame;
    
        if (kbSize.height == 216) {
    
            NSLog(@"english");
    
            ReplayView.frame = CGRectMake(0, HEIGHT.height-216-89, 320, 45);
    
        }
    
        else if(kbSize.height == 252){
    
            NSLog(@"中文");
    
            
    
            ReplayView.frame = CGRectMake(0, HEIGHT.height-216-125, 320, 45);
    
        }
    
    
    }

抱歉!评论已关闭.