Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>With a simple BOOL to store the current state of the sliding UIView you can achieve this.</p> <p>The initial state is not visible if I understand well? So initially the BOOL citiesVisible is set to N0, then the button's action check its value, and performs a different animation accordingly. Simple.</p> <p>Finally, I also disable the button during the animation to prevent the user to trigger a second animation while a first one is already performing.</p> <p>I rewrote a bit your code to follow some best practices, and improve readability/maintainability:</p> <ul> <li>Setting up the views beforehand because the button's action responsibility shouldn't be to setup the views, but only to animate them.</li> <li>Changed the signature of the target action, that's my personal style guide to state in the sender's name and the ControlEvent, it makes it more tidy when you have multiple buttons/actions ;). Also per Apple guidelines, methods receiving actions should have one "id" parameter, the sender.</li> <li>Store the frames in variables (you could also have them as #define, that'd be even better I think), so you don't have to hard-code them twice, it reduces the risk of breaking the animation by having a view sliding to different positions...</li> </ul> <p>Code update:</p> <pre><code>@interface ViewController () @property (nonatomic, strong) UIButton *showCitiesButton; @property (nonatomic, strong) CGRect citiesInitialFrame; @property (nonatomic, strong) CGRect citiesVisibleFrame; @property (nonatomic, strong) UITableView *tableViewCities; @property (nonatomic, strong) UIView *citiesContainer; @property (nonatomic, strong) BOOL citiesVisible; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Setting up the cities view self.citiesInitialFrame = CGRectMake(10, -180, 300, 180); self.citiesVisibleFrame = CGRectMake(10, 0, 300, 180); self.tableViewCities = [[UITableView alloc] init ...]; self.citiesContainer = [[UIView alloc] init ...]; // ... self.citiesVisible = NO; [self.showCitiesButton addTarget:self action:@selector(showCitiesButtonDidTouchUpInside:) forControlEvents:UIControlEventTouchUpInside]; } - (void)showCitiesButtonDidTouchUpInside:(id)sender { [self.showCitiesButton setEnabled:NO]; if (self.citiesVisible) { // Cities view is visible, sliding it out of the screen [UIView animateWithDuration:0.5 animations:^{ self.citiesContainer.frame = self.citiesInitialFrame; } completion:^(BOOL finished) { self.citiesVisible = NO; [self.showCitiesButton setEnabled:YES]; }]; } else { // Cities view is not visible, sliding it in [UIView animateWithDuration:0.5 animations:^{ self.citiesContainer.frame = self.citiesVisibleFrame; } completion:^(BOOL finished) { self.citiesVisible = YES; [self.showCitiesButton setEnabled:YES]; }]; } } @end </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload