0

I try to write an animation with Swift 5, following are some codes

let animations:(() -> Void) = {
    self.keyboardOPT.transform = CGAffineTransform(translationX: 0,y: -deltaY)
    if duration > 0 {
        let options = UIView.AnimationOptions(rawValue: UInt((userInfo[UIResponder.keyboardAnimationCurveUserInfoKey] as! NSNumber).intValue << 16))
        UIView.animate(withDuration: duration, delay: 0, options: options, animations: animations, completion: nil)
    } else {
        animations()
    }
}

But in animations: animations and animations() it shows error:

Variable used within its own initial value

2
  • 2
    The name of your block constant is animations => let animations:.... Inside this block, in your else statement, you also call animations(). So your calling the block inside the block. You cannot do this. Commented Apr 16, 2019 at 5:37
  • Possible duplicate of Use block in swift giving error "Variable used within its own initial value" Commented Apr 16, 2019 at 9:02

1 Answer 1

1

You can not call itself when initializing. You can achieve it like this also.

var animations:(() -> Void)!

animations = {
    animations()
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.