Forked from myell0w/LongPressGestureRecognizer.swift
Last active
November 13, 2017 14:55
-
-
Save tamassengel/d0f1079698e9d26cc63f714e88b9fc0b to your computer and use it in GitHub Desktop.
A UIGestureRecognizer that fires either on long-press or on a force-touch (3D Touch ™️)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit.UIGestureRecognizerSubclass | |
final class MNTLongPressGestureRecognizer: UILongPressGestureRecognizer { | |
var triggerWithForceTouch = true | |
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent) { | |
super.touchesBegan(touches, with: event) | |
handleTouches(touches, with: event) | |
} | |
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent) { | |
super.touchesMoved(touches, with: event) | |
handleTouches(touches, with: event) | |
} | |
fileprivate func handleTouches(_ touches: Set<UITouch>, with event: UIEvent) { | |
guard triggerWithForceTouch, | |
state == .possible, | |
let traitCollection = view?.traitCollection, | |
traitCollection.forceTouchCapability == .available, | |
touches.count == 1 else { | |
return | |
} | |
let touch = touches.first! | |
let force = touch.force / touch.maximumPossibleForce | |
if force >= 1.0 { | |
state = .began | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment