1.NSValue:
NSValue提供了简单的容器来包含C或Objective-C数据项。可以容纳任何基本数据类型如char,int,float,double,以及指针,结构体和对象ids。NSArray和NSSet集合类对象要求它们的元素为对象类型,NSValue的主要目的是使这些数据类型可以添加至集合中。NSValue对象是不可变类型。 简而言之,NSValue是基本数据类型或自定义数据类型所定义变量的对象包装器。
注:NSNumber等都是继承于NSValue。
这里重点说NSValueUIGeometryExtensions:
通过这个类别封装CGPoint,CGSize,CGRect等结构体
3.键盘弹出和消失时的通知:
- UIKeyboardWillShowNotification
- UIKeyboardDidShowNotification
- UIKeyboardWillHideNotification
- UIKeyboardDidHideNotification
- UIKeyboardWillChangeFrameNotification <ios 5>
- UIKeyboardDidChangeFrameNotification <ios 5>
- <changeFrame 待研究>
4.Keyboard Notification User Info Keys:
即目前只能用最上面四个,其余三个不用管。
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
获取方法:
注意,关于这两个属性,苹果官方文档里面写到:
"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:)。
如:
(这里找到资料说,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:
5.综述: