Chinaunix首页 | 论坛 | 博客
  • 博客访问: 189943
  • 博文数量: 60
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 385
  • 用 户 组: 普通用户
  • 注册时间: 2013-02-19 21:43
个人简介

readonly

文章分类

全部博文(60)

文章存档

2013年(60)

我的朋友

分类: iOS平台

2013-02-20 09:17:36

  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;
    }
    }
阅读(2197) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~