UIKeyboard

其实标题是存在问题的,iOS并不提供UIKeyboard这个类的,但是提供了UIKeyboardAppearance , UIKeyboardType这种枚举类型,很显然了就是UI层面上键盘的类型罢了。另外也是这里要说的重点:

关于键盘的全局通知常量值

// Each notification includes a nil object and a userInfo dictionary containing the
// begining and ending keyboard frame in screen coordinates. Use the various UIView and
// UIWindow convertRect facilities to get the frame in the desired coordinate system.
// Animation key/value pairs are only available for the "will" family of notification.
UIKIT_EXTERN NSString *const UIKeyboardWillShowNotification;
UIKIT_EXTERN NSString *const UIKeyboardDidShowNotification; 
UIKIT_EXTERN NSString *const UIKeyboardWillHideNotification; 
UIKIT_EXTERN NSString *const UIKeyboardDidHideNotification;

关于键盘的全局常量Key

UIKIT_EXTERN NSString *const UIKeyboardFrameBeginUserInfoKey        NS_AVAILABLE_IOS(3_2); // NSValue of CGRect
UIKIT_EXTERN NSString *const UIKeyboardFrameEndUserInfoKey          NS_AVAILABLE_IOS(3_2); // NSValue of CGRect
UIKIT_EXTERN NSString *const UIKeyboardAnimationDurationUserInfoKey NS_AVAILABLE_IOS(3_0); // NSNumber of double
UIKIT_EXTERN NSString *const UIKeyboardAnimationCurveUserInfoKey    NS_AVAILABLE_IOS(3_0); // NSNumber of NSUInteger (UIViewAnimationCurve)

文档中的解释已经很清楚了。这几个通知传递的是nil对象,但是包含着下面的常量Key作为userInfo字典的值。而且明确地是作为界面frame和坐标值服务的,同时动画key/values对只会对“will”通知簇有效。



接触到这一知识点是因为在学习仿微信的聊天界面的时候,点击输入框,整体界面随着键盘的上“弹”,而刚开始的想法是单纯的点击textField触发代理时间从二手动设置Frame。然而参考别人的代码,原来有这么神奇的实现:

注册通知:

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardHide:) name:UIKeyboardWillHideNotification object:nil];
}


通知操作:

- (void)keyboardShow:(NSNotification *)note
{
    CGRect keyBoardRect = [note.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
    CGFloat deltaY = keyBoardRect.size.height;
    
    [UIView animateWithDuration:[note.userInfo[UIKeyboardAnimationDurationUserInfoKey] floatValue] animations:^{
       
        self.view.transform = CGAffineTransformMakeTranslation(0, -deltaY);
    }];
}

- (void)keyboardHide:(NSNotification *)note
{
    [UIView animateWithDuration:[note.userInfo[UIKeyboardAnimationDurationUserInfoKey] floatValue] animations:^{
        self.view.transform = CGAffineTransformIdentity;
    }];
}

简单分析一下代码可以知道:通过所提供的key可以到上下偏移的偏差值deltaY(键盘的高度), 甚至还提供了键盘弹出来所需要的时间


如果缩回键盘,就是将view的transform属性设置回CGAffineTransformIdentity (相当于原点值的东西)


不过其实亲测了,动画的效果也不是很明显的



参考:动画的实现用旧的方法比较繁琐

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






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值