iphone - 如果在添加 subview 后直接将 CATransition 添加到 subview 的图层,为什么 CATransition 不起作用?

标签 iphone core-animation

出于某种原因,如果在添加 subview 后直接将动画添加到 subview 的图层,则 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];
}

过渡将按预期进行。这有什么原因吗?

最佳答案

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

无论如何,您可以通过使用 [CATransactionlush] 来解决这个问题,如下所示:

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

关于iphone - 如果在添加 subview 后直接将 CATransition 添加到 subview 的图层,为什么 CATransition 不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9552400/

相关文章:

ios - CATransform3DTranslate如何翻译?

ios - CoreAnimation 和 Cocos2D - 我应该选择哪一个?

ios - 在 UIImagePickerController 中启用相机胶卷缩略图

ios - 隐式属性动画不适用于CAReplicatorLayer吗?

objective-c - 如何同步动画 UIView 和 CALayer

ios - 将 UICollectionView 滚动到节标题 View

ios - CALayer 如何在其子层之间转换点?

iphone - 将多个 .wav 转换为 .caf

iphone - iOS - 从选项卡检索到不正确的角标(Badge)值

iphone - 通过gdata获取gmail的所有电子邮件和联系人