TouchesMoved没有调用自定义手势识别器

问题描述 投票:1回答:1

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

但是,添加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
    }
}

}
ios swift uigesturerecognizer
1个回答
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课程。这可能也有你想要的一些功能。

© www.soinside.com 2019 - 2024. All rights reserved.