vendredi 9 juin 2017

How to implement function with optional animation in Swift3

I want to implement a function in Swift 3 which change something in UI. I want to add to this function a parameter to decide if this changes will do with/without animation.

My function:

func doSomeChanges(animated: Bool) {
    if animated {
        UIView.animate(withDuration: 0.25) {
            //UI changes
        }
    }
    else {
        //UI changes
    }
}

My 1st is to use inside functions feature from Swift:

func doSomeChanges(animated: Bool) {
    func uiChanges() {
        //UI changes
    }

    if animated {
        UIView.animate(withDuration: 0.25) {
            uiChanges()
        }
    }
    else {
        uiChanges()
    }
}

2nd is to set duration to 0.0 but I don't know it is a good practice:

func doSomeChanges(animated: Bool) {
    UIView.animate(withDuration: animated ? 0.25 : 0.0) {
        //UI changes
    }
}

How should this be done correctly?

Aucun commentaire:

Enregistrer un commentaire