Skip to content

Instantly share code, notes, and snippets.

@wata
Last active January 16, 2020 13:40
Show Gist options
  • Save wata/f50d401a89b53e81b0a2a09f92c842b8 to your computer and use it in GitHub Desktop.
Save wata/f50d401a89b53e81b0a2a09f92c842b8 to your computer and use it in GitHub Desktop.
Touches shorter than 0.1 seconds are detected as a tap, and touches longer than 0.1 seconds are canceled.
import UIKit
final class JustTapGestureRecognizer: UITapGestureRecognizer {
private var timer: Timer?
private let cancellationTimeInterval = TimeInterval(0.1)
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent) {
timer = Timer.scheduledTimer(withTimeInterval: cancellationTimeInterval, repeats: false) { [weak self] (_) in
self?.state = .cancelled
}
super.touchesBegan(touches, with: event)
}
override func reset() {
timer?.invalidate()
timer = nil
super.reset()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment