7

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

user2586519
  • 250
  • 1
  • 6
  • 18
  • The code you posted should shrink the `sliderView` down to nothing. What actually happens now? You don't tell us what the actual problem is. – rmaddy Aug 23 '13 at 05:50
  • How can that code show the view? It shrinks the frame's width and height to 0. Are you sure the code to hide the view is being called? – rmaddy Aug 23 '13 at 05:57
  • Where are you calling each of those methods? Right after each other or with some event? Is sliderView the same object in both calls? – Jsdodgers Aug 23 '13 at 05:58
  • check you hide action called or not – NANNAV Aug 23 '13 at 05:59
  • you can use the aplha property eg--> .alpha = 0;(completely invisible) and .alpha = 1;(completely visible) – Pradeep Aug 23 '13 at 06:02
  • @rmaddy and NANNAV Yes,the code to hide the view is being called because I am checking this using flag. – user2586519 Aug 23 '13 at 06:49
  • http://stackoverflow.com/a/29080894/1442541 – evya Jul 18 '16 at 08:13

6 Answers6

18

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
MOBYTELAB
  • 891
  • 1
  • 10
  • 10
  • Thanks Binh Lee ,Also I want to add Imageview on that UIView,so can I change the frame of imageview like UIView?? – user2586519 Aug 23 '13 at 07:14
  • 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 – MOBYTELAB Aug 23 '13 at 08:08
  • @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. – Thangavel Jan 18 '14 at 07:20
6
  -(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];
codercat
  • 22,873
  • 9
  • 61
  • 85
Leena
  • 2,678
  • 1
  • 30
  • 43
4

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];
}];
Muhammad Zeeshan
  • 2,441
  • 22
  • 33
  • 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 – Udaya Sri Jul 18 '14 at 09:10
2

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]; 
    }
}
Manab Kumar Mal
  • 20,788
  • 5
  • 31
  • 43
iosdeveloper
  • 99
  • 3
  • 13
0

Apply Initial/starting frame of you View is

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

Give tag of your button such 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

Try this:

[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:.25];
[sliderView setFrame:CGRectMake(130, 30, 0, 0)];
[UIView commitAnimations];
Hristo
  • 6,382
  • 4
  • 24
  • 38
  • 2
    These are old and outdated. Apple recommends that you use the block versions (as shown in the original question). – rmaddy Aug 23 '13 at 06:51