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){

                    }];

到底如何在添加子视图时或添加后立即为子视图的翻转设置动画?

4

2 回答 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];
}
于 2013-04-25T21:40:29.427 回答
1

这值得一试:

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){

                }];
于 2013-04-25T21:31:10.163 回答