无法在 Swift 中的 animateWithDuration 动画块中进行变换更改

Can't put transform change in animation block of animateWithDuration in Swift

我正在尝试更改 Swift 中的简单 animateWithDuration 调用的动画块内图层的变换:

UIView.animateWithDuration(0.5, delay: 0.05, usingSpringWithDamping: 0.8, initialSpringVelocity: 0.5, options: UIViewAnimationOptions.CurveEaseIn , animations: {
            self.zigZag!.layer.mask.transform = CGAffineTransformMakeTranslation(100, 0)
            }, completion: { (finished: Bool) in
        })

然而,这总是给我带来以下构建错误:

Cannot invoke 'animateWithDuration' with an argument list of type '(Double, animations: () -> Void)'

如果我删除 trasnform 行或用类似这样的其他内容替换它:

self.zigZag!.center = self.view.center

然后构建错误消失,应用构建正常。

此时我不确定我应该如何更改动画块内的变换并使其正确构建。

您可以完全省略 Void -> in;导致问题的真正罪魁祸首是这一行:

self.zigZag!.layer.mask.transform = CGAffineTransformMakeTranslation(100, 0)

因为类型不匹配。

应该是:

Swift 2

UIView.animateWithDuration(0.5, delay: 0.05, usingSpringWithDamping: 0.8, initialSpringVelocity: 0.5, options: UIViewAnimationOptions.CurveEaseIn, animations: {
   self.view!.layer.mask.transform = CATransform3DMakeTranslation(100.0, 0.0, 0.0)
}, completion: nil)

Swift 3, 4, 5

UIView.animate(withDuration: 0.5, delay: 0.05, usingSpringWithDamping: 0.8, initialSpringVelocity: 0.5, options: UIView.AnimationOptions.curveEaseIn, animations: {
    self.view!.layer.mask.transform = CATransform3DMakeTranslation(100.0, 0.0, 0.0)
}, completion: nil)