Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to hide/show UIView with animation in iPhone

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

like image 219
user2586519 Avatar asked Aug 23 '13 05:08

user2586519


People also ask

Does UIView animate need weak self?

No, it is not needed in this case. animations and completion are not retained by self so there is no risk of strong retain cycle.

How do you animate a view in Swift?

To be exact, whenever you want to animate the view, you actually call layoutIfNeeded on the superview of that view. Try this instead: UIView. animate(withDuration: 0.1, delay: 0.1, options: UIViewAnimationOptions.

Does UIView animate run on the main thread?

The contents of your block are performed on the main thread regardless of where you call [UIView animateWithDuration:animations:] .


1 Answers

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
like image 149
MOBYTELAB Avatar answered Nov 03 '22 00:11

MOBYTELAB