在CoreAnimation完成时无法更新模型层

时间:2014-06-26 00:11:20

标签: ios ios7 core-animation swift

我有以下功能。它的目的是将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抱怨的,当我尝试直接在模型层更新属性时,这些元素不会重置到它们的初始位置。

enter image description here

它也给了我这些奇怪的建议:

enter image description here

为什么我不能设置图层的位置?

提前致谢!

1 个答案:

答案 0 :(得分:2)

element.layer?.position是一个可选链,可能会导致nil。您无法将任何内容分配给nil。首先确保你可以获得图层,然后设置它的位置。

if let layer = element.layer {
   layer.position = CGPointMake(-330.0, 0.0);
}