首页 文章

嵌套参数化闭包参数异常

提问于
浏览
1

我试图使用 UIView.animateWithDuration 执行一组嵌套动画但是我似乎得到异常,无论我使用的闭包返回参数 .

无法使用类型'(NSTimeInterval,delay:NSTimeInterval,options:UIViewAnimationOptions,animations :() - > Void,completion:(Bool) - > Void)'

这是违规的功能

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 回答

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

相关问题