ios - Swift 中的下拉动画 - 完成 block 错误

标签 ios swift animation

我正在遵循此处列出的代码:Dropdown list in swift

即:

func animateDropDownToFrame(frame: CGRect, completion:() -> Void) {
    if (!self.isAnimating) {
        self.isAnimating = true
        UIView.animateWithDuration(0.5, delay: 0.0, options: .CurveEaseInOut, animations: { () -> Void in
            self.dropDownView.frame = frame
            }, completion: {(completed: Bool) -> Void in
            self.isAnimating = false
            if (completed) {
                completion()
            }
            }
        )
    }
}

并继续在完成时获取错误:

Expected ',' separator

Expected member name or constructor call after type name

请注意,我对完成行进行了一些更改,因为我认为括号位于错误的位置,但我不认为这就是问题所在。

可能是什么问题(以及我自己调试此问题的最佳方法是什么)?

最佳答案

所写的语法很难理解。 Swift 编译器也可能存在一个错误,导致它无法解析您编写的内容。 Swift 允许嵌套函数,因此您可以执行以下操作:

func animateDropDownToFrame(frame: CGRect, completion:() -> Void) {
    func animate() {
        self.dropDownView.frame = frame
    }

    func animationDidComplete(completed : Bool) {
        self.isAnimating = false
        if (completed) {
            completion()
        }
    }

    if (!self.isAnimating) {
        self.isAnimating = true
        UIView.animateWithDuration(0.5, delay: 0.0, options: .CurveEaseInOut, animations: animate, completion: animationDidComplete)
    }
}

(我现在可以访问 Xcode,因此可能存在一些拼写错误。请根据需要进行更正)

关于ios - Swift 中的下拉动画 - 完成 block 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31413070/

相关文章:

ios - (iOS) 如何使用 shapeLayer 为圆角矩形制作动画?

html - 如何摆脱媒体查询中的一个动画?

ios - Objective C - 使用 NSString 的 Switch 语句

ios - Xcode 错误地设置为使用旧版构建系统

ios - 无法从 CoreData 中删除 NSOrderedSet

ios - AppDelegate Swift 中的 reloadData

ios - 使用动画从按钮位置放大和缩小 UIView

ios - 具有表情符号时 TTTAttributedLabel 不显示最后一行

ios - 为什么 AVPlayer 不停止加载数据?

ios - TodayWidget openURL 多次触发应用:openURL