首次显示视图控制器的视图时,我想运行一个动画,其中视图控制器中的所有元素从屏幕底部外部滑动到其自然位置.为此,我在viewDidLayoutSubviews中执行subview.frame.origin.y = self.view.frame.size.height.我也尝试过viewWillAppear,但它根本不起作用.然后我使用viewDidAppear中的subview.frame.origin.y – = self.view.frame.size.height将它们设置为自然位置.
问题是viewDidLayoutSubviews在整个视图控制器的生命周期中被调用了好几次.因此,当显示键盘的事情发生时,我的所有内容都会再次在视图之外被替换.
这样做有更好的方法吗?我是否需要添加某种标志来检查动画是否已经运行?
编辑:这是代码.这里我在viewDidLayoutSubviews中调用prepareAppearance,它可以工作,但是在整个控制器的生命周期中多次调用viewDidLayoutSubviews.
- (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; // Move the views outside the screen,to the bottom frame.origin.y += self.view.frame.size.height; [view setFrame:frame]; } } - (void)animateAppearance { NSArray * views = [self animatableViews]; NSUInteger count = [views count]; for (NSUInteger it=0 ; it < count ; ++it) { __weak UIView * weakView = [views objectAtIndex:it]; CGRect referenceFrame = self.view.frame; [UIView animateWithDuration:0.4f delay:0.05f * it options:UIViewAnimationOptionCurveEaSEOut animations:^{ CGRect frame = weakView.frame; frame.origin.y -= referenceFrame.size.height; [weakView setFrame:frame]; } completion:^(BOOL finished) { }]; } }