0

我正在尝试链接图层及其子图层的动画。但是,我遇到的问题是子层的动画位置的模型更新在其超级层的动画期间在视觉上是显而易见的。这是我用来链接动画的代码:

// Superlayer. 
CFTimeInterval now = [self.artworkContainer.layer convertTime:CACurrentMediaTime() fromLayer:nil];
CABasicAnimation *slideDown = [CABasicAnimation animationWithKeyPath:@"position"];
slideDown.duration = SLIDE_DOWN_DURATION; //SLIDE_DOWN_DURATION;
slideDown.beginTime = now;
slideDown.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
slideDown.fromValue = [self.artworkContainer.layer valueForKey:@"position"];

CGPoint finalPointOfContainer = CGPointMake(self.artworkContainer.layer.position.x, self.artworkContainer.layer.position.y+verticalDistanceOfFirstFall);
slideDown.toValue = [NSValue valueWithCGPoint:finalPointOfContainer];
self.artworkContainer.layer.position = finalPointOfContainer;
[self.artworkContainer.layer addAnimation:slideDown forKey:@"position"];

// Sublayer 
CABasicAnimation *moveActualArtwork = [CABasicAnimation animationWithKeyPath:@"position"];
moveActualArtwork.duration = timeOfSecondFall;
moveActualArtwork.beginTime = now + SLIDE_DOWN_DURATION;
moveActualArtwork.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
moveActualArtwork.fromValue = [self.artWork.layer valueForKey:@"position"];

CGPoint finalPointOfArtwork = CGPointMake(self.artWork.layer.position.x, self.artWork.layer.position.y+verticalDistanceOfSecondFall);
moveActualArtwork.toValue = [NSValue valueWithCGPoint:finalPointOfArtwork];
self.artWork.layer.position = finalPointOfArtwork; // If this is commented out, the superlayer animation looks correct (of course this isn't a true fix because the sublayer snaps back to its original position without a model update). 

moveActualArtwork.delegate = self;
[self.artWork.layer addAnimation:moveActualArtwork forKey:@"position"];
4

1 回答 1

0

我在写问题时找到了答案。我需要设置第二个动画的 fillMode 属性:moveActualArtwork.fillMode = kCAFillModeBackwards;

如果将fromValueandtoValue视为时间轴上的位置,问题是在子层fromValue到达时间轴上的位置之前,它正在使用其模型层来确定其位置。通过使用 kCAFillModeBackwards,fromValue 被有效地扩展以覆盖它之前的时间线,因此 iOS 知道在动画实际开始之前要渲染什么。

我在 45:31 回顾了 WWDC 2011 视频 Session 421 - Core Animation Essentials 找到了答案。

于 2012-10-16T02:31:47.207 回答