如何使用翻转动画添加子视图?

6
如果你创建一个全新的单页面应用,并将该代码放在一个按钮后面:
UIView *blah = [[UIView alloc] initWithFrame:CGRectMake(0,0,100,100)];
blah.backgroundColor = [UIColor grayColor];
[UIView transitionWithView:blah duration:1
                   options:UIViewAnimationOptionTransitionFlipFromRight
                animations:^{
                    [self.view addSubview:blah];
                }
                completion:^(BOOL finished){

                }];

子视图会立即添加,没有任何动画。如果您先添加子视图,然后尝试对其进行动画处理... 您将遇到同样的问题。
    UIView *blah = [[UIView alloc] initWithFrame:CGRectMake(0,0,100,100)];
    [self.view addSubview:blah];
    [UIView transitionWithView:blah duration:1
                       options:UIViewAnimationOptionTransitionFlipFromRight
                    animations:^{
                        blah.backgroundColor = [UIColor grayColor];
                    }
                    completion:^(BOOL finished){

                    }];

在添加子视图时或之后如何为其制作翻转动画?

当您使用容器视图而不是 [UIView transitionWithView:containerView ...] 时会发生什么? - guenis
如果我使用self.view作为过渡视图,那么它会翻转整个屏幕,完成后显示我的子视图。我希望只在显示子视图时翻转子视图。 - William T.
2个回答

12

通常需要先放置包含动画的容器:

- (void)viewDidLoad
{
    [super viewDidLoad];

    CGRect frame = CGRectMake(0, 0, 100, 100);

    _container = [[UIView alloc] initWithFrame:frame];
    _container.backgroundColor = [UIColor lightGrayColor];
    [self.view addSubview:_container];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];

    UIView *subview = [[UIView alloc] initWithFrame:_container.bounds];
    subview.backgroundColor = [UIColor darkGrayColor];

    [UIView transitionWithView:_container
                      duration:1.0
                       options:UIViewAnimationOptionTransitionFlipFromRight
                    animations:^{
                        [_container addSubview:subview];
                    }
                    completion:NULL];
}

1
没错,那个有效。只需创建一个预加载的子视图容器就可以了!谢谢,Rob! - William T.
你能否在一个回调函数中添加容器并进行转换? - Paweł Brewczynski

1

This is worth a try:

UIView *blah = [[UIView alloc] initWithFrame:CGRectMake(0,0,100,100)];
blah.backgroundColor = [UIColor grayColor];
[self.view addSubview:blah];
blah.alpha = 0.0; //Or with blah.hidden = TRUE and then FALSE inside the animation block
[UIView transitionWithView:blah
                  duration:1
                   options:UIViewAnimationOptionTransitionFlipFromRight
                animations:^{
                     blah.alpha = 1.0;
                }
                completion:^(BOOL finished){

                }];

哈哈,我也试过了,但是没有翻转效果,不过子视图确实有淡入效果。我真的需要那个翻转效果。;( - William T.

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接