gpt4 book ai didi

ios - 自定义手势识别器未调用 TouchMove

转载 作者:行者123 更新时间:2023-11-28 07:30:06 25 4
gpt4 key购买 nike

我是 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
}
}

}

最佳答案

我确实想引用 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)。这也可能有一些您想要的功能。

关于ios - 自定义手势识别器未调用 TouchMove,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55117843/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com