Carlos
Carlos

Reputation: 5445

Nested parametrized closure argument exception

I am trying to perform a set of nested animations using UIView.animateWithDuration however I seem to get exceptions regardless of the closure return parameters i use.

Cannot invoke 'animateWithDuration' with an argument list of type '(NSTimeInterval, delay: NSTimeInterval, options: UIViewAnimationOptions, animations: () -> Void, completion: (Bool) -> Void)'

This is the offending function

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
                        }
                    )
                }
            )
        }
    )
}

In all honestly I must have tried every possible combination for the closure return parameters (with and without optionals) but no luck. For example:

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

Any suggestions for me to try?

Upvotes: 1

Views: 1136

Answers (2)

Vaibhav Jhaveri
Vaibhav Jhaveri

Reputation: 1605

Adding an explicit return should resolve the problem. Below code shows an example :

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

Upvotes: 5

Carlos
Carlos

Reputation: 5445

The issue seems to be that not all named parameters are required, as well as, the curly braces and parenthesis change depending on the nesting.

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
            })
        })
    }

Upvotes: 0

Related Questions