2012-03-04 9 views
3

アニメーションがサブビューの層に添加されている場合、いくつかの理由で、CATransitionは、サブビューを追加した直後にアニメーション化しません。サブビューが追加された直後にサブビューのレイヤーに追加された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]; 

しかし、アニメーションは若干の遅延の後に追加された場合:

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]; 

-

- (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]; 
} 

予想通り遷移が動作します。これには何らかの理由がありますか?

答えて

7

この現象もあります。レイヤーはアクセスできる「モデル」レイヤー階層にありますが、実際に画面に表示されているものを表す「実際の」データ構造にはまだ追加されていないからです。

とにかく、あなたはこのよう[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]; 
+1

また、あなたはフラッシュの前に 'clearColor'にあなたの背景色を設定し、後redColor''にそれを変更することもできますフラッシュ。 –

関連する問題