How to hide/show UIView with animation in iPhone

54,801

Solution 1

Your code works, i have used it. Look 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

Solution 2

  -(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];

Solution 3

You can use alpha property and hidden property of UIView to hide your sliderView. 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];
}];

Solution 4

try this

- (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]; 
    }
}
Share:
54,801
user2586519
Author by

user2586519

Updated on September 11, 2020

Comments

  • user2586519
    user2586519 over 3 years

    I want to show UIView after button pressed with animation,I can show the view but I am unable to hide it again pressed that button.Here is my code to show/hide the view. To Show the UIView :

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

    To Hide the UIView :

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

    Using above code view is showing with animation but not hiding. Does anybody know how to hide it,Please help,Thanks

  • rmaddy
    rmaddy over 10 years
    These are old and outdated. Apple recommends that you use the block versions (as shown in the original question).
  • user2586519
    user2586519 over 10 years
    Thanks Binh Lee ,Also I want to add Imageview on that UIView,so can I change the frame of imageview like UIView??
  • MOBYTELAB
    MOBYTELAB over 10 years
    no problem man, (1).you can add ImageView to that UIView and change frame on that UIView (2) change UIView above with ImageView with no problem
  • Thangavel
    Thangavel over 10 years
    @BinhLee i am using for your code that _cautionView is animated. i did add two UILabel into that _cautionView. That Label not hide while animate.
  • Udaya Sri
    Udaya Sri almost 10 years
    This works for me .. If you are using story board , you can add a uiview & make it's size as you want . If so you can remove the sliderView.frame's CGRectMake values