如果在添加子视图后直接将CATransition添加到子视图层,为什么CATransition不会工作?

Pro*_*ere 5 iphone core-animation

出于某种原因,如果在添加子视图后直接将动画添加到子视图的图层,则CATransition将不会设置动画:

transitionView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[transitionView setBackgroundColor:[UIColor redColor]];
[[self contentView] addSubview:transitionView];

CATransition *animation = [CATransition animation];
[animation setDelegate:self];
[animation setType:@"cube"];
[animation setDuration:1.0];
[animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn]];
[animation setSubtype:@"fromRight"];

[[transitionView layer] addAnimation:animation forKey:nil];
Run Code Online (Sandbox Code Playgroud)

但是如果在稍微延迟后添加动画:

transitionView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[transitionView setBackgroundColor:[UIColor redColor]];
[[self contentView] addSubview:transitionView];

CATransition *animation = [CATransition animation];
[animation setDelegate:self];
[animation setType:@"cube"];
[animation setDuration:1.0];
[animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn]];
[animation setSubtype:@"fromRight"];

[self performSelector:@selector(animate) withObject:nil afterDelay:0.00001];
Run Code Online (Sandbox Code Playgroud)

-

- (void)animate
{
    CATransition *animation = [CATransition animation];
    [animation setDelegate:self];
    [animation setType:@"cube"];
    [animation setDuration:1.0];
    [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn]];
    [animation setSubtype:@"fromRight"];

    [[transitionView layer] addAnimation:animation forKey:nil];
}
Run Code Online (Sandbox Code Playgroud)

过渡将按预期进行.这有什么理由吗?

rob*_*off 9

我也看到过这种行为.我认为这是因为该层位于您可以访问的"模型"层次结构中,但尚未添加到表示屏幕上实际内容的"真实"数据结构中.

无论如何,你可以[CATransaction flush]像这样使用它来解决它:

transitionView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[transitionView setBackgroundColor:[UIColor redColor]];
[[self contentView] addSubview:transitionView];

// Synchronize the implementation details with my changes so far.
[CATransaction flush];

CATransition *animation = [CATransition animation];
[animation setDelegate:self];
[animation setType:@"cube"];
[animation setDuration:1.0];
[animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn]];
[animation setSubtype:@"fromRight"];

[[transitionView layer] addAnimation:animation forKey:nil];
Run Code Online (Sandbox Code Playgroud)