I'm trying to present a viewcontroller modally over current context when I press a button ("Touch Down") and when I release that button ("Touch Up Inside") I want the viewcontroller to be dismissed.
This is my code: (buttonPressed is the "Touch Down" event and buttonReleased is the "Touch Up Inside" event)
@IBAction func buttonPressed(_ sender: AnyObject) {
let storyboard = UIStoryboard.init(name: "Main", bundle: .main)
let anotherView = storyboard.instantiateViewController(withIdentifier: "AnotherView")
anotherView.modalPresentationStyle = UIModalPresentationStyle.overCurrentContext
anotherView.modalTransitionStyle = UIModalTransitionStyle.crossDissolve
present(anotherView, animated: true, completion: nil)
}
@IBAction func buttonReleased(_ sender: AnyObject) {
dismiss(animated: true, completion: nil)
}
But if I spam the button anotherView gets stuck and leaves me there. Does anyone know a solution to this problem? Thank you.
EDIT: also, if I change the animation to false this still happens so it's probably not the issue.
EDIT 2 I solved the problem. I was simply listening to the wrong event from the button. If you press and release the button really quickly it seems that the event that gets fired is "Touch Cancel". I added that as well and my code is now working as it should.