ios - 无法在 CoreAnimation 完成后更新模型层

标签 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

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

提前致谢!

最佳答案

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/

相关文章:

ios - 导航 Controller 的问题

iphone - 使用 threadDictionary 这段代码线程安全吗?

ios - 如何测试ios6兼容性?

iphone - CAKeyframeanimation 沿路径移动 UIView

ios - Facebook 测试推送通知 "Device token does not match the environment"

ios - 从 View Controller Xcode 传回数据

iOS7 SKScene 如何让 Sprite 从屏幕边缘反弹?

objective-c - iOS7 状态栏与 View 重叠

ios - 我们什么时候应该使用 CATransformLayer?

ios - 核心动画不适用于 "alpha"值