繁体   English   中英

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

[英]How to track time of finger on screen swift

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

 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更容易管理您的情况

您可以给它用户需要点击的最短时间,因此它看起来非常适合您的要求。

你可以在这里阅读更多关于它的信息

首先,我使用以下代码在我的 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)
    }

这给了我这样的东西:

带有 UILongPressGestureRecognizer 的 UIView

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

例如,假设用户点击这里:

UITapGestureRecognizer UIView

现在我们可以询问水龙头的位置与

  1. 蓝色 UIView - 大约 x = 0,y = 0
  2. ViewController - 大约 x = 15, y = 30
  3. UIView - 大约 x = 15,y = 120

UITapGesture 位置点击 UITouch

因此,根据您的应用程序,您需要决定您希望触摸哪个视图。

因此,您可以根据视图获得触摸:

    @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
    }

对于与上图相同的触摸,我得到以下 output 打印出来:

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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM