In this tutorial we will see how to slide the view by clicking on to the text Field.This is the view based apps. Here are some steps for it…
1. Open the xcode & choose "File->New Project".
2. Select "Application" from left menu and then "View-based Application".
3.Name your project as "viewslider" and save the project.
4.Now just select "ViewSliderViewController.h" file from left menu for define two UITextField . Set the property for UITextField .
#import <UIKit/UIKit.h>
@interface ViewSliderViewController : UIViewController {
IBOutlet UITextField *textFieldFirst;
IBOutlet UITextField *textFieldSecond;
int flag;
}
@end
5. Now select "ViewSliderViewController.m" file from menu for implementation.
#import "ViewSliderViewController.h"
#define kOFFSET_FOR_KEYBOARD 120.0
#define kOFFSET_FOR_KEYBOARD1 120.0
@implementation ViewSliderViewController
-(void)textFieldDidBeginEditing:(UITextField *)sender
{
if ([sender isEqual:textFieldFirst]||[sender isEqual:textFieldSecond])
{
if (self.view.frame.origin.y >= 0)
{
[self setViewMovedUp:YES];
}
}
}
-(void)setViewMovedUp:(BOOL)movedUp
{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5];
CGRect rect = self.view.frame;
if (movedUp)
{
rect.origin.y -= kOFFSET_FOR_KEYBOARD;
rect.size.height += kOFFSET_FOR_KEYBOARD;
}
else
{
// revert back to the normal state.
rect.origin.y += kOFFSET_FOR_KEYBOARD;
rect.size.height -= kOFFSET_FOR_KEYBOARD;
}
self.view.frame = rect;
[UIView commitAnimations];
}
- (void)keyboardWillShow:(NSNotification *)notif
{
if (([textFieldFirst isFirstResponder])||([textFieldSecond isFirstResponder])&& self.view.frame.origin.y >= 0)
{
flag=YES;
[self setViewMovedUp:YES];
}
else if ((![textFieldFirst isFirstResponder])||(![textFieldSecond isFirstResponder]) && self.view.frame.origin.y < 0)
{
[self setViewMovedUp:NO];
}
}
- (void)viewWillAppear:(BOOL)animated
{
// register for keyboard notifications
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector
(keyboardWillShow:)
name:UIKeyboardWillShowNotification object:self.view.window];
}
- (void)viewWillDisappear:(BOOL)animated
{
// unregister for keyboard notifications while not visible.
[[NSNotificationCenter defaultCenter]
removeObserver:self name:UIKeyboardWillShowNotification object:nil]; }
-(void) touchesBegan :(NSSet *) touches withEvent:(UIEvent *)event
{
//NSLog(@"%@",flag);
if (flag==TRUE)
{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5];
CGRect rect = self.view.frame;
rect.origin.y += kOFFSET_FOR_KEYBOARD;
rect.size.height -= kOFFSET_FOR_KEYBOARD;
self.view.frame = rect;
[UIView commitAnimations];
flag=FALSE;
}
[textFieldFirst resignFirstResponder];
[textFieldSecond resignFirstResponder];
[super touchesBegan:touches withEvent:event ];
}
@end
6. Now double click on "ViewSliderViewController.xib" from Resource folder for open it.
7. On the view window drag the two textfield from library . Then link the textfields to the File's Owner.
8. Then Quit and Save the Interface Builder.
9. Now you are ready for click on "Build & Go" button.
Now you can see the output in iphone simulator .
Hey, that post lavees me feeling foolish. Kudos to you!
(1) Responses to this post