如何在单个iOS屏幕中切换带动画的UIViews

时间:2012-12-17 19:17:09

标签: uiview uiviewcontroller uianimation

我想用一个屏幕构建一个相当简单的应用程序。因此,我希望在同一个视图控制器中有两个视图。

View1 - appear for 5 seconds
View2 - appear during gameplay
View1 - appear for 5 seconds
View2 - appear during gameplay
And so on. 

虽然我不知道如何实现这一点,但我确信这种设计模式是有效的。

我搜索了文档,但找不到明确的答案。

我知道这样做的应用程序,但我不知道怎么做。

是viewcontroller.view = View1还是View2?如果是这样,我将如何使用一些不错的动画进行此切换?

我知道动画,但不是在这种情况下。请帮忙......

1 个答案:

答案 0 :(得分:1)

将两个视图添加为viewController视图的子视图:

[viewController.view addSubview:view1];
[viewController.view addSubview:view2];

然后使用动画块:

// the following code will replace view1 with view2 after a 5 second delay

// this ensures view2 is behind view1
[viewController.view bringSubviewToFront:view2];
[viewController.view bringSubviewToFront:view1];

// get view2 ready for the animation
view2.alpha = 0;
view2.hidden = NO;

// delay for 5 seconds before executing animation
[UIView animateWithDuration:duration delay:5 options:(UIViewAnimationOptionCurveLinear | UIViewAnimationOptionAllowUserInteraction) animations:^{

    //fade out
    view1.alpha = 0;

    // fade in
    view2.alpha = 1;

} completion:^(BOOL finished) {

    // hide it after animation completes
    view1.hidden = YES;

    // bring view2 to front (even though view1 is not visible, it is still above view2)
    [viewController.view bringSubviewToFront:view2];
}];

用view2替换view1。等等。