Ios 如何在闭包内创建闭包?

Ios 如何在闭包内创建闭包?,ios,swift,Ios,Swift,我正在尝试用Swift创建一个函数,以便制作一个重复的动画(我知道您可以使用。repeat,但我不想使用它)。在我的completionclosure中,我得到了一个错误。以下是我目前的代码: import UIKit var withDurationVar:TimeInterval = 0 var optionsVar:UIViewAnimationOptions? var iterateVar = 0 var animationsVar:(() -> ()?)? var comple

我正在尝试用Swift创建一个函数,以便制作一个重复的动画(我知道您可以使用
。repeat
,但我不想使用它)。在我的
completion
closure中,我得到了一个错误。以下是我目前的代码:

import UIKit

var withDurationVar:TimeInterval = 0
var optionsVar:UIViewAnimationOptions?
var iterateVar = 0
var animationsVar:(() -> ()?)?
var completionVar:(() -> ()?)?

var numberOfIterations = 0


func repeatingAnimation(withDuration:TimeInterval, options:UIViewAnimationOptions, iterate:Int, animations:@escaping () -> (), completion:@escaping () -> ()) {

    withDurationVar = withDuration
    optionsVar = options
    iterateVar = iterate
    animationsVar = animations
    completionVar = completion

}


func animationRepeat() {

    UIView.animate(withDuration: withDurationVar, delay: 0, options: optionsVar!, animations: animationsVar as! () -> Void, completion: { (Finished) in

        // Increase number of iterations
        numberOfIterations += 1

        // If it has not yet done every iteration needed
        if numberOfIterations != iterateVar {

            // Repeat animation
            animationRepeat()

        }
        else {

            completionVar // Where I get an error.  'Expression resolves to an unused I-value'

        }

    })

}
不过,我可以这样做:

func animationRepeat() {

    UIView.animate(withDuration: withDurationVar, delay: 0, options: optionsVar!, animations: animationsVar as! () -> Void, completion: completionVar)

}

那么,如何将
repeatingAnimation
函数中的
completion
转换为
animationRepeat
completion
以及其余的代码呢?谢谢

如果有人想将我的代码用作重复动画功能,我已经更新了代码:)

导入UIKit
//使一些变量可以在整个文件中访问
var withDurationVar:TimeInterval=0
变量选项变量:UIViewAnimationOptions?
var iterateVar=0
var动画var:(()->()?)?
变量完成变量:(()->()?)?
var numberOfIterations=0
//要调用的函数
func repeatingAnimation(持续时间:时间间隔,选项:UIViewAnimationOptions,迭代:Int,动画:@escaping()->(),完成:@escaping()->()){
//根据参数设置全局变量
withDurationVar=withDuration
optionsVar=选项
iterateVar=迭代
animationsVar=动画
completionVar=完成
//还没有开始重复
numberOfIterations=0
//开始重复
animationRepeatForFunction()
}
//仅由repeatingAnimation或其本身调用,而非用户调用的函数
func animationRepeatForFunction(){
动画(withDuration:withDurationVar,延迟:0,选项:optionVar!,动画:{animationVar?()},完成:{(完成)in
//增加迭代次数
迭代次数+=1
//如果它还没有完成所需的每个迭代
如果numberOfIterations
使用
repeatingAnimation(…)


.repeat
函数实际上只能用于无限循环,不需要停止。在我的例子中,我想重复有限的次数(终止),所以我制作了这个自定义函数,它可以有自己的
.swift
文件。

你会得到什么错误?
completionVar?()
@rmaddy正是我所需要的!谢谢:)你的回答并没有解释问题所在,也没有解释这个答案是如何解决问题的。不要依赖这些评论。在答案中填入所有相关信息。@rmaddy我已在代码中填入注释,并解释了原始解决方案不起作用的原因
import UIKit

// Make some variables to be accessed across the file
var withDurationVar:TimeInterval = 0
var optionsVar:UIViewAnimationOptions?
var iterateVar = 0
var animationsVar:(() -> ()?)?
var completionVar:(() -> ()?)?

var numberOfIterations = 0


// The function to call
func repeatingAnimation(withDuration:TimeInterval, options:UIViewAnimationOptions, iterate:Int, animations:@escaping () -> (), completion:@escaping () -> ()) {

    // Set the global variables from the parameters
    withDurationVar = withDuration
    optionsVar = options
    iterateVar = iterate
    animationsVar = animations
    completionVar = completion

    // Has not started repeating yet
    numberOfIterations = 0

    // Start repeat
    animationRepeatForFunction()

}


// The function which is ONLY called by the repeatingAnimation or itself, not the user
func animationRepeatForFunction() {

    UIView.animate(withDuration: withDurationVar, delay: 0, options: optionsVar!, animations: {animationsVar?()}, completion: { (Finished) in

        // Increase number of iterations
        numberOfIterations += 1

        // If it has not yet done every iteration needed
        if numberOfIterations < iterateVar {

            // Repeat animation
            animationRepeatForFunction()

        }
        else {

            // Perform what the user wanted to do after the repeating animation
            completionVar?()

        }

    })

}