HannahCarney
HannahCarney

Reputation: 3631

animate tableview from top (like a dropdown) on hidden

This is toggled whenever my dropdown is pressed, however the view just appears (because it is hidden). How do I make it come down from the dropdown on selected and go back up into the dropdown on unselected. I've tried a few transitions but I can't get anything to work. I want it to look like a dropdown appearing.

-(void)addSubviewOvertop:(BOOL)selected
{
    dropDownTableView.hidden = !selected;
}

Upvotes: 0

Views: 593

Answers (1)

Kiko Lobo
Kiko Lobo

Reputation: 547

The way to do it is to use animations (UIViewAnimations). So you set your hidden view above the visible view frame before showing anything. And animated to the viewable frame when you desire to show the frame.

When you load the view for the first time you set your hidden frames (I use offsets) and also your frames when showed. And then before appearing your set your view to the hidden frame so it is not showed when the user sees the main view for the first time.

Then when you want to show it, you set it to the showed frame with an animation and the other way around to hide it.

A good idea is to make a setter of a property, where you can intercept the set before it happens...

Then, to show the menu view, you can do something like:

self.menuShowed = YES;

Note that you do have to use self. if you use _menuShowed it will set the iVar but will not fire the setter.

Untested code idea (Assumes you have a connected and instantiated UIView (menuView) named menuView. :

@interface ViewController ()
@property (nonatomic, strong) UIView *menuView;
@property (nonatomic) CGRect hiddenFrame;
@property (nonatomic) CGRect showedFrame;
@property (nonatomic) BOOL menuShowed;

@end

@implementation ViewController

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.hiddenFrame = CGRectOffset(self.menuView.frame, 0 - self.menuView.frame.size.height, 0);
    self.showedFrame = CGRectOffset(self.menuView.frame, self.menuView.frame.size.height, 0);

    _menuShowed = NO;
    self.menuView.frame = self.hiddenFrame;
}

- (void)setMenuShowed:(BOOL)menuShowed
{
    if (menuShowed) {
        [UIView animateWithDuration:0.7 animations:^{
            self.menuView.frame = self.showedFrame;
        }];
    } else {
        [UIView animateWithDuration:0.7 animations:^{
            self.menuView.frame = self.hiddenFrame;
        }];
    }

    _menuShowed = menuShowed;
}

@end

Upvotes: 1

Related Questions