I have written the below Logic for moving a view upwards when a textfiled begins its editing.It is working fine in iOS 7 but it is not working in iOS 8.
Code:
-(void)viewDidLoad
{
_leagalName.delegate = self;
_phoneNumber.delegate = self;
_email.delegate = self;
_contactName.delegate = self;
_contactNumber.delegate = self;
_password.delegate = self;
_confirmPassword.delegate = self;
}
-(void)animateTextField:(UITextField*)textField up:(BOOL)up
{
const float movementDuration = 0.3f; // tweak as needed
int movement = (up ? movementDistance : -movementDistance);
[UIView beginAnimations: #"animateTextField" context: nil];
[UIView setAnimationBeginsFromCurrentState: YES];
[UIView setAnimationDuration: movementDuration];
self.sampleView.frame = CGRectOffset(self.sampleView.frame, 0, movement);
[UIView commitAnimations];
}
-(void)textFieldDidBeginEditing:(UITextField *)textField
{
if([textField isEqual:_leagalName])
{
movementDistance = 0;
[self animateTextField:textField up:YES];
}
else if([textField isEqual:_phoneNumber])
{
movementDistance = 0;
[self animateTextField:textField up:YES];
}
else if([textField isEqual:_email])
{
movementDistance = -40;
[self animateTextField:textField up:YES];
}
else if([textField isEqual:_contactName])
{
movementDistance = -80;
[self animateTextField:textField up:YES];
}
else if([textField isEqual:_contactNumber])
{
movementDistance = -110;
[self animateTextField:textField up:YES];
}
else if([textField isEqual:_password])
{
movementDistance = -140;
[self animateTextField:textField up:YES];
}
else if([textField isEqual:_confirmPassword])
{
movementDistance = -170;
[self animateTextField:textField up:YES];
}
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
[self animateTextField:textField up:NO];
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
Can anyone please explain how i can move view upwards in iOS 8.
in iOS8, it is not recommended to change frame during animation for auto-layout case.
The common method used by most developers would be updating the constrains accordingly in your animation block.
If you still want to try the set frame method, probably you can disable auto-layout and give a try.
Please use following methods for updating you UI
- (void)keyboardDidShow:(NSNotification *)note
{
[UIView animateWithDuration:0.5f animations:^ {
if([super isIpad]){
self.view.frame = CGRectMake(0, -350, 768, 1024);
}
else{
self.view.frame = CGRectMake(0, -275, 320, 568);
}
}];
}
-(void)keyboardDidhide:(NSNotification *)note
{
[UIView animateWithDuration:0.5f animations:^ {
if([super isIpad]){
self.view.frame = CGRectMake(0, 0, 768, 1024);
}
else{
self.view.frame = CGRectMake(0, 0, 320, 568);
}
}];
}
remember that i am using [UIWindow sharedWindow] to draw my nib, you mush be using your screen size and secondly please add following code in your viewDidLoad method.
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardDidShow:) name:UIKeyboardDidShowNotification object:nil];
`[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardDidhide:) name:UIKeyboardDidHideNotification object:nil];
`
Related
I'm using the below code to shift a view and my tableview up when my keyboard is activated. When the keyboard is closed however, it takes the upView a solid 2 seconds after the keyboard closes to return to where it was (the tableView on the other hand, is instant). Why is this happening?
- (void)viewDidLoad {
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardWillChange:) name:UIKeyboardWillShowNotification object:nil];
}
- (void)keyboardWillChange:(NSNotification *)notification {
NSDictionary* keyboardInfo = [notification userInfo];
NSValue* keyboardFrameBegin = [keyboardInfo valueForKey:UIKeyboardFrameEndUserInfoKey];
CGRect keyboardFrameBeginRect = [keyboardFrameBegin CGRectValue];
UITabBarController *tabBarController = [UITabBarController new];
CGFloat tabBarHeight = tabBarController.tabBar.frame.size.height;
self.keyboardHeight = keyboardFrameBeginRect.size.height - tabBarHeight;
}
- (void) animateTextView:(BOOL) up
{
const int movementDistance = self.keyboardHeight;
const float movementDuration = 0.2f;
int movement= movement = (up ? -movementDistance : movementDistance);
[UIView beginAnimations: #"anim" context: nil];
[UIView setAnimationBeginsFromCurrentState: YES];
[UIView setAnimationDuration: movementDuration];
self.upView.frame = CGRectOffset(self.upView.frame, 0, movement);
[UIView setAnimationDidStopSelector:#selector(afterAnimationStops)];
[UIView commitAnimations];
self.tableView.frame = CGRectOffset(self.tableView.frame, 0, movement);
[UIView setAnimationDidStopSelector:#selector(afterAnimationStops)];
[UIView commitAnimations];
}
- (void)textViewDidBeginEditing:(UITextView *)textView
{
[self animateTextView:YES];
}
- (void)textViewDidEndEditing:(UITextView *)textView
{
[self animateTextView:NO];
}
UPDATED CODE
.m
- (void)handleKeyboard:(NSNotification*)aNotification{
NSDictionary* info = [aNotification userInfo];
NSValue* value = [info objectForKey:UIKeyboardAnimationDurationUserInfoKey];
NSTimeInterval duration = 3;
[value getValue:&duration];
if (aNotification.name == UIKeyboardWillHideNotification) {
/** KEYBOARD HIDE **/
[UIView animateWithDuration:0 animations:^{ self.upView.frame = CGRectOffset(self.upView.frame, 0, self.keyboardHeight); self.tableView.frame = CGRectOffset(self.tableView.frame, 0, self.keyboardHeight); } completion:^(BOOL finished) {}];
[self moveCustomView:NO duration:duration];
NSLog(#"CLOSED!");
}
if (aNotification.name == UIKeyboardWillShowNotification) {
/** KEYBOARD SHOW **/
[UIView animateWithDuration:0 animations:^{ self.upView.frame = CGRectOffset(self.upView.frame, 0, -self.keyboardHeight); self.tableView.frame = CGRectOffset(self.tableView.frame, 0, -self.keyboardHeight); } completion:^(BOOL finished) {}];
[self moveCustomView:YES duration:duration];
}
}
- (void)moveCustomView:(BOOL)move duration:(NSTimeInterval)time{
}
This issue might be with the animation duration, so you can get the keyboard showing and hiding animation duration from the -(void)handleKeyboard:(NSNotification *)notification {}
and also handle the showing and hiding your custom view inside the same function. Add the following code to your viewDidLoad function
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(handleKeyboard:) name:UIKeyboardWillHideNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(handleKeyboard:) name:UIKeyboardWillShowNotification object:nil];
Handle keyboard actions and UI changes
- (void)handleKeyboard:(NSNotification*)aNotification{
NSDictionary* info = [aNotification userInfo];
NSValue* value = [info objectForKey:UIKeyboardAnimationDurationUserInfoKey];
NSTimeInterval duration = 0;
[value getValue:&duration];
if (aNotification.name == UIKeyboardWillHideNotification) {
/** KEYBOARD HIDE **/
//calculate your view frames and handle UI changes
/*
.
.
.
.
.
*/
[self moveCustomView:NO duration:duration];
}
if (aNotification.name == UIKeyboardWillShowNotification) {
/** KEYBOARD SHOW **/
//calculate your view frames and handle UI changes
/*
.
.
.
.
.
*/
[self moveCustomView:YES duration:duration];
}
}
- (void)moveCustomView:(BOOL)move duration:(NSTimeInterval)time{
}
I think you need to use thread to call animation immediately...
- (void)textViewDidBeginEditing:(UITextView *)textView {
dispatch_async(dispatch_get_main_queue(), ^{
[self animateTextView:YES];
});
}
- (void)textViewDidEndEditing:(UITextView *)textView {
dispatch_async(dispatch_get_main_queue(), ^{
[self animateTextView:NO];
});
}
I'm not much aware of your problem, but try this because if we got this type of problems dispatch_async() will solve.
I have set a UITextView on full screen of device like in iphone-5s,textView size is (0,44,320,524). when keyboard appear then user cant show the insert text in view.how can I manage the UITextView, can i use UIScrollView for it or any thing else?
While editing complete View will move up and after done editing will move down...
(void)textViewDidBeginEditing:(UITextView *)textView {
[self animateTextView: YES];
}
(void)textViewDidEndEditing:(UITextView *)textView {
[self animateTextView:NO];
}
(void) animateTextView:(BOOL) up {
const int movementDistance =heightKeyboard; // tweak as needed
const float movementDuration = 0.3f; // tweak as needed
int movement= movement = (up ? -movementDistance : movementDistance);
NSLog(#"%d",movement);
[UIView beginAnimations: #"anim" context: nil];
[UIView setAnimationBeginsFromCurrentState: YES];
[UIView setAnimationDuration: movementDuration];
self.view.frame = CGRectOffset(self.inputView.frame, 0, movement);
[UIView commitAnimations];
}
I hope it will work for you.
I have solved it by using this code
.h file define a value
CGRect originalTextViewFrame;
.m file
- (void)viewWillAppear:(BOOL)animated
{
// Register notifications for when the keyboard appears
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}
- (void)viewWillDisappear:(BOOL)animated
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)keyboardWillShow:(NSNotification*)notification
{
[self moveTextViewForKeyboard:notification up:YES];
}
- (void)keyboardWillHide:(NSNotification*)notification
{
[self moveTextViewForKeyboard:notification up:NO];
}
- (void)moveTextViewForKeyboard:(NSNotification*)notification up:(BOOL)up
{
NSDictionary *userInfo = [notification userInfo];
NSTimeInterval animationDuration;
UIViewAnimationCurve animationCurve;
CGRect keyboardRect;
[[userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey] getValue:&animationCurve];
animationDuration = [[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
keyboardRect = [[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
keyboardRect = [self.view convertRect:keyboardRect fromView:nil];
[UIView beginAnimations:#"ResizeForKeyboard" context:nil];
[UIView setAnimationDuration:animationDuration];
[UIView setAnimationCurve:animationCurve];
if (up == YES) {
CGFloat keyboardTop = keyboardRect.origin.y;
CGRect newTextViewFrame = textView.frame;
originalTextViewFrame = textView.frame;
newTextViewFrame.size.height = keyboardTop - textView.frame.origin.y - 10;
textView.frame = newTextViewFrame;
}
else
{
// Keyboard is going away (down) - restore original frame
textView.frame = originalTextViewFrame;
}
[UIView commitAnimations];
}
-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
// Any new character added is passed in as the "text" parameter
if ([text isEqualToString:#"\n"])
{
// Be sure to test for equality using the "isEqualToString" message
[textView resignFirstResponder];
//[scrlView setContentOffset:CGPointMake(0,0) animated:YES];
// Return FALSE so that the final '\n' character doesn't get added
return FALSE;
}
// For any other character return TRUE so that the text gets added to the view
return TRUE;
}
use scroll view and then after try
UITextViewDelegate
method
- (void)textViewDidBeginEditing:(UITextView *)textView
{
[scrollView setContentOffset:CGPointMake(0,yourtextview.center.y-200) animated:YES];
}
I hope this question makes sense, I have a login screen, when I click on the one of my input fields (either Username or Password) a keyboard appears. but when that happens Everything moves in motion from left to right, so I have a big white space on the left side. What I am expecting is for everything to move from down to up instead. I really hope this makes sense. Here is my code:
- (void)animateTextField:(UITextField *) textField up: (BOOL) up;
- (void)NavButtonPressed: (UIButton*)sender;
- (void)NavButtonPressed: (UIButton*)sender
{
if([sender isEqual:navBarNextButton])
{
[self.idTextField resignFirstResponder];
[self.passwordTextField becomeFirstResponder];
currentResponder = 1;
[self animateTextField:self.passwordTextField up:NO];
[self animateTextField:self.passwordTextField up:YES];
[navBarNextButton setEnabled:NO];
[navBarPrevButton setEnabled:YES];
}
else
{
[self.passwordTextField resignFirstResponder];
[self.idTextField becomeFirstResponder];
currentResponder = 0;
[self animateTextField:self.idTextField up:NO];
[self animateTextField:self.idTextField up:YES];
[navBarNextButton setEnabled:YES];
[navBarPrevButton setEnabled:NO];
}
}
- (void)animateTextField:(UITextField *) textField up: (BOOL) up
{
int movementDistance;
if(UIInterfaceOrientationIsLandscape(self.interfaceOrientation))
{
if([textField isEqual:self.idTextField])
{
[textfieldNavigatorView setFrame:CGRectMake(0.0f, (1024.0f/2.0f)-31.5f, 1024.0f, 45.0f)];
movementDistance = 110;
}
else
{
[textfieldNavigatorView setFrame:CGRectMake(0.0f, (1024.0f/2.0f)-26.5f, 1024.0f, 45.0f)];
movementDistance = 115;
}
}
else
{movementDistance = 0;}
const float movementDuration = 0.3f;
int movement=0;
if([self interfaceOrientation] == UIInterfaceOrientationLandscapeLeft)
movement = (up? -movementDistance : movementDistance);
if([self interfaceOrientation] == UIInterfaceOrientationLandscapeRight)
movement = (up? movementDistance : -movementDistance);
[UIView beginAnimations:#"keyboardtransition" context:nil];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:movementDuration];
self.view.frame = CGRectOffset(self.view.frame, movement, 0);
[UIView commitAnimations];
}
If anyone could help, that would be amazing. My project Device is set to Landscape Right only.
I had same issue before, in iOS 7 and 8 it works differently. So I implemented a method for handling the issue:
- (void)keyboardHideShow:(UIView *)view shiftBy:(float)shiftAmount forState:(BOOL)state
{
float keyBoardOffset = shiftAmount;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.3]; // if you want to slide up the view
[UIView setAnimationBeginsFromCurrentState:YES];
UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
CGRect rect = [[[UIApplication sharedApplication] keyWindow] bounds];
if (state)
{
if ([[[UIDevice currentDevice] systemVersion] floatValue] < 8)
{
if (orientation == UIInterfaceOrientationLandscapeLeft)
{
rect.origin.x -= keyBoardOffset;
}
else
{
rect.origin.x += keyBoardOffset;
}
}
else
{
rect.origin.y -= keyBoardOffset;
}
}
view.frame = rect;
[UIView commitAnimations];
}
You can call it like:
[self keyboardHideShow:textfieldNavigatorView shiftBy:31.5 forState:true]; // For up
[self keyboardHideShow:textfieldNavigatorView shiftBy:31.5 forState:false]; // For down
I am implementing a custom focus bar on top of my keyboard. The focus bar has widgets to move cursor back and forward along with a done button.
Now, with iOS 7, I am seeing the focus bar is moving faster than keyboard. Because of this for a second I see screen behind the focus bar before it sits on top of keyboard. This is working fine in iOS 6.
Below is what I am doing:
- (void)keyboardWillShow:(NSNotification *)iNotification {
self.dismissForm = NO;
self.shouldScrollCell = YES;
CGFloat aKeyboardAnimationDuration = [[iNotification userInfo][UIKeyboardAnimationDurationUserInfoKey] doubleValue];
CGRect aKeyboardFrame = [[iNotification userInfo][UIKeyboardFrameEndUserInfoKey] CGRectValue];
CGRect adjustedKeyboardFrame = [self.view convertRect:aKeyboardFrame fromView:nil];
CGFloat adjustedHeight;
adjustedHeight = aKeyboardFrame.size.height + self.focusControlBar.frame.size.height;
[self.focusControlBar viewForFocusControlWillShowWithEndFrame:adjustedKeyboardFrame andAnimationDuration:aKeyboardAnimationDuration];
[UIView animateWithDuration:0.3 animations:^() {
[self.tableView setContentInset:UIEdgeInsetsMake(kScreenOrigin, kScreenOrigin, adjustedHeight, 0.0f)];
[self.tableView setScrollIndicatorInsets:UIEdgeInsetsMake(kScreenOrigin, kScreenOrigin, adjustedHeight, 0.0f)];
}];
}
This is my focus bar animation code:
- (void)viewForFocusControlWillShowWithEndFrame:(CGRect)iFrame andAnimationDuration:(CGFloat)iDuration {
BOOL aShouldAppear = YES;
if (self.delegate && [self.delegate respondsToSelector:#selector(focusControlBarShouldAppear:)]) {
aShouldAppear = [self.delegate focusControlBarShouldAppear:self];
}
if (aShouldAppear) {
if (self.delegate && [self.delegate respondsToSelector:#selector(focusControlBarWillAppear:)]) {
[self.delegate focusControlBarWillAppear:self];
}
CGRect aBarFrame = self.frame;
UIInterfaceOrientation anInterfaceOrientation = [UIApplication sharedApplication].statusBarOrientation;
aBarFrame.size.height = [self heightForOrientation:anInterfaceOrientation];
self.frame = aBarFrame;
aBarFrame.origin.y = self.superview.bounds.size.height - iFrame.size.height - aBarFrame.size.height;
[UIView animateWithDuration:iDuration animations:^(void) {
self.frame = aBarFrame;
} completion:^(BOOL iFinished) {
if (self.delegate && [self.delegate respondsToSelector:#selector(focusControlBarDidAppear:)]) {
[self.delegate focusControlBarDidAppear:self];
}
}];
}
}
I just got to know that with iOS 7 we need to also keep KeyboardAnimationCurve into account. With the below modified code I had it worked.
- (void)keyboardWillShow:(NSNotification *)iNotification {
self.dismissForm = NO;
self.shouldScrollCell = YES;
NSDictionary *aNotificationInfo = [iNotification userInfo];
CGFloat aKeyboardAnimationDuration = [aNotificationInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
UIViewAnimationCurve aKeyboardAnimationCurve = [aNotificationInfo[UIKeyboardAnimationCurveUserInfoKey] intValue];
CGRect aKeyboardFrame = [aNotificationInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
CGRect adjustedKeyboardFrame = [self.view convertRect:aKeyboardFrame fromView:nil];
CGFloat adjustedHeight;
adjustedHeight = aKeyboardFrame.size.height + self.focusControlBar.frame.size.height;
[self.focusControlBar viewForFocusControlWillShowWithEndFrame:adjustedKeyboardFrame animationCurve:aKeyboardAnimationCurve andAnimationDuration:aKeyboardAnimationDuration];
[UIView animateWithDuration:0.3 animations:^() {
[self.tableView setContentInset:UIEdgeInsetsMake(kScreenOrigin, kScreenOrigin, adjustedHeight, 0.0f)];
[self.tableView setScrollIndicatorInsets:UIEdgeInsetsMake(kScreenOrigin, kScreenOrigin, adjustedHeight, 0.0f)];
}];
}
- (void)viewForFocusControlWillShowWithEndFrame:(CGRect)iFrame animationCurve:(UIViewAnimationCurve)iAnimationCurve andAnimationDuration:(CGFloat)iDuration {
BOOL aShouldAppear = YES;
if (self.delegate && [self.delegate respondsToSelector:#selector(focusControlBarShouldAppear:)]) {
aShouldAppear = [self.delegate focusControlBarShouldAppear:self];
}
if (aShouldAppear) {
if (self.delegate && [self.delegate respondsToSelector:#selector(focusControlBarWillAppear:)]) {
[self.delegate focusControlBarWillAppear:self];
}
CGRect aBarFrame = self.frame;
UIInterfaceOrientation anInterfaceOrientation = [UIApplication sharedApplication].statusBarOrientation;
aBarFrame.size.height = [self heightForOrientation:anInterfaceOrientation];
self.frame = aBarFrame;
aBarFrame.origin.y = self.superview.bounds.size.height - iFrame.size.height - aBarFrame.size.height;
[UIView animateWithDuration:iDuration delay:0.0 options:(iAnimationCurve << 16) animations:^{
self.frame = aBarFrame;
} completion:^(BOOL finished) {
if (self.delegate && [self.delegate respondsToSelector:#selector(focusControlBarDidAppear:)]) {
[self.delegate focusControlBarDidAppear:self];
}
}];
}
}
I made a custom keyboard using UITextField method inputView. That works perfectly. But the keyboard shows as a normal keyboard from the bottom of the screen. I would like to animate the keyboard appearance from the right side of the screen.
Is that even possible ?
Thanks for any help!
I ended up with coding my own keyboard.
If somebody is interested:
#interface Keyboard ()
#property (assign, readwrite) BOOL visible;
#property (nonatomic, strong) UITextField *editingTextInput;
#property (nonatomic, strong) UIView *blockingView;
#end
#implementation Keyboard
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
[self addObservers];
self.exclusiveTouch = YES;
}
return self;
}
- (id)initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder:aDecoder];
if (self) {
[self addObservers];
self.exclusiveTouch = YES;
}
return self;
}
#pragma mark Register for notifications
- (void)addObservers {
// Keep track of the textView/Field that we are editing
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(editingDidBegin:)
name:UITextFieldTextDidBeginEditingNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(editingDidBegin:)
name:UITextViewTextDidBeginEditingNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(editingDidEnd:)
name:UITextFieldTextDidEndEditingNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(editingDidEnd:)
name:UITextViewTextDidEndEditingNotification
object:nil];
}
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UITextFieldTextDidBeginEditingNotification
object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UITextViewTextDidBeginEditingNotification
object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UITextFieldTextDidEndEditingNotification
object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UITextViewTextDidEndEditingNotification
object:nil];
}
-(BOOL)isKeyboardForTextField:(UITextField*)textField
{
// need to be implemented at child object
return NO;
}
#pragma mark Show In View
static UIView *showInView;
+(void)setShowInView:(UIView *)view {
showInView = view;
}
+(UIView*)showInView {
return showInView;
}
#pragma mark Notifications - editing DidBegin/End
// Editing just began, store a reference to the object that just became the firstResponder
- (void)editingDidBegin:(NSNotification *)notification {
if ([notification.object isKindOfClass:[UIResponder class]])
{
if ([notification.object conformsToProtocol:#protocol(UITextInput)]) {
if([self isKeyboardForTextField:notification.object]) {
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:#selector(hideKeyboard) object:nil];
_targetTextInput = notification.object;
_targetTextInput.inputView = [[UIView alloc] initWithFrame:CGRectNull];;
if(!_visible) {
[self showKeyboard];
}
}
return;
}
}
// Not a valid target for us to worry about.
_targetTextInput = nil;
}
// Editing just ended.
- (void)editingDidEnd:(NSNotification *)notification {
[self performSelector:#selector(hideKeyboard) withObject:nil afterDelay:0.1]; // so we have chance to stop it
}
#pragma mark Show / Hide keyboard
-(void)enableUserInteraction {
[_blockingView removeFromSuperview];
}
-(void)disableUserInteraction {
_blockingView = [[UIView alloc] initWithFrame:[Keyboard showInView].frame];
_blockingView.backgroundColor = [UIColor clearColor];
[[Keyboard showInView] addSubview:_blockingView];
}
- (void)showKeyboardWithAnimation:(BOOL)animation
{
[[Keyboard showInView] addSubview:self];
_visible = NO;
[self disableUserInteraction];
if (IS_IPHONE)
{
[self setOrigin:CGPointMake(0, [Keyboard showInView].size.height)];
[self updateBackground];
[UIView animateWithDuration:( animation ? .2 : 0)
delay:0.0
options: UIViewAnimationOptionBeginFromCurrentState
animations:^(void){
[self moveOriginY:-self.size.height];
}
completion:^(BOOL finished){
_visible = YES;
[self enableUserInteraction];
}];
} else {
[self setOrigin:CGPointMake([Keyboard showInView].size.width, [Keyboard showInView].size.height - self.size.height - 10)];
[UIView animateWithDuration:( animation ? .5 : 0)
delay:0.0
options: UIViewAnimationOptionBeginFromCurrentState
animations:^(void){
[self moveOriginX:-self.size.width];
}
completion:^(BOOL finished){
_visible = YES;
[self enableUserInteraction];
}];
}
}
-(void)showKeyboard
{
[self showKeyboardWithAnimation:YES];
}
- (void)hideKeyboardWithAnimation:(BOOL)animation
{
[self disableUserInteraction];
if ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPhone) {
[UIView animateWithDuration:( animation ? .2 : 0)
delay:0.0
options: UIViewAnimationOptionBeginFromCurrentState
animations:^(void){
[self moveOriginY:self.size.height];
}
completion:^(BOOL finished){
[self removeFromSuperview];
_visible = NO;
[self enableUserInteraction];
}];
} else {
[UIView animateWithDuration:( animation ? .5 : 0)
delay:0.0
options: UIViewAnimationOptionBeginFromCurrentState
animations:^(void){
[self moveOriginX:self.size.width];
}
completion:^(BOOL finished){
[self removeFromSuperview];
_visible = NO;
[self enableUserInteraction];
}];
}
}
-(void)hideKeyboard
{
[self hideKeyboardWithAnimation:YES];
}
#end
UIView categories:
#implementation UIView (Frame)
-(CGPoint)origin {
return self.frame.origin;
}
-(CGSize)size {
return self.frame.size;
}
#pragma mark Change Origin
-(void)setOrigin:(CGPoint)origin
{
CGRect frame = self.frame;
frame.origin = origin;
self.frame = frame;
}
-(void)setOriginX:(CGFloat)x
{
CGRect frame = self.frame;
frame.origin.x = x;
self.frame = frame;
}
-(void)setOriginY:(CGFloat)y
{
CGRect frame = self.frame;
frame.origin.y = y;
self.frame = frame;
}
#pragma mark Move Origin
-(void)moveOrigin:(CGPoint)origin
{
CGRect frame = self.frame;
frame.origin.y += origin.y;
frame.origin.x += origin.x;
self.frame = frame;
}
-(void)moveOriginX:(CGFloat)x
{
CGRect frame = self.frame;
frame.origin.x += x;
self.frame = frame;
}
-(void)moveOriginY:(CGFloat)y
{
CGRect frame = self.frame;
frame.origin.y += y;
self.frame = frame;
}
#pragma mark Change Size
-(void)setSize:(CGSize)size
{
CGRect frame = self.frame;
frame.size = size;
self.frame = frame;
}
-(void)setSizeWidth:(CGFloat)width
{
CGRect frame = self.frame;
frame.size.width = width;
self.frame = frame;
}
-(void)setSizeHeight:(CGFloat)height
{
CGRect frame = self.frame;
frame.size.height = height;
self.frame = frame;
}
#pragma mark Change Size
-(void)addSizeWidth:(CGFloat)width
{
CGRect frame = self.frame;
frame.size.width += width;
self.frame = frame;
}
-(void)addSizeHeight:(CGFloat)height
{
CGRect frame = self.frame;
frame.size.height += height;
self.frame = frame;
}
#end