嵌套的参数化闭包参数异常

时间:2014-08-24 02:05:05

标签: ios objective-c uiview swift closures

我正在尝试使用UIView.animateWithDuration执行一组嵌套动画,但无论我使用什么闭包返回参数,我似乎都会得到异常。

  

无法调用“animateWithDuration”'使用类型的参数列表   '(NSTimeInterval,延迟:NSTimeInterval,选项:   UIViewAnimationOptions,动画:() - >无效,完成:(布尔) - >   空隙)'

这是违规行为

func animateLikeButton(button: UIButton?)
{
    button?.userInteractionEnabled = false;

    let pixelsToScale = 9.0;
    let pixelsToShrink = 4.0;

    let buttonFrame = button?.frame

    // Big
    let scaleOriginX = Double(buttonFrame!.minX) - pixelsToScale / 2.0
    let scaleOriginY = Double(buttonFrame!.minY) - pixelsToScale / 2.0
    let scaleSizeX = Double(buttonFrame!.width) + pixelsToScale
    let scaleSizeY = Double(buttonFrame!.height) + pixelsToScale

    // Small
    let shrinkOriginX = Double(buttonFrame!.minX) + pixelsToScale / 2.0
    let shrinkOriginY = Double(buttonFrame!.minY) + pixelsToScale / 2.0
    let shrinkSizeX = Double(buttonFrame!.width) - pixelsToScale
    let shrinkSizeY = Double(buttonFrame!.height) - pixelsToScale

    UIView.animateWithDuration(NSTimeInterval(0.4), delay:NSTimeInterval(0), options: UIViewAnimationOptions.CurveEaseInOut,
        animations:
        {
            () -> Void in
            button?.frame = CGRect(origin: CGPoint(x: scaleOriginX, y: scaleOriginY), size: CGSize(width: scaleSizeX, height: scaleSizeY))
        },
        completion:
        {
            (finished: Bool) -> Void in
            UIView.animateWithDuration(NSTimeInterval(0.2), delay:NSTimeInterval(0.1), options: UIViewAnimationOptions.CurveEaseInOut,
                animations:
                {
                    () -> Void in
                    button?.frame = CGRect(origin: CGPoint(x: shrinkOriginX, y: shrinkOriginY), size: CGSize(width: shrinkSizeX, height: shrinkSizeY))
                },
                completion:
                {
                    (finished: Bool) -> Void in
                    UIView.animateWithDuration(NSTimeInterval(0.2), delay:NSTimeInterval(0), options: UIViewAnimationOptions.CurveEaseInOut,
                        animations:
                        {
                            () -> Void in
                            button?.frame = buttonFrame!
                        },
                        completion:
                        {
                            (finished: Bool) -> Void in
                            button?.userInteractionEnabled = true
                        }
                    )
                }
            )
        }
    )
}

老实说,我必须为封闭返回参数(有和没有选项)尝试了所有可能的组合,但没有运气。例如:

(_) -> Void in
(finished: Bool) in
(finished: Bool) -> bool in
finished in
_ in

我有什么建议可以尝试吗?

2 个答案:

答案 0 :(得分:5)

添加明确的return可以解决问题。下面的代码显示了一个示例:

UIView.animateWithDuration(1.0, delay: 1.0, options: nil, animations:
{
    self.imgIcon?.alpha = 0.0
    return
},
completion:
{
    finished in
    self.imgIcon?.hidden = true
    return
})

答案 1 :(得分:0)

问题似乎并非所有命名参数都是必需的,并且花括号和括号会根据嵌套而改变。

UIView.animateWithDuration(NSTimeInterval(0.4), delay: NSTimeInterval(0.0), options: UIViewAnimationOptions.CurveEaseInOut, animations: { () -> Void in
        button?.frame = CGRect(origin: CGPoint(x: scaleOriginX, y: scaleOriginY), size: CGSize(width: scaleSizeX, height: scaleSizeY))
    }) { (finished) -> Void in
        UIView.animateWithDuration(NSTimeInterval(0.2), delay: NSTimeInterval(0.1), options: UIViewAnimationOptions.CurveEaseInOut, animations: { () -> Void in
            button?.frame = CGRect(origin: CGPoint(x: shrinkOriginX, y: shrinkOriginY), size: CGSize(width: shrinkSizeX, height: shrinkSizeY))
        }, completion: { (finished) -> Void in
            UIView.animateWithDuration(NSTimeInterval(0.2), delay: NSTimeInterval(0.0), options: UIViewAnimationOptions.CurveEaseInOut, animations: { () -> Void in
                button?.frame = buttonFrame!
            }, completion: { (finished) -> Void in
                button?.userInteractionEnabled = true
            })
        })
    }