penn-wang
一位老和尚,他身边聚拢着一帮虔诚的弟子。这一天,他嘱咐弟子每人去南山打一担柴回来。弟子们匆匆行至离山不远的河边,人人目瞪口呆。只见洪水从山上奔泻而下,无论如何也休想渡河打柴了。无功而返,弟子们都有些垂头丧气。唯独一个小和尚与师傅坦然相对。师傅问其故,小和尚从怀中掏出一个苹果,递给师傅说,过不了河,打不了柴,见河边有棵苹果树,我就顺手把树上唯一的一个苹果摘来了。后来,这位小和尚成了师傅的衣钵传人。
#pragma -mark -keyBoardNotification
- (void)addNotification {
//   if(IOS_VERSION<5.0)
//     {
//         [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
//         [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillHideNotification object:nil];
//     }else{
//         [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
//     }
    if (IOS_VERSION>5.0) {
         [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    }
}

- (void)removeNotification
{
    if (IOS_VERSION>5.0) {
        [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
    }
}

- (void)keyboardWillShow:(id)notification
{

    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
#if __IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_3_2
        NSValue *keyboardBoundsValue = [[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey];
#else
        NSValue *keyboardBoundsValue = [[notification userInfo] objectForKey:UIKeyboardBoundsUserInfoKey];
#endif

        CGRect keyboardBounds;
        [keyboardBoundsValue getValue:&keyboardBounds];
        //键盘高度 216  汉字键盘:252
        [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
        [UIView setAnimationBeginsFromCurrentState:YES];
        [UIView setAnimationDuration:0.3];
        //处理移动事件,将各视图设置最终要达到的状态
//        NSInteger offset = self.view.frame.size.height-keyboardBounds.origin.y;
//        NSInteger offset = keyboardBounds.origin.y;
//         CGRect listFrame = CGRectMake(0, -offset, self.view.frame.size.width,self.view.frame.size.height);
//        NSLog(@"keybodarH : %f", keyboardBounds.size.height);
        if([_textFiled isFirstResponder]) {
//            CGFloat lastY = downImageView.frame.origin.y +downImageView.frame.size.height;
//            CGFloat nowY = backgroundView.frame.size.height-keyboardBounds.size.height;
//            CGFloat offset = lastY-nowY;
//            NSLog(@"lastY:%f nowY:%f offset:%f", lastY, nowY,offset);
//            if(offset>0) {
//                backgroundView.frame = CGRectMake(0, 44-offset, 320, 460);
//            }
            if (keyboardBounds.size.height == 216) {
                backgroundView.frame = CGRectMake(0, 4, 320, 460);
            } else {
                backgroundView.frame = CGRectMake(0, 4-36, 320, 460);
            }
        }
//        239.000000 nowY:200.000000 offset:39.000000  1
        [UIView commitAnimations];
    }
}

  

以前的老代码,随便改了改

监听键盘事件。

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];

此通知在以下两种情况都会调用
1:键盘显示
2:当前为英文输入,点击切换为中文输入,(5.0以后,中文输入,键盘上面多出一栏来) 都会调用

英文输入时候键盘高度:216
中文输入时候键盘高度:252

这代码,真它丫乱。 (T_T)
posted on 2013-01-10 15:15  penn-wang  阅读(406)  评论(0编辑  收藏  举报