Я новичок в разработке для iOS.В настоящее время мне нужно разработать ForceTouchGestureRecognizer с 2-секундной активацией, когда пользователь начинает касаться
Но состояние для жеста принудительного касания проходит очень хорошо после добавления holdFor > minimumPressDuration
Кажется, что .changed
состояние не может быть достигнуто
Имеется консольный вывод
Ниже приведен код для класса ForceTouchGestureRecognizer
import UIKit
import UIKit.UIGestureRecognizerSubclass
class ForceTouchGestureRecognizer: UIGestureRecognizer {
var minimumValue: CGFloat = 0 // Value between 0.0 - 1.0 that needs to be reached before gesture begins
var tolerance: CGFloat = 1 // Once force drops below maxValue - tolerance, the gesture ends
var minimumPressDuration: Int = 1000
private(set) var forceValue: CGFloat? // value between 0.0 - 1.0
private var maxValue: CGFloat = 0
private var touchStartTime: Int = 0
override func reset() {
super.reset()
forceValue = nil
maxValue = 0
minimumPressDuration = 1500
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent) {
super.touchesBegan(touches, with: event)
if #available(iOS 9.0, *) {
if touches.count != 1 {
state = .failed
}
touchStartTime = Int(NSDate().timeIntervalSince1970 * 1000)
}
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent) {
super.touchesMoved(touches, with: event)
if #available(iOS 9.0, *) {
let touch = touches.first!
let value = touch.force / touch.maximumPossibleForce
let holdFor = Int(NSDate().timeIntervalSince1970 * 1000) - touchStartTime
if state == .possible {
if value > minimumValue && holdFor > minimumPressDuration {
self.state = .began
}
} else {
if value < (maxValue - tolerance) {
state = .ended
} else {
maxValue = max(self.forceValue ?? 0, maxValue)
self.forceValue = value
state = .changed
}
}
}
}
override func touchesCancelled(_ touches: Set<UITouch>, with event: UIEvent) {
super.touchesCancelled(touches, with: event)
if state == .began || state == .changed {
state = .cancelled
}
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent) {
super.touchesEnded(touches, with: event)
if state == .began || state == .changed {
state = .ended
}
}
}
Источник: https://github.com/ashleymills/ForceTouchGestureRecognizer.swift/blob/master/ForceTouchGestureRecognizer.swift
Обновлено 17 марта 2019
По словам автора Craz1k0ek.Я думаю, что будет хорошо опубликовать мой работающий код здесь
import UIKit.UIGestureRecognizerSubclass
class ForceTouchGestureRecognizer: UIGestureRecognizer {
var minimumValue: CGFloat = 0 // Value between 0.0 - 1.0 that needs to be reached before gesture begins
var tolerance: CGFloat = 1 // Once force drops below maxValue - tolerance, the gesture ends
var minimumPressDuration: TimeInterval = 1.5
private(set) var forceValue: CGFloat? // value between 0.0 - 1.0
private var maxValue: CGFloat = 0
private var touchStartTime: TimeInterval?
override func reset() {
super.reset()
forceValue = nil
maxValue = 0
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent) {
super.touchesBegan(touches, with: event)
if #available(iOS 9.0, *) {
if touches.count != 1 {
state = .failed
}
touchStartTime = Date().timeIntervalSince1970
}
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent) {
guard touchStartTime != nil else { return }
super.touchesMoved(touches, with: event)
if #available(iOS 9.0, *) {
let touch = touches.first!
let value = touch.force / touch.maximumPossibleForce
let holdFor = NSDate().timeIntervalSince1970 - touchStartTime!
if holdFor > minimumPressDuration {
if state == .possible {
if value > minimumValue {
self.state = .began
}
} else {
if value < (maxValue - tolerance) {
state = .ended
} else {
maxValue = max(self.forceValue ?? 0, maxValue)
self.forceValue = value
state = .changed
}
}
}
}
}
override func touchesCancelled(_ touches: Set<UITouch>, with event: UIEvent) {
super.touchesCancelled(touches, with: event)
if state == .began || state == .changed {
state = .cancelled
}
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent) {
super.touchesEnded(touches, with: event)
if state == .began || state == .changed {
state = .ended
}
}
}