如何跟踪手指在屏幕上的时间 swift

How to track time of finger on screen swift

我来这里是因为在尝试了数周不同的解决方案之后,却没有提供正确的答案和功能性的应用程序,我已经筋疲力尽了。 我需要跟踪手指在屏幕上的时间,如果手指在屏幕上的时间超过 1 秒,我需要调用函数。但如果现在用户正在执行手势,如平移或捏合功能,则不得调用。

 override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    super.touchesBegan(touches, with: event)
    
    let touch = touches.first
    guard let touchLocation = touch?.location(in: self) else {return }
    let tile: Tile?
    switch atPoint(touchLocation){
    case let targetNode as Tile:
        tile = targetNode
    case let targetNode as SKLabelNode:
        tile = targetNode.parent as? Tile
    case let targetNode as SKSpriteNode:
        tile = targetNode.parent as? Tile
    default:
        return
    }
    
    guard let tile = tile else {return }
   
    paint(tile: tile)

}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
    super.touchesEnded(touches, with: event)
    
}
private func paint(tile: Tile){
    let col =  tile.column
    let row = tile.row
    let colorPixel =  gridAT(column: col, row: row)
    
    if !tile.isTouchable {
        
    }else {
        
        if !selectedColor.isEmpty {
            
            if tile.mainColor == selectedColor {
                
                tile.background.alpha = 1
                tile.background.color = UIColor(hexString:selectedColor)
                tile.text.text = ""
                tile.backgroundStroke.color = UIColor(hexString:selectedColor)
                uniqueColorsCount[selectedColor]?.currentNumber += 1
                didPaintCorrect(uniqueColorsCount[selectedColor]?.progress ?? 0)
                colorPixel?.currentState = .filledCorrectly
                tile.isTouchable = false
            }
            else if tile.mainColor != selectedColor {
                tile.background.color = UIColor(hexString:selectedColor)
                tile.background.alpha = 0.5
                colorPixel?.currentState = .filledIncorrectly
            }
        }
    }
}

这是我根据使用 UILongPressGestureRecognizer 的建议为您创建的一个小示例,因为它似乎比处理 touchesBegintouchesEnded[=20= 更容易管理您的情况]

你可以给它一个用户需要点击的最短时间,这样它看起来很适合你的要求。

你可以read more about it here

首先,我只是在我的 UIViewController 中使用以下代码设置了一个基本的 UIView,并向其中添加了一个长按手势识别器:

    override func viewDidLoad() {
        super.viewDidLoad()
        
        // Create a basic UIView
        longTapView = UIView(frame: CGRect(x: 15, y: 30, width: 300, height: 300))
        longTapView.backgroundColor = .blue
        view.addSubview(longTapView)
        
        // Initialize UILongPressGestureRecognizer
        let longTapGestureRecognizer = UILongPressGestureRecognizer(target: self,
                                                                    action: #selector(self.handleLongTap(_:)))
        
        // Configure gesture recognizer to trigger action after 2 seconds
        longTapGestureRecognizer.minimumPressDuration = 2
        
        // Add gesture recognizer to the view created above
        view.addGestureRecognizer(longTapGestureRecognizer)
    }

这给了我这样的东西:

接下来,要获得点击的位置,您要问自己的主要问题是 - 用户在哪个视图中点击了哪些位置?

例如,假设用户点击此处:

现在我们可以问水龙头相对于

的位置是什么
  1. 蓝色 UIView - 它大约为 x = 0,y = 0
  2. ViewController - 大约是 x = 15, y = 30
  3. UIView - 它大约是 x = 15,y = 120

所以根据您的应用程序,您需要决定您想要触摸哪个视图。

下面是如何根据视图获得触摸:

    @objc
    private func handleLongTap(_ sender: UITapGestureRecognizer)
    {
        let tapLocationInLongTapView = sender.location(in: longTapView)
        let tapLocationInViewController = sender.location(in: view)
        let tapLocationInWindow = sender.location(in: view.window)
        
        print("Tap point in blue view: \(tapLocationInLongTapView)")
        print("Tap point in view controller: \(tapLocationInViewController)")
        print("Tap point in window: \(tapLocationInWindow)")
        
        // do your work and function here
    }

对于与上图相同的触摸,我打印出以下输出:

Tap point in blue view: (6.5, 4.5)
Tap point in view controller: (21.5, 34.5)
Tap point in window: (21.5, 98.5)