iOS開発中のキーボード高度変化処理
227315 ワード
転載:http://blog.unieagle.net/2012/06/04/ios開発中のキーボードの高度変化処理/
ios開発ではキーボードがよく使われています.sdkバージョン5.0の前に、キーボードの高さは固定値216 pxである.5.0が出てきたら、キーボードの高さはキーボード言語によって変化します.この場合、一般的にはインタフェースの再配置が必要です.方法はNSNotificationCenterを利用することです.
使い方は、まずnotificationに観察者を登録します.例えば、
ios開発ではキーボードがよく使われています.sdkバージョン5.0の前に、キーボードの高さは固定値216 pxである.5.0が出てきたら、キーボードの高さはキーボード言語によって変化します.この場合、一般的にはインタフェースの再配置が必要です.方法はNSNotificationCenterを利用することです.
UIKeyboardWillShowNotification;
UIKeyboardDidShowNotification;
UIKeyboardWillHideNotification;
UIKeyboardDidHideNotification;
これらのnotificationは5.0 sdk前からあって、名前の通り意味が分かります.UIKeyboardWillChangeFrameNotification __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_5_0);
UIKeyboardDidChangeFrameNotification __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_5_0);
この二つはsdk 5.0以降に出てきたもので、キーボードの高さの変化を処理します.使い方は、まずnotificationに観察者を登録します.例えば、
if([[[UIDevice currentDevice] systemVersion] floatValue] >= 5.0) {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChangeFrame:) name:UIKeyboardWillChangeFrameNotification object:nil];
}
キーボードの高さが変わると、通知を受けます.キーボードの現在の高さと変化の目標高さを教えてくれます.-(void)keyboardWillChangeFrame:(NSNotification*)notif{
#if __IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_3_2
NSValue *keyboardBoundsValue = [[notif userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey];
#else
NSValue *keyboardBoundsValue = [[notif userInfo] objectForKey:UIKeyboardBoundsUserInfoKey];
#endif
CGRect keyboardEndRect = [keyboardBoundsValue CGRectValue];
CGRect inputFrame = self.feedBackTextView.frame;
//kb 216 vs textFrame 185
float delta = keyboardEndRect.size.height - 216;
float originalHeight = inputFrame.size.height;
inputFrame.size.height = 185 - delta;
if (inputFrame.size.height != originalHeight) {
self.feedBackTextView.frame = inputFrame;
self.feedBackBackgroundView.frame = inputFrame;
}
}
他のいくつかのnotifications.userInfoから取得できるkeyは以下の通りです.UIKeyboardFrameBeginUserInfoKey // NSValue of CGRect
UIKeyboardFrameEndUserInfoKey // NSValue of CGRect
UIKeyboardAnimationDurationUserInfoKey // NSNumber of double
UIKeyboardAnimationCurveUserInfoKey // NSNumber of double
notifにおけるuserInfoの完全情報は以下の通りである.keyboardChange:{
UIKeyboardAnimationCurveUserInfoKey = 0;
UIKeyboardAnimationDurationUserInfoKey = "0.25";
UIKeyboardBoundsUserInfoKey = "NSRect: {{0, 0}, {320, 216}}";
UIKeyboardCenterBeginUserInfoKey = "NSPoint: {160, 372}";
UIKeyboardCenterEndUserInfoKey = "NSPoint: {160, 588}";
UIKeyboardFrameBeginUserInfoKey = "NSRect: {{0, 264}, {320, 216}}";
UIKeyboardFrameChangedByUserInteraction = 0;
UIKeyboardFrameEndUserInfoKey = "NSRect: {{0, 480}, {320, 216}}";
}
以下は完全なソリューションです.ユーザーはキーボードの高さの入念な変化を知る必要があります.#pragma mark Keyboard
-(void)keyboardWillChangeFrame:(NSNotification*)notif{
NSLog(@"keyboardChange:%@",[notif userInfo]);
float keyboadHeightBegin = 0;
float keyboadHeightEnd = 0;
float animationDuration = [[[notif userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue];
UIViewAnimationCurve animationCurve = [[[notif userInfo] objectForKey:UIKeyboardAnimationCurveUserInfoKey] unsignedIntegerValue];
#if __IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_3_2
CGRect keyboardBeginFrame = [[[notif userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue];
CGRect keyboardEndFrame = [[[notif userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
keyboadHeightBegin = 480 - keyboardBeginFrame.origin.y;
keyboadHeightEnd = 480 - keyboardEndFrame.origin.y;
#else
//these deprecated after iOS 3.2
CGRect keyboardBounds = [[[notif userInfo] objectForKey:UIKeyboardBoundsUserInfoKey] CGRectValue];
CGPoint keybordCenterBegin = [[[notif userInfo] objectForKey:UIKeyboardCenterBeginUserInfoKey] CGPointValue];
CGPoint keybordCenterEnd = [[[notif userInfo] objectForKey:UIKeyboardCenterEndUserInfoKey] CGPointValue];
keyboadHeightBegin = 480 - (keybordCenterBegin.y - keyboardBounds.size.height / 2);
keyboadHeightEnd = 480 - (keybordCenterEnd.y - keyboardBounds.size.height / 2);
#endif
NSLog(@"keyboardHeightChangeFrom:%.2f,To:%.2f",keyboadHeightBegin,keyboadHeightEnd);
return;
if (keyboadHeightEnd > 0) {
//keyboard show or change frame
[UIView animateWithDuration:animationDuration delay:0 options:animationCurve animations:^{
} completion:^(BOOL finished) {
}];
} else {
//keyboard hide
}
}
-(void)keyboardDidChangeFrame:(NSNotification*)notif{
//info like willChangeFrame
}
-(void)keyboardWillShow:(NSNotification*)notif{
//keyboard height will be 216, on iOS version older than 5.0
[UIView animateWithDuration:0.3f animations:^{
self.contentTableView.height = 480 - 44 - 216;
}];
}
-(void)keyboardWillHide:(NSNotification*)notif{
[UIView animateWithDuration:0.3f animations:^{
self.contentTableView.height = 480 - 44 - 28;
}];
}
-(void)registerKeyboardEvent{
float systemVer = [[[UIDevice currentDevice] systemVersion] floatValue];
if(systemVer >= 5.0) {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChangeFrame:) name:UIKeyboardWillChangeFrameNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidChangeFrame:) name:UIKeyboardDidChangeFrameNotification object:nil];
} else {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}
}
-(void)unregisterKeyboardEvent{
if([[[UIDevice currentDevice] systemVersion] floatValue] > 5.0) {
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillChangeFrameNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardDidChangeFrameNotification object:nil];
} else {
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}
}
以下の解決策はキーボードの出現と消失の処理だけを考えます.#pragma mark Keyboard
-(void)keyboardWillShow:(NSNotification*)notif{
//keyboard height will be 216, on iOS version older than 5.0
CGRect boxFrame = self.loginBoxView.frame;
boxFrame.origin.y = 50;
[UIView animateWithDuration:0.3f animations:^{
self.loginBoxView.frame = boxFrame;
}];
}
-(void)keyboardWillHide:(NSNotification*)notif{
CGRect boxFrame = self.loginBoxView.frame;
boxFrame.origin.y = 216;
[UIView animateWithDuration:0.3f animations:^{
self.loginBoxView.frame = boxFrame;
}];
}
// viewdidload
-(void)registerKeyboardEvent{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}
// viewdidunload
-(void)unregisterKeyboardEvent{
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
}