How to hide / show UIView with animation on iPhone

I want to show UIView after clicking the button with the animation, I can show the view, but I can not hide it again by clicking this button. Here is my code to show / hide the view. Show UIView:

sliderView.frame = CGRectMake(130, 20, 0, 0); [UIView animateWithDuration:0.25 animations:^{ sliderView.frame = CGRectMake(130, 30, 100, 200); }]; 

Hide UIView:

  [UIView animateWithDuration:0.25 animations:^{ sliderView.frame = CGRectMake(130, 30, 0, 0); }]; 

Using the above code is displayed with animation, but not hidden. Does anyone know how to hide it, please help, thanks

+7
ios objective-c uiview uiviewanimation
source share
6 answers

Your code works, I used it. Check out the code below.
.h file:

 #import <UIKit/UIKit.h> @interface StackoverflowTestViewController : UIViewController @property (strong, nonatomic) IBOutlet UIView *cautionView; - (IBAction)btnToggleClick:(id)sender; @property (strong, nonatomic) IBOutlet UIButton *btnToggle; @end 


.m file:

 #import "StackoverflowTestViewController.h" @interface StackoverflowTestViewController () @end @implementation StackoverflowTestViewController bool isShown = false; - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } - (IBAction)btnToggleClick:(id)sender { if (!isShown) { _cautionView.frame = CGRectMake(130, 20, 0, 0); [UIView animateWithDuration:0.25 animations:^{ _cautionView.frame = CGRectMake(130, 30, 100, 200); }]; isShown = true; } else { [UIView animateWithDuration:0.25 animations:^{ _cautionView.frame = CGRectMake(130, 30, 0, 0); }]; isShown = false; } } @end 
+18
source share
  -(void)fadeInAnimation:(UIView *)aView { CATransition *transition = [CATransition animation]; transition.type =kCATransitionFade; transition.duration = 0.5f; transition.delegate = self; [aView.layer addAnimation:transition forKey:nil]; } Add Subview: [self.view addsubView:mysubview]; [self fadeInAnimation:self.view]; Remove SubView: [mySubView removefromSuperView]; [self fadeInAnimation:self.view]; 
+6
source share

You can use the alpha property and the hidden UIView property to hide your slider. Try the following code:

 /*To unhide*/ [sliderView setHidden:NO]; sliderView.frame = CGRectMake(130, 20, 0, 0); [UIView animateWithDuration:0.25 animations:^{ sliderView.frame = CGRectMake(130, 30, 100, 200); sliderView.alpha = 1.0f; } completion:^(BOOL finished) { }]; /*To hide*/ [UIView animateWithDuration:0.25 animations:^{ sliderView.frame = CGRectMake(130, 30, 0, 0); [sliderView setAlpha:0.0f]; } completion:^(BOOL finished) { [sliderView setHidden:YES]; }]; 
+4
source share

try it

 - (IBAction)eventparameter:(id)sender { if ([self.parameterview isHidden]) { [UIView beginAnimations:@"LeftFlip" context:nil]; [UIView setAnimationDuration:0.8]; _parameterview.hidden=NO; [UIView setAnimationCurve:UIViewAnimationCurveLinear]; [UIView setAnimationTransition:UIViewAnimationTransitionCurlDown forView:_parameterview cache:YES]; [UIView commitAnimations]; } else { [UIView transitionWithView:_parameterview duration:0.4 options:UIViewAnimationOptionTransitionCurlUp animations:NULL completion:NULL]; [self.parameterview setHidden:YES]; } } 
+2
source share

Apply the start / start frame that you are viewing.

 sliderView.frame = CGRectMake(130, 480, 100, 200); 

Give your button tag like

myButton.tag = 101;

And your button method

 -(void)MyButonMethod:(UIButton *)sender { if(sender.tag == 101) { [UIView animateWithDuration:0.25 animations:^{ sliderView.frame = CGRectMake(130, 30, 100, 200); }]; sender.tag = 102; } else { [UIView animateWithDuration:0.25 animations:^{ sliderView.frame = CGRectMake(130, 480, 100, 200); }]; sender.tag = 101; } } 
0
source share

Try the following:

 [UIView beginAnimations:nil context:NULL]; [UIView setAnimationDuration:.25]; [sliderView setFrame:CGRectMake(130, 30, 0, 0)]; [UIView commitAnimations]; 
0
source share

All Articles