自定义 GesturesRecognizer 未调用 TouchMoved

TouchMoved not called for customized GesturesRecognizer

我是 iOS 开发的新手。我目前需要开发一个 ForceTouchGestureRecognizer,在用户开始触摸时激活 2 秒

但是,强制触摸手势的状态在添加 holdFor > minimumPressDuration

后确实过渡得很好

好像.changed一个状态无法达到

有控制台输出

以下是ForceTouchGestureRecognizer的代码class

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


2019 年 3 月 17 日更新

根据 Craz1k0ek 的回答。我认为post我的工作代码在这里

会很好
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
    }
}

}

我确实想引用 documentation:

Special Considerations Subclasses of UIGestureRecognizer must use a read-write version of the state property. They get this redeclaration when they import the UIGestureRecognizerSubclass.h header file...

我已简化您的手势识别器,让它在两秒后激活。请注意,我在您的代码中发现了一些不一致之处:minimumPressDuration 默认设置为 1000,但是,reset() 将值设置为 1500,这可能解释了一些你的行为也是如此。

class ForceTouchRecognizer: UIGestureRecognizer {

    // The minimum time the touch should take
    private let minimumTouchTime: TimeInterval = 2.0
    // The start time of the touch
    private var touchStartTime: TimeInterval?

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent) {
        // Set the start time
        touchStartTime = Date().timeIntervalSince1970
    }

    override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent) {
        // If there is no start time, how is this possible, don't do anything
        guard touchStartTime != nil else { return }

        // Check if the minimumTouchTime has passed since the start time
        if(Date().timeIntervalSince1970 - touchStartTime! > minimumTouchTime) {
            print("I should perform some action now!")
        }
    }

    override func touchesCancelled(_ touches: Set<UITouch>, with event: UIEvent) {
        // Make sure the start time is reset
        touchStartTime = nil
    }

    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent) {
        // Make sure the start time is reset
        touchStartTime = nil
    }

}

我还建议您看一下 UILongPressGestureRecognizer class。这可能还有一些您想要的功能。