如何在闭包内创建一个闭包?

时间:2018-04-06 22:21:41

标签: ios swift

我正在尝试在Swift中创建一个函数,所以我可以制作一个重复的动画(我知道你可以使用.repeat,但我不想使用它)。在我的completion关闭中,我收到了一个错误。到目前为止,这是我的代码:

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)

}

那么如何将completion函数中的repeatingAnimation加载到completion的{​​{1}}中以及其他代码呢?谢谢!

1 个答案:

答案 0 :(得分:0)

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

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?()

        }

    })

}

使用repeatingAnimation(...)

调用该函数

.repeat函数只能用于无限循环,不需要停止。在我的情况下,我想重复有限次数(终止),所以我创建了这个自定义函数,它可以拥有自己的.swift文件。