This question already has answers here:
How can I make a UITextField move up when the keyboard is present - on starting to edit?
(98 answers)
Closed 8 years ago.
How can i move textView/textField to top of view when I select one of them?
Working code....
1) set delegate to you textFields
[self.textField setDelegate:self];
2) To move textField up
-(void)textFieldDidBeginEditing:(UITextField *)textField
{
if (textField == self.textField)
{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDelegate:self];
[UIView setAnimationDuration:0.5];
[UIView setAnimationBeginsFromCurrentState:YES];
self.view.frame = CGRectMake(self.view.frame.origin.x , (self.view.frame.origin.y - 80), self.view.frame.size.width, self.view.frame.size.height);
[UIView commitAnimations];
}
}
3) To move textField down
-(void)textFieldDidEndEditing:(UITextField *)textField
{
if (textField == self.textField)
{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDelegate:self];
[UIView setAnimationDuration:0.5];
[UIView setAnimationBeginsFromCurrentState:YES];
self.view.frame = CGRectMake(self.view.frame.origin.x , (self.view.frame.origin.y + 80), self.view.frame.size.width, self.view.frame.size.height);
[UIView commitAnimations];
}
}
4) to move textView Up
-(void)textViewDidBeginEditing:(UITextView *)textView
{
if (textView == self.textView)
{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDelegate:self];
[UIView setAnimationDuration:0.5];
[UIView setAnimationBeginsFromCurrentState:YES];
self.view.frame = CGRectMake(self.view.frame.origin.x , (self.view.frame.origin.y - 80), self.view.frame.size.width, self.view.frame.size.height);
[UIView commitAnimations];
}
}
5) to move textView Down
-(void)textViewDidEndEditing:(UITextView *)textView
{
if (textView == self.textView)
{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDelegate:self];
[UIView setAnimationDuration:0.5];
[UIView setAnimationBeginsFromCurrentState:YES];
self.view.frame = CGRectMake(self.view.frame.origin.x , (self.view.frame.origin.y + 80), self.view.frame.size.width, self.view.frame.size.height);
[UIView commitAnimations];
}
}
Related
I am using this code to change between 2 UIView:
UIViewAnimationOptions animationType = UIViewAnimationOptionTransitionFlipFromLeft;
[UIView transitionFromView:self.playlistTableView toView:self.playerView duration:0.5 options:animationType completion:^(BOOL finished){
[self.containerView sendSubviewToBack:self.upperView];
[self.containerView sendSubviewToBack:self.playerView];
self.isPlaylistVisible = !self.isPlaylistVisible;
isControlsHidden = NO;
}];
And i noticed a strange behavior, that when i made the flip the height of the self.playerView loosing 20px and after one second it's increase back to the normal frame size.
I try to change the animationType to UIViewAnimationOptionLayoutSubviews and now when i change between the view this behavior is not occur.
Any idea what can be the issue?
Please try this code.
[self.upperView setHidden:YES];
[self.playerView setHidden:NO];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1.0];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:containerView cache:YES];
[UIView setAnimationDuration:1.0];
[UIView commitAnimations];
[containerView addSubview:self.playerView];
For getting reverse case
[self.upperView setHidden:NO];
[self.playerView setHidden:YES];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1.0];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:containerView cache:YES];
[UIView setAnimationDuration:1.0];
[UIView commitAnimations];
[containerView addSubview:self.upperView];
So, I am trying to implement iAds into my application, but I cannot get it to work properly.
I am using these two functions to show and hide the banner and it works great:
- (void)showAdBanner:(ADBannerView *)banner {
if (bannerVisible) return;
NSLog(#"showing ad");
[UIView beginAnimations:#"animateBannerIn" context:nil];
[UIView setAnimationDuration:1.0];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[banner setFrame:CGRectOffset([banner frame], 0, -bannerHeight)];
[toolbarView setFrame:CGRectOffset([toolbarView frame], 0, -bannerHeight)];
[UIView commitAnimations];
bannerVisible = YES;
}
- (void)hideAdBanner:(ADBannerView *)banner {
if (!bannerVisible) return;
NSLog(#"hiding ad");
[UIView beginAnimations:#"animateBannerOut" context:nil];
[UIView setAnimationDuration:1.0];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[banner setFrame:CGRectOffset([banner frame], 0, bannerHeight)];
[toolbarView setFrame:CGRectOffset([toolbarView frame], 0, bannerHeight)];
[UIView commitAnimations];
bannerVisible = NO;
}
The only issue is that after one presses on the iAd banner, the toolbar view returns to its original position sans-iAd.
I have removed all layout constraints and it still is occurring. I can attach a video after if it clarifies.
Thank you
Video: https://www.dropbox.com/s/64mbiowk94sl6rq/iAdIssue.mov
I have app i want that when user enters any data then view should slide up.
I am writing this code this code works fine in another app but not working in this app.I am following same way.
-(void)showAnimationBack{
NSLog(#"Back Animation is Working");
[UIView animateWithDuration:0.5
animations:^{
self.subViewLand.frame = CGRectMake(0,-10,1024,768);
}];
}
-(void) showAnimationPests{
NSLog(#" Animation is Working");
[UIView animateWithDuration:0.5
animations:^{
self.subViewLand.frame = CGRectMake(0,-200,1024,768);
}];
}
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5];
self.subViewLand.frame = CGRectMake(0,-200,1024,768); // or to self.view.frame = CGRectMake(0,-200,1024,768);
[UIView commitAnimations];
}
- (void)textFieldDidEndEditing:(UITextField *)textField{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5];
self.subViewLand.frame = CGRectMake(0,-200,1024,768); // or to self.view.frame = CGRectMake(0,-200,1024,768);
[UIView commitAnimations];
}
This code will work
in textfielddidBeginEditing Method set view frame like this
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
[self.subViewLand setFrame:CGRectMake(0, -200, 320, 480)];
}
in textfielddidEndEditing Method set view frame as it is.
- (void)textFieldDidEndEditing:(UITextField *)textField{
[self.subViewLand setFrame:CGRectMake(0, 0, 320, 480)];
}
It will definitely work. :)
In textFieldDidBeginEditing() call the your method showAnimationPests() and similarly in textFieldDidEndEditing() call showAnimationBack() .
This will work fine for you.
I am use this code when a cell is tapped in an uitableview that exists inside an uiview but is not doing the "flash" that is supposed to do on the cell when the user taps it.
-(IBAction)labelTap:(UIGestureRecognizer *)sender
{
if(sender.state != UIGestureRecognizerStateRecognized)
return;
UIView *tappedview=[sender.view hitTest:[sender locationInView:sender.view]
withEvent:nil];
UIView *contentview1=tappedview.superview.superview;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.3];
[contentview1 setAlpha:0];
[contentview1 setAlpha:1];
[UIView commitAnimations];
}
Any help appreciated.
Try this is working!
UIView *contentview1=tappedview.superview;
[contentview1 setAlpha:0];
[UIView animateWithDuration:0.3f
animations:^(void)
{
// Animate the flash on tap
[contentview1 setAlpha:1];
}
completion:^(BOOL finished)
{
NSLog(#"Done! Now show the pop up");
)];
I have this code, where successview start with alpha = 0.00
- (void) startAnimation{
//immediately
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:3];
[successView setAlpha:1.00];
[UIView commitAnimations];
//in three seconds
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:3];
[successView setAlpha:0.00];
[UIView commitAnimations];
}
in this way, in first animation (alpha 0.00 to 1.00), it don't happen in 3 seconds but immediately, instead in the second animation (alpha 1.00 to alpha 0.00) it happens in 3 seconds
if I write only firts animation:
- (void) startAnimation{
//in three seconds
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:3];
[successView setAlpha:1.00];
[UIView commitAnimations];
}
it happens in 3 seconds, why in the forst example it don't happen?
- (void)startAnimation
{
[UIView beginAnimations:#"successAnimationPart1" context:nil];
[UIView setAnimationDuration:3];
[UIView setAnimationDelegate:self];
[successView setAlpha:1.00];
[UIView commitAnimations];
}
And add this animation delegate method:
- (void)animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context
{
if ([animationID isEqualToString:#"successAnimationPart1"]) {
// when first animation is done, call second
[UIView beginAnimations:#"successAnimationPart2" context:nil];
[UIView setAnimationDuration:3];
[UIView setAnimationDelegate:self];
[successView setAlpha:0.00];
[UIView commitAnimations];
}
}
You can try to set animation delay for your 2nd animation (note that using this api is discouraged and you should use block-based animateWithDuration:delay:options:animations:completion: animation if you're targeting iOS4.0+):
...
[UIView setAnimationDelay:3.0];
...
But if you just want to change view's alpha to 1 and revert it back you can use just your 1st animation but make it autoreversing:
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:3];
[UIView setAnimationRepeatAutoreverses:YES];
[successView setAlpha:1.00];
[UIView commitAnimations];