【问题标题】:Unable to update the model layer upon CoreAnimation completionCoreAnimation 完成后无法更新模型层
【发布时间】:2014-06-26 00:11:19
【问题描述】:

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

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

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

提前致谢!

【问题讨论】:

    标签: ios ios7 core-animation swift


    【解决方案1】:

    element.layer?.position 是一个可选链,可能导致 nil。您不能将任何内容分配给 nil。首先确保您可以获取图层,然后设置其位置。

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

    【讨论】:

    • 非常感谢@connor!我可以确认这确实起到了作用,尽管现在一切都消失了,然后我才能看到任何类型的动画正在发生。我当然会再考虑一下。 :)
    猜你喜欢
    • 2012-05-05
    • 2013-12-16
    • 1970-01-01
    • 2015-06-05
    • 2014-04-26
    • 1970-01-01
    • 2019-10-31
    • 1970-01-01
    • 2014-07-19
    相关资源
    最近更新 更多