KEYBOARD HIDE AND SHOW AND VIEW ANIMATIONS IN TEXT FIELD
ViewController.h
float animatedDistance;
ViewController.m
- (void)textViewDidBeginEditing:(UITextView *)textView
{
CGRect textFieldRect=[self.view.window convertRect:textView.bounds fromView:textView];
CGRect viewRect=[self.view.window convertRect:self.view.bounds fromView:self.view];
CGFloat midline=textFieldRect.origin.y + 0.5 * textFieldRect.size.height;
CGFloat numerator = midline - viewRect.origin.y - 0.2 * viewRect.size.height;
CGFloat denominator = (0.8 - 0.2) * viewRect.size.height;
CGFloat heightFraction=numerator / denominator;
if (heightFraction < 0.0)
{
heightFraction= 0.0;
}
else if (heightFraction > 1.0)
{
heightFraction = 1.0;
}
UIInterfaceOrientation orientation = [[UIApplication sharedApplication]statusBarOrientation];
if (orientation == UIInterfaceOrientationPortrait || orientation == UIInterfaceOrientationPortraitUpsideDown)
{
animatedDistance = floorf(216 * heightFraction);
}
else
{
animatedDistance = floorf(140 * heightFraction);
}
CGRect viewFrame=self.view.frame;
viewFrame.origin.y-=animatedDistance;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:0.3];
[self.view setFrame:viewFrame];
[UIView commitAnimations];
// if ([textView.text isEqualToString:@"Enquiry/Request"])
{
// textView.text =@"";
// textView.textColor = [UIColor blackColor]; //optional
// }
[textView becomeFirstResponder];
}
- (void)textViewDidEndEditing:(UITextView *)textView
{
CGRect viewFrame = self.view.frame;
viewFrame.origin.y +=animatedDistance;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:0.3];
[self.view setFrame:viewFrame];
[UIView commitAnimations];
// if ([textView.text isEqualToString:@""])
{
// textView.text =@"Enquiry/Request";
// textView.textColor = [UIColor darkGrayColor]; //optional
// }
[textView resignFirstResponder];
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
-(void)textFieldDidEndEditing:(UITextField *)textField
{
CGRect viewFrame = self.view.frame;
viewFrame.origin.y +=animatedDistance;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:0.3];
[self.view setFrame:viewFrame];
[UIView commitAnimations];
}
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
CGRect textFieldRect=[self.view.window convertRect:textField.bounds fromView:textField];
CGRect viewRect=[self.view.window convertRect:self.view.bounds fromView:self.view];
CGFloat midline=textFieldRect.origin.y + 0.5 * textFieldRect.size.height;
CGFloat numerator = midline - viewRect.origin.y - 0.2 * viewRect.size.height;
CGFloat denominator = (0.8 - 0.2) * viewRect.size.height;
CGFloat heightFraction=numerator / denominator;
if (heightFraction < 0.0)
{
heightFraction= 0.0;
}
else if (heightFraction > 1.0)
{
heightFraction = 1.0;
}
UIInterfaceOrientation orientation = [[UIApplication sharedApplication]statusBarOrientation];
if (orientation == UIInterfaceOrientationPortrait || orientation == UIInterfaceOrientationPortraitUpsideDown)
{
animatedDistance = floorf(216 * heightFraction);
}
else
{
animatedDistance = floorf(140 * heightFraction);
}
CGRect viewFrame=self.view.frame;
viewFrame.origin.y-=animatedDistance;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:0.3];
[self.view setFrame:viewFrame];
[UIView commitAnimations];
}
}
else if (heightFraction > 1.0)
{
heightFraction = 1.0;
}
UIInterfaceOrientation orientation = [[UIApplication sharedApplication]statusBarOrientation];
if (orientation == UIInterfaceOrientationPortrait || orientation == UIInterfaceOrientationPortraitUpsideDown)
{
animatedDistance = floorf(216 * heightFraction);
}
else
{
animatedDistance = floorf(140 * heightFraction);
}
CGRect viewFrame=self.view.frame;
viewFrame.origin.y-=animatedDistance;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:0.3];
[self.view setFrame:viewFrame];
[UIView commitAnimations];
// if ([textView.text isEqualToString:@"Enquiry/Request"])
{
// textView.text =@"";
// textView.textColor = [UIColor blackColor]; //optional
// }
[textView becomeFirstResponder];
}
- (void)textViewDidEndEditing:(UITextView *)textView
{
CGRect viewFrame = self.view.frame;
viewFrame.origin.y +=animatedDistance;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:0.3];
[self.view setFrame:viewFrame];
[UIView commitAnimations];
// if ([textView.text isEqualToString:@""])
{
// textView.text =@"Enquiry/Request";
// textView.textColor = [UIColor darkGrayColor]; //optional
// }
[textView resignFirstResponder];
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
-(void)textFieldDidEndEditing:(UITextField *)textField
{
CGRect viewFrame = self.view.frame;
viewFrame.origin.y +=animatedDistance;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:0.3];
[self.view setFrame:viewFrame];
[UIView commitAnimations];
}
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
CGRect textFieldRect=[self.view.window convertRect:textField.bounds fromView:textField];
CGRect viewRect=[self.view.window convertRect:self.view.bounds fromView:self.view];
CGFloat midline=textFieldRect.origin.y + 0.5 * textFieldRect.size.height;
CGFloat numerator = midline - viewRect.origin.y - 0.2 * viewRect.size.height;
CGFloat denominator = (0.8 - 0.2) * viewRect.size.height;
CGFloat heightFraction=numerator / denominator;
if (heightFraction < 0.0)
{
heightFraction= 0.0;
}
else if (heightFraction > 1.0)
{
heightFraction = 1.0;
}
UIInterfaceOrientation orientation = [[UIApplication sharedApplication]statusBarOrientation];
if (orientation == UIInterfaceOrientationPortrait || orientation == UIInterfaceOrientationPortraitUpsideDown)
{
animatedDistance = floorf(216 * heightFraction);
}
else
{
animatedDistance = floorf(140 * heightFraction);
}
CGRect viewFrame=self.view.frame;
viewFrame.origin.y-=animatedDistance;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:0.3];
[self.view setFrame:viewFrame];
[UIView commitAnimations];
}
Comments
Post a Comment
Thank You.