When the view controller’s view is first displayed, I want to start an animation in which all the elements in the view controller slide from the outside at the bottom of the screen to their natural positions. To do this, I execute subview.frame.origin.y += self.view.frame.size.height
in viewDidLayoutSubviews
. I also tried viewWillAppear
, but it doesn't work at all. Then I animate them to their natural positions using subview.frame.origin.y -= self.view.frame.size.height
in viewDidAppear
.
The problem is that viewDidLayoutSubviews
is called several times throughout the life of the controller. That way, when things like showing the keyboard, all of my content is replaced again outside the view.
Is there a better way to do this? Do I need to add some kind of flag to check if the animation is already running?
EDIT: here is the code. Here, I call prepareAppearance
on viewDidLayoutSubviews
, which works, but viewDidLayoutSubviews
is called several times over the life of the controller.
- (void)viewDidLayoutSubviews { [super viewDidLayoutSubviews]; [self prepareAppearance]; } - (void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; [self animateAppearance]; } - (NSArray *)animatableViews { return @[self.createAccountButton, self.facebookButton, self.linkedInButton, self.loginButton]; } - (void)prepareAppearance { NSArray * views = [self animatableViews]; NSUInteger count = [views count]; for (NSUInteger it=0 ; it < count ; ++it) { UIView * view = [views objectAtIndex:it]; CGRect frame = view.frame;
source share