I am using performSelector
to control the execution time of some methods in my controller. I would expect in this case the argument passed to animateIntroViews
to be the value of with
however it is not, instead it is some massively negative number like -7878379202283
func runOnRenderAnimations() -> Void {
perform(#selector(animateIntroViews), with: 0, afterDelay: 0.5)
perform(#selector(animateIntroViews), with: 1, afterDelay: 2.5)
}
@objc fileprivate func animateIntroViews(textPosition: Int) -> Void {
print(textPosition)
}
perform
doesn't work with parameters of primitive types like Int
.
A better solution is to use DispatchQueue asyncAfter
. This allows you to call the method normally.
func runOnRenderAnimations() -> Void {
DispatchQueue.main.asyncAfter(deadline: now() + 0.5) {
self.animateIntroViews(textPosition: 0)
}
DispatchQueue.main.asyncAfter(deadline: now() + 2.5) {
self.animateIntroViews(textPosition: 1)
}
}
With your perform
selector, you can pass argument as NSNumber
instead of Int
and get it as Number
and Convert it to Int
in your animateIntroViews
function.
Example:
func runOnRenderAnimations() -> Void {
perform(#selector(animateIntroViews), with: NSNumber(value: 0), afterDelay: 0.5)
perform(#selector(animateIntroViews), with: NSNumber(value: 1), afterDelay: 2.5)
}
@objc fileprivate func animateIntroViews(textPosition: NSNumber) -> Void {
print(textPosition.intValue)
}
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