不要求使用TouchMoved自定义手势识别器

时间:2019-03-12 09:18:09

标签: ios swift uigesturerecognizer

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

但是,在添加holdFor > minimumPressDuration

之后,强制触摸手势的状态会很好地过渡

似乎.changed的状态无法达到

有控制台输出

enter image description here

以下是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


于2019年3月17日更新

根据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
    }
}

}

1 个答案:

答案 0 :(得分:0)

我确实想引用documentation

  

UIGestureRecognizer的特殊注意事项子类必须使用   state属性的读写版本。他们得到了这个重新声明   当他们导入UIGestureRecognizerSubclass.h头文件时...

我简化了手势识别器,使其在两秒钟后激活。请注意,我在您的代码中看到了一些不一致之处: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类。这可能还需要一些功能。