gpt4 book ai didi

objective-c - 隐式 CALayer 动画不会产生动画

转载 作者:行者123 更新时间:2023-12-03 17:41:51 25 4
gpt4 key购买 nike

我正在尝试制作从窗口滑出一个 View 并同时滑入另一个 View 的动画。就像这样:

     Window
------------------
|----------------| ------------------
|| || | |
|| view || | view |
|| sliding out || | sliding in |
|----------------| ------------------
------------------
<--- <---

我正在使用图层支持的 View ,尝试使用隐式动画和 CATransaction。发生的情况是,滑出的 View 具有动画效果,但滑入的 View 只是跳入到位。这是我的代码:

    NSView *slidingInView = nil;
NSView *slidingOutView = nil;
if (currentState.primaryView != nextState.primaryView) {
// Slide in a primary view
[self.window.contentView addSubview:nextState.primaryView];
CGRect frame = nextState.primaryView.layer.frame;
frame.origin.x = slideFromRight ? frame.size.width : 0 - frame.size.width;
[nextState.primaryView.layer setPosition: frame.origin];

slidingInView = nextState.primaryView;
slidingOutView = currentState.primaryView;
}

[CATransaction begin];
[CATransaction setValue:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]
forKey:kCATransactionAnimationTimingFunction];
[CATransaction setAnimationDuration:1.0f];
[CATransaction setCompletionBlock:^{
[slidingOutView removeFromSuperview];
}];

// Slide in the new view
slidingInView.layer.position = CGPointMake(0.0f, 0.0f);

// if there is a view in place already, slide it out to the left.
if (slidingOutView != nil) {

CGRect frame = slidingOutView.layer.frame;
frame.origin = CGPointMake(slidingOutView.layer.frame.size.width, slidingOutView.layer.frame.origin.y);
if (slideRight)
frame.origin = CGPointMake(0-slidingOutView.layer.frame.size.width, slidingOutView.layer.frame.origin.y);

[slidingOutView.layer setPosition: frame.origin];
}

[CATransaction commit];

这对我来说完全是个谜。有人有线索吗?我窗口的 contentView 具有 setWantsLayer: YES,就像我滑入的所有 View 一样。

最佳答案

隐式动画仅适用于独立层(不支持 View 的层)。

您需要显式地为图层设置动画(并更改属性值,因为默认情况下不会发生这种情况)。它看起来像

CABasicAnimation *slideIn = [CABasicAnimation animationWithKeyPath:@"position"];
[slideIn setToValue:[NSValue valueWithCGPoint:myNewEndPoint]];
[slideIn setFromValue:[NSValue valueWithCGPoint:[[slidingInView layer] position]]];
[[slidingInView layer] addAnimation:slideIn forKey:@"mySlideInAnimation"];
[[slidingInView layer] setPosition:myNewEndPoint];
<小时/>

另外:除非您更改了 anchor ,否则图层的位置应位于中心(这意味着将其设置为 (0,0) 将使图层的 3/4 出现在屏幕外

关于objective-c - 隐式 CALayer 动画不会产生动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11163031/

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