【发布时间】:2015-01-22 11:37:22
【问题描述】:
当第一次显示视图控制器的视图时,我想运行一个动画,其中视图控制器中的所有元素从屏幕底部之外滑动到它们的自然位置。为此,我在viewDidLayoutSubviews 中执行subview.frame.origin.y += self.view.frame.size.height。我也试过viewWillAppear,但它根本不起作用。然后我用subview.frame.origin.y -= self.view.frame.size.height in viewDidAppear 将它们动画到它们的自然位置。
问题是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) {
}];
}
}
【问题讨论】:
标签: ios objective-c uiview uiviewcontroller