【问题标题】:CALayer setPosition not called during animation动画期间未调用 CALayer setPosition
【发布时间】:2013-03-25 15:21:49
【问题描述】:

我有一个自定义 CALayer,我正在使用 CAAnimationGroup 制作动画,以跟随路径并在路径的切线处旋转:

   // Create the animation path
CAKeyframeAnimation *pathAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
pathAnimation.calculationMode = kCAAnimationPaced;
pathAnimation.fillMode = kCAFillModeForwards;
pathAnimation.removedOnCompletion = NO;

//Setting Endpoint of the animation
CGRect contentBounds = [self contentBounds];
self.boatLayer.bounds = contentBounds;
CGPoint endPoint = CGPointMake(contentBounds.size.width - 150, contentBounds.size.height - 150);

CGMutablePathRef curvedPath = CGPathCreateMutable();
CGPathMoveToPoint(curvedPath, NULL, startPosition.x, startPosition.y);
CGPathAddCurveToPoint(curvedPath, NULL, endPoint.x, 0, endPoint.x, 0, endPoint.x, endPoint.y);
pathAnimation.path = curvedPath;

pathAnimation.duration = 10.0;
pathAnimation.rotationMode = kCAAnimationRotateAuto;
pathAnimation.delegate = self;

// Create an animation group of all the animations
CAAnimationGroup *animationGroup = [[[CAAnimationGroup alloc] init] autorelease];
animationGroup.animations = [NSArray arrayWithObjects:pathAnimation, nil];
animationGroup.duration = 10.0;
animationGroup.removedOnCompletion = NO;

// Add the animations group to the layer (this starts the animation at the next refresh cycle)
[testLayer addAnimation:animationGroup forKey:@"animation"];

我需要能够在图层沿路径前进时跟踪图层位置和旋转的变化。我已经覆盖了setPositionsetTransform(调用super setPositionsuper setTranform),然后记录了它们的值。在动画期间似乎都没有设置这些值。

我如何在 CALayer 类本身的动画中获取位置和旋转更新?

【问题讨论】:

    标签: ios cocoa-touch calayer caanimation cakeyframeanimation


    【解决方案1】:

    Core Animation 不能这样工作

    对不起。这不是核心动画的工作方式。当您将动画添加到图层时,它不会更改该图层的模型。只有演示文稿。

    当您将动画配置为在完成后不自行移除时

    yourAnimation.fillMode = kCAFillModeForwards;
    tourAnimation.removedOnCompletion = NO;
    

    您实际上导致屏幕上显示的内容与该层的模型不一致。例如,如果你有一个像这样动画的按钮,你会因为它“不再响应触摸”或更有趣的“响应来自它的'旧'位置的触摸”这一事实而感到非常惊讶/愤怒。

    半解决方案

    根据您实际需要更新的内容和频率,您可以在动画期间定期检查presentationLayer 的值,或者在屏幕变化时使用CADisplayLink 运行一些代码。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-22
      • 1970-01-01
      相关资源
      最近更新 更多