static func animate(_ duration: TimeInterval,
animations: (() -> Void)!,
delay: TimeInterval = 0,
options: UIViewAnimationOptions = [],
withComplection completion: (() -> Void)! = {}) {
UIView.animate(
withDuration: duration,
delay: delay,
options: options,
animations: {
animations()
}, completion: { finished in
completion()
})
}
Using above class in my swift file and create function like below
SPAnimation.animate(durationScalingRootView,
animations: {
rootViewController.view.transform = CGAffineTransform.identity
},
delay: delayScalingRootView,
options: UIViewAnimationOptions.curveEaseOut,
withComplection: {
finished in
//rootViewController.view.layer.mask = nil
})
Get this error
Contextual closure type '() -> Void' expects 0 arguments, but 1 was used in closure body
The problem is here:
withComplection: {
finished in
//rootViewController.view.layer.mask = nil
}
If you look at your method declaration, the completion handler is of type (() -> Void)!
. It does not take any arguments. Your closure above takes one argument - finished
. As a result, the error occurs.
You remove the finished
argument from your closure:
withComplection: {
//rootViewController.view.layer.mask = nil
}
Or you edit your animate
method to accept a closure taking one argument:
static func animate(_ duration: TimeInterval,
animations: (() -> Void)!,
delay: TimeInterval = 0,
options: UIViewAnimationOptions = [],
withComplection completion: ((Bool) -> Void)? = nil) {
UIView.animate(
withDuration: duration,
delay: delay,
options: options,
animations: {
animations()
}, completion: { finished in
completion?(finished)
})
}
1.) You misspelled completion
2.) remove the closure parameter finished in
in you SPAnimation function
The reason why this doesn't work is that your created function has the closure type simply void. The static function contained in UIView has closure type ((Bool) -> Void)?
therefore you have to put the parameter in there.
Either change the closure type in your animate function in SPAnimate or remove the finished argument in your closure call..
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With