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

iOS Dev (54) 键盘弹出后收起时View随之移动

时间:2014-06-22 14:56:07      阅读:222      评论:0      收藏:0      [点我收藏+]

标签:style   blog   code   http   tar   ext   

iOS Dev (54) 键盘弹出后收起时View随之移动

-

添加监听

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

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(changeContentViewPosition:)
                                             name:UIKeyboardWillHideNotification
                                           object:nil];

移除监听

[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];

事件处理函数

- (void) changeContentViewPosition:(NSNotification *)notification{

    NSDictionary *userInfo = [notification userInfo];
    NSValue *value = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
    CGFloat keyBoardEndY = value.CGRectValue.origin.y;

    NSNumber *duration = [userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];
    NSNumber *curve = [userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey];

    [UIView animateWithDuration:duration.doubleValue animations:^{
        [UIView setAnimationBeginsFromCurrentState:YES];
        [UIView setAnimationCurve:[curve intValue]];
        self.view.center = CGPointMake(self.view.center.x, keyBoardEndY - STATUS_BAR_HEIGHT - self.view.bounds.size.height/2.0);
    }];
}

Reference

  • http://www.cnblogs.com/programmer-blog/p/3265110.html

iOS Dev (54) 键盘弹出后收起时View随之移动,布布扣,bubuko.com

iOS Dev (54) 键盘弹出后收起时View随之移动

标签:style   blog   code   http   tar   ext   

原文地址:http://blog.csdn.net/prevention/article/details/32324617

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