我有以下功能。它的目的是在屏幕边界之外为 ui 元素设置动画。
func animateElementsOut(elements: AnyObject...) {
var moveOutAnimation : CAKeyframeAnimation = CAKeyframeAnimation(keyPath: "transform");
moveOutAnimation.delegate = self;
var key1 : NSValue = NSValue(CATransform3D: CATransform3DIdentity);
var key2 : NSValue = NSValue(CATransform3D: CATransform3DMakeTranslation(-300, 0, 0));
var values : NSArray = [key1, key2];
var delayAmount = 0.0;
moveOutAnimation.values = values;
moveOutAnimation.calculationMode = kCAAnimationLinear;
moveOutAnimation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseIn);
moveOutAnimation.duration = 0.5;
moveOutAnimation.additive = true;
for element : AnyObject in elements {
delayAmount += 0.1;
moveOutAnimation.removedOnCompletion = true;
moveOutAnimation.beginTime = CACurrentMediaTime() + delayAmount;
element.layer?.addAnimation(moveOutAnimation, forKey: "move object along the x axis");
// HERE LIES THE PROBLEM
element.layer?.position = CGPointMake(-330.0, 0.0);
}
}
现在这是 xCode 提示的问题,当我尝试直接在模型层上更新属性时,元素不会重置到它们的初始位置。
它还给我这些奇怪的建议:
为什么我不能设置图层的位置?
提前致谢!
最佳答案
element.layer?.position
是一个可选链,可能导致 nil。你不能给 nil 赋值。首先确保你可以获取图层,然后设置它的位置。
if let layer = element.layer {
layer.position = CGPointMake(-330.0, 0.0);
}
关于ios - 无法在 CoreAnimation 完成后更新模型层,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24420575/