Previously, the article "add a dynamic hidden button" on the iOS virtual keyboard described how to add a dynamic button on the keyboard. It was found that the buttons on the keyboard were stiff when they were displayed, here we have made improvements, added an animation transition, and changed the image to make people feel that the buttons are displayed with the keyboard animation, and exit with the keyboard animation, it looks smoother;
:
-(Void) viewDidLoad
{
NSLog (@ "% @", NSStringFromSelector (_ cmd ));
[Super viewDidLoad];
ExitButton = [UIButton buttonWithType: UIButtonTypeCustom];
[ExitButton setImage: [UIImage imageNamed: @ "down.png"] forState: UIControlStateNormal];
CGRect exitBtFrame = CGRectMake (self. view. frame. size. width-48, self. view. frame. size. height, 48366f, 30366f );
[ExitButton setFrame: exitBtFrame];
[Self. view addSubview: exitButton];
[[Nsicationcenter center defacenter center] addObserver: self selector: @ selector (handleKeyboardDidShow :) name: UIKeyboardWillShowNotification object: nil];
[[Nsicationcenter center defacenter center] addObserver: self selector: @ selector (handleKeyboardWillHide :) name: UIKeyboardWillHideNotification object: nil];
}
-(Void) handleKeyboardDidShow :( NSNotification *) notification
{
NSLog (@ "% @", NSStringFromSelector (_ cmd ));
NSDictionary * info = [notification userInfo];
NSLog (@ "--> info: % @", info );
CGRect keyboardFrame;
[[Info objectForKey: UIKeyboardFrameEndUserInfoKey] getValue: & keyboardFrame];
CGSize kbSize = [[info objectForKey: UIKeyboardFrameEndUserInfoKey] CGRectValue]. size;
NSValue * animationDurValue = [info objectForKey: UIKeyboardAnimationDurationUserInfoKey];
NSTimeInterval animationDuration;
// Copy value
[AnimationDurValue getValue: & animationDuration];
// Add an animation when the keyboard pops up
[UIView beginAnimations: @ "animal" context: nil];
[UIView setAnimationDuration: animationDuration];
CGFloat distanceToMove = kbSize. height;
NSLog (@ "----> dynamic keyboard Height: % f", distanceToMove );
[Self adjustPanelsWithKeyBordHeight: distanceToMove];
[UIView commitAnimations];
ExitButton. hidden = NO;
[ExitButton addTarget: self action: @ selector (CancelBackKeyboard :) forControlEvents: UIControlEventTouchDown];
}
-(Void) handleKeyboardWillHide :( NSNotification *) notification
{
NSLog (@ "% @", NSStringFromSelector (_ cmd ));
NSDictionary * info = [notification userInfo];
CGRect keyboardFrame;
[[Info objectForKey: UIKeyboardFrameEndUserInfoKey] getValue: & keyboardFrame];
NSValue * animationDurValue = [info objectForKey: UIKeyboardAnimationDurationUserInfoKey];
NSTimeInterval animationDuration;
// Copy the animationDurvalue to the animationDuration.
[AnimationDurValue getValue: & animationDuration];
[UIView beginAnimations: @ "animal" context: nil];
[UIView setAnimationDuration: animationDuration];
If (exitButton ){
CGRect exitBtFrame = CGRectMake (self. view. frame. size. width-48, self. view. frame. size. height, 48366f, 30366f );
ExitButton. frame = exitBtFrame;
[Self. view addSubview: exitButton];
}
[UIView commitAnimations];
}
-(Void) adjustPanelsWithKeyBordHeight :( float) height
{
NSLog (@ "% @", NSStringFromSelector (_ cmd ));
If (exitButton ){
CGRect exitBtFrame = CGRectMake (self. view. frame. size. width-48, self. view. frame. size. height-30, 48366f, 30366f );
ExitButton. frame = exitBtFrame;
[Self. view addSubview: exitButton];
}
}
-(Void) CancelBackKeyboard :( id) sender
{
NSLog (@ "% @", NSStringFromSelector (_ cmd ));
[TextField resignFirstResponder];
}
-(Void) viewDidUnload
{
[Self setTextField: nil];
ExitButton = nil;
[Super viewDidUnload];
// Release any retained subviews of the main view.
}
-(BOOL) shouldAutorotateToInterfaceOrientation :( UIInterfaceOrientation) interfaceOrientation
{
Return (interfaceOrientation! = UIInterfaceOrientationPortraitUpsideDown );
}
-(Void) dealloc {
[TextField release];
[ExitButton release];
[[Nsicationcenter center defacenter center] removeObserver: self];
[Super dealloc];
}