关于获取键盘通知信息的处理


1.NSValue:

NSValue提供了简单的容器来包含CObjective-C数据项。可以容纳任何基本数据类型如charintfloatdouble,以及指针,结构体和对象idsNSArrayNSSet集合类对象要求它们的元素为对象类型,NSValue的主要目的是使这些数据类型可以添加至集合中。NSValue对象是不可变类型。 简而言之,NSValue是基本数据类型或自定义数据类型所定义变量的对象包装器。 

注:NSNumber等都是继承于NSValue

这里重点说NSValueUIGeometryExtensions

@interface NSValue (NSValueUIGeometryExtensions)

+ (NSValue *)valueWithCGPoint:(CGPoint)point;
+ (NSValue *)valueWithCGSize:(CGSize)size;
+ (NSValue *)valueWithCGRect:(CGRect)rect;
+ (NSValue *)valueWithCGAffineTransform:(CGAffineTransform)transform;
+ (NSValue *)valueWithUIEdgeInsets:(UIEdgeInsets)insets;
+ (NSValue *)valueWithUIOffset:(UIOffset)insets NS_AVAILABLE_IOS(5_0);

- (CGPoint)CGPointValue;
- (CGSize)CGSizeValue;
- (CGRect)CGRectValue;
- (CGAffineTransform)CGAffineTransformValue;
- (UIEdgeInsets)UIEdgeInsetsValue;
- (UIOffset)UIOffsetValue NS_AVAILABLE_IOS(5_0);

@end

通过这个类别封装CGPoint,CGSize,CGRect等结构体



3.键盘弹出和消失时的通知:

  • UIKeyboardWillShowNotification
  • UIKeyboardDidShowNotification
  • UIKeyboardWillHideNotification
  • UIKeyboardDidHideNotification
  • UIKeyboardWillChangeFrameNotification <ios 5>
  • UIKeyboardDidChangeFrameNotification <ios 5>
  • <changeFrame 待研究>


4.Keyboard Notification User Info Keys

NSString *const UIKeyboardFrameBeginUserInfoKey;
NSString *const UIKeyboardFrameEndUserInfoKey;
NSString *const UIKeyboardAnimationDurationUserInfoKey;
NSString *const UIKeyboardAnimationCurveUserInfoKey;
   
// Deprecated in iOS 3.2 and later.
NSString *const UIKeyboardCenterBeginUserInfoKey;
NSString *const UIKeyboardCenterEndUserInfoKey;
NSString *const UIKeyboardBoundsUserInfoKey;


即目前只能用最上面四个,其余三个不用管。


4.1 捕捉键盘弹出时获取到的NSNotification(pad下):

$1 = 0x25a433f0 NSConcreteNotification 0x25a433f0 {name = UIKeyboardDidShowNotification; userInfo = {

    UIKeyboardAnimationCurveUserInfoKey = 0;

    UIKeyboardAnimationDurationUserInfoKey = "0.25";

    UIKeyboardBoundsUserInfoKey = "NSRect: {{0, 0}, {1024, 352}}";

    UIKeyboardCenterBeginUserInfoKey = "NSPoint: {512, 944}";

    UIKeyboardCenterEndUserInfoKey = "NSPoint: {512, 592}";

    UIKeyboardFrameBeginUserInfoKey = "NSRect: {{768, 0}, {352, 1024}}";

    UIKeyboardFrameChangedByUserInteraction = 0;

    UIKeyboardFrameEndUserInfoKey = "NSRect: {{416, 0}, {352, 1024}}";

}}


4.2属性解释:

UIKeyboardFrameBeginUserInfoKey: 动画前键盘的位置,包含CGRect的NSValue

UIKeyboardFrameEndUserInfoKey:动画结束后的键盘位置,包含CGRect的NSValue


获取方法:

NSDictionary* info = [aNotification userInfo];
CGRect _rect        =[[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];


注意,关于这两个属性,苹果官方文档里面写到:

"The key for an NSValue object containing a CGRect that identifies the start/end frame of the keyboard in screen coordinates. These coordinates do not take into account any rotation factors applied to the window’s contents as a result of interface orientation changes. Thus, you may need to convert the rectangle to window coordinates (using the convertRect:fromWindow: method) or to view coordinates (using the convertRect:fromView: method) before using it."

大意说,这个CGRect不考虑任何旋转,用的时候一定要对这个rect进行坐标转换(convertRect:)。

如:

CGRect _rect        =[[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
CGRect _convertRect=[self.view convertRect:_rect fromView:nil];

(这里找到资料说The first view should be your view. The second view should be nil, meaning window/screen coordinates. 


另外关于通知里默认rect的设备方向,有资料说:

The UIWindow's coordinate system is always in portrait orientation. It applies the rotation by setting its rootViewController's view's transform.



注意:苹果在Text programming Guide for IOS里面说: 

 "The rectangle contained in the UIKeyboardFrameBeginUserInfoKey and UIKeyboardFrameEndUserInfoKey properties of the userInfo dictionary should be used only for the size information it contains. Do not use the origin of the rectangle (which is always {0.0, 0.0}) in rectangle-intersection operations. Because the keyboard is animated into position, the actual bounding rectangle of the keyboard changes over time."

意思是UIKeyboardFrameBeginUserInfoKey,UIKeyboardFrameEndUserInfoKey里面只能用size属性,origin属性由于动画的缘故会随时变化(stack overflow里面有人讨论说有些情况可以用,但是个人认为尽量按照苹果官方文档的做,只用size属性)。



UIKeyboardAnimationDurationUserInfoKey:动画持续时间,数值是NSNumber

UIKeyboardAnimationCurveUserInfoKey:动画曲线类型(UIViewAnimationCurve),数值是NSNumber

附:UIViewAnimationCurve:

typedef enum {
   UIViewAnimationCurveEaseInOut, //淡入淡出,开始时慢,由慢变快,中间最快,然后变慢;
   UIViewAnimationCurveEaseIn,//淡入,开始时慢然后越来越快;
   UIViewAnimationCurveEaseOut,//淡出,开始快然后越来越慢;
   UIViewAnimationCurveLinear//线性匀速,开始和结束是一个速度。
} UIViewAnimationCurve;


5.综述:

//注册,移除通知
- (void)registerForKeyboardNotifications
{
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWasShown:)
                                                 name:UIKeyboardDidShowNotification object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillBeHidden:)
                                                 name:UIKeyboardWillHideNotification object:nil];
    
}

-(void)resignForKeyboardNotifications
{
    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name:UIKeyboardDidShowNotification
                                                  object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name:UIKeyboardWillHideNotification
                                                  object:nil];
}

//方法里对键盘弹出和将要消失做处理
- (void)keyboardWasShown:(NSNotification*)aNotification
{
}

- (void)keyboardWillBeHidden:(NSNotification*)aNotification
{
}



  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值