【问题标题】:CATransition Push transition replacing content earlyCATransition 提前推送过渡替换内容
【发布时间】:2012-11-09 04:21:06
【问题描述】:

我在 Mac 应用程序中有一个简单的动画设置,其中页面控件交换了一些视图。我非常接近获得我想要的类似分页的动画,但是有一个小问题。分页工作正常,新页面动画正确到位,但新页面也替换了动画之前的初始页面。我希望最终让被推出的视图保持不变,而不是提前更改到新页面。 Here is a video showing the problem,动画持续时间减慢到 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标签是因为此代码与平台无关。

【问题讨论】:

    标签: ios macos cocoa core-animation caanimation


    【解决方案1】:

    我使用CAAnimationBlocks 解决了类似的问题(这是我的original 的分支,作者朝我不太关心的方向发展)。

    想法是将removedOnCompletion 设置为NO,将fillMode 设置为kCAFillModeForwards,并在完成块中执行实际的切换(无动画)。这仅在 OSX 下进行了测试(您可能需要针对 iOS 的不同解决方案,它已经支持完成块)。

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-27
      • 2016-07-28
      • 2017-12-06
      • 2012-11-24
      相关资源
      最近更新 更多