简体   繁体   English

动画期间未调用CALayer setPosition

[英]CALayer setPosition not called during animation

I have a custom CALayer that I am animating using a CAAnimationGroup to follow a path and rotate at a tangent to the path: 我有一个自定义的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"];

I need to be able to track the changes to the position and rotation of the layer as it progresses along the path. 我需要能够跟踪沿着路径前进的图层的位置和旋转的变化。 I have overridden both setPosition and setTransform (calling super setPosition and super setTranform ) and then logging their values. 我已经覆盖了setPositionsetTransform (调用super setPositionsuper setTranform ),然后记录了它们的值。 Neither of these values appear to be set during the animation. 这些值似乎都没有在动画期间设置。

How can I get the position and rotation updates from within the CALayer class itself as it animates? 在动画时,如何从CALayer类本身中获取位置和旋转更新?

Core Animation doesn't work like that 核心动画不能那样工作

Sorry. 抱歉。 That is not how Core Animation work. 那不是核心动画的工作方式。 When you add an animation to a layer it doesn't change the model of that layer. 将动画添加到图层时,它不会更改该图层的模型。 Only the presentation. 仅介绍。

When you configure then animation to not remove itself upon completion 配置动画后,动画在完成时不会自行删除

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

you are actually causing an inconsistency between what is shown on screen and the model of that layer. 您实际上是在屏幕上显示的内容与该层的模型之间造成不一致。 If you for instance had a button that you animated like this you would get very surprised/angry by the fact that it "no longer responds to toucher" or even more funny "responds to touches from it's 'old' location". 例如,如果您有一个像这样动画的按钮,您将对其“不再响应触摸器”或更有趣的“响应来自其“旧”位置的触摸”感到非常惊讶/生气。

Semi-solutions 半解决方案

Depending on what and how often you actually need the updates you could either periodically check the value of the presentationLayer during the animation or use a CADisplayLink to run some code when the screen changes. 根据您实际需要更新的频率和频率,可以在动画过程中定期检查presentationLayer的值,也可以在屏幕变化时使用CADisplayLink运行一些代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM