我正在尝试动画从窗口滑出一个视图并同时滑入另一个视图。像这样:
Window
------------------
|----------------| ------------------
|| || | |
|| view || | view |
|| sliding out || | sliding in |
|----------------| ------------------
------------------
<--- <---
我正在使用图层支持的视图,尝试使用隐式动画和 CATransaction。发生的事情是视图滑出动画,但滑入的视图只是跳到位。这是我的代码:
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,就像我滑入的所有视图一样。