Add dynamic buttons on iOS virtual keyboard

Source: Internet
Author: User

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];
}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.