1

我在 Mac 应用程序中有一个简单的动画设置,其中页面控件交换了一些视图。我非常接近获得我想要的类似分页的动画,但是有一个小问题。分页工作正常,新页面动画正确到位,但新页面也会替换动画之前的初始页面。我希望最终让被推出的视图保持不变,而不是提前更改到新页面。这是一个显示问题的视频,动画持续时间减慢到 3 秒。这是代码:

UASourceView *previousSourceView = [self.sourceViewContainer.subviews objectAtIndex:0];
NSInteger previousIndex = [self.sourceViews indexOfObjectIdenticalTo:previousSourceView];

CATransition *pushTransition = [CATransition animation];
[pushTransition setType:kCATransitionPush];
[pushTransition setSubtype:(previousIndex < index) ? kCATransitionFromRight : kCATransitionFromLeft];
[pushTransition setDuration:PANEL_PAGE_SWIPE_ANIMATION_DURATION];
[pushTransition setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
[pushTransition setRemovedOnCompletion:YES];

[CATransaction begin];
[self.sourceViewContainer replaceSubview:previousSourceView with:sourceView];
[CATransaction commit];

[sourceView.layer addAnimation:pushTransition forKey:@"push"];

有一个固定的容器视图self.sourceViewContainer,可以在每个动画上替换一个新的子视图。如上所示,问题是previousSourceView立即被替换sourceView 并且也被推入。请帮我停止立即更换。我哪里错了?

*注意,iOS添加标签是因为此代码与平台无关。

4

1 回答 1

1

我使用CAAnimationBlocks解决了类似的问题(这是我原来的分支,作者已经朝着我不太关心的方向移动)。

这个想法是在完成块中设置和removedOnCompletion执行NO实际的切换(没有动画)。这仅在 OSX 下进行了测试(您可能需要一个不同的 iOS 解决方案,它已经支持完成块)。fillModekCAFillModeForwards

这是我的代码中的一个示例用法,它在棋盘上移动一个棋子,然后再返回:

CABasicAnimation *moveAnimation = [CABasicAnimation animationWithKeyPath:@"position"];
moveAnimation.fromValue = [NSValue valueWithPoint:[self _pointForSquare:move.from()]];
moveAnimation.toValue = [NSValue valueWithPoint:[self _pointForSquare:move.to()]];
moveAnimation.duration = _animateSpeed / 1000.0;
moveAnimation.autoreverses = YES;
moveAnimation.removedOnCompletion = NO;
moveAnimation.fillMode = kCAFillModeForwards;
moveAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
moveAnimation.completion = ^(BOOL finished)
{
    [pieceLayer removeAnimationForKey:@"movePiece"];
    [self _setPieceLayer:pieceLayer toPiece:piece];
    [pieceLayer setNeedsDisplay];
};

[pieceLayer addAnimation:moveAnimation forKey:@"movePiece"];
于 2012-11-09T14:25:14.807 回答