UIKeyboard

来源:互联网 发布:淘宝优惠券推广渠道 编辑:程序博客网 时间:2024/06/05 10:19

其实标题是存在问题的,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 CGRectUIKIT_EXTERN NSString *const UIKeyboardFrameEndUserInfoKey          NS_AVAILABLE_IOS(3_2); // NSValue of CGRectUIKIT_EXTERN NSString *const UIKeyboardAnimationDurationUserInfoKey NS_AVAILABLE_IOS(3_0); // NSNumber of doubleUIKIT_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






0 0
原创粉丝点击