链接图层及其子图层的动画(子图层模型效果超级图层动画)

时间:2012-10-16 02:31:47

标签: ios core-animation

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

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

1 个答案:

答案 0 :(得分:0)

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

如果有人将fromValuetoValue视为时间轴上的位置,问题是在子图层在时间轴上点击fromValue点之前,它正在解决其模型层确定其位置。通过使用kCAFillModeBackwards,有效地扩展了fromValue以覆盖它之前的时间线,因此iOS知道在动画实际开始之前要呈现的内容。

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