gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 18:54:03 26 4
gpt4 key购买 nike

出于某种原因,如果在添加 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com