nastassia
nastassia

Reputation: 897

swift - how to prevent two touches at the same time

For example, I have 2 buttons Change email and Change password, and each of them call functions with Alamofire request, and responce data should reload both the UI and data scheme.

The point is that this PUT requests change not only servers's data, but generate new token and get updated user's profile.

And when pressing buttons at the same time, at the same moment touches begin and end, app crash after parsing requests.

I'm blocking another UI elements(like textfields), I was trying to block another button, but when press it together, it's not works.

So how can I prevent the same time touch? I'm not good at OperationQueue, maybe thats'the way? Is there an option to check if operation not first at the queue and kill it?

Upvotes: 0

Views: 1109

Answers (2)

Marwen Doukh
Marwen Doukh

Reputation: 2050

Set isExclusiveTouch of your UIButton to true in order to trigger only one button action in a specific time.

This code will get all the buttons contained in the view and set the exclusiveTouch to true:

  self.view.subviewsRecursive()
        .filter { $0 is UIButton }
        .forEach { $0.isExclusiveTouch = true }

Upvotes: 4

Peeyush karnwal
Peeyush karnwal

Reputation: 642

This problem with the UIResponder object is very usual. However, your problem description is not clear and your implementation seems not so good. Here, to resolve this quick touch event problem: Your solution is debouncing the action event of UIButton. Debouncing also helps to prevent multiple executions when a user mistakenly pressed a button (or any UIResponder object) multiple times so quickly that even the UI was not blocked till then. Following article may guide you more regarding the same: Debouncing to tackle repeating user action

Upvotes: 0

Related Questions