简体   繁体   English

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

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

I`m here because after weeks of trying different solutions and don't come with the right answer and functional in-app I am exhausted.我在这里是因为经过数周尝试不同的解决方案并且没有提供正确的答案和功能性的应用程序后,我已经筋疲力尽了。 I need to track the time of finger on-screen and if a finger is on screen longer than 1 sec I need to call function.我需要跟踪手指在屏幕上的时间,如果手指在屏幕上的时间超过 1 秒,我需要调用 function。 But also if now user is performing gestures like a pan or pinch function must be not called.但是,如果现在用户正在执行平移或捏合等手势,则不得调用 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
            }
        }
    }
}

Here is a small example I created for you with my suggestion of using UILongPressGestureRecognizer as it seems easier to manage for your situation than processing touchesBegin and touchesEnded这是我为您创建的一个小示例,建议您使用UILongPressGestureRecognizer ,因为它似乎比处理touchesBegintouchesEnded更容易管理您的情况

You can give it the minimum time the user needs to tap so it seems perfect for your requirement.您可以给它用户需要点击的最短时间,因此它看起来非常适合您的要求。

You can read more about it here你可以在这里阅读更多关于它的信息

First I just set up a basic UIView inside my UIViewController with this code and add a long tap gesture recognizer to it:首先,我使用以下代码在我的 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)
    }

This gives me something like this:这给了我这样的东西:

带有 UILongPressGestureRecognizer 的 UIView

Next, to get the location of the tap, the main question to ask yourself is - Where did the user tap in relation to what view?接下来,要获得点击的位置,要问自己的主要问题是 - 用户点击了哪些视图?

For example, let's say the user taps here:例如,假设用户点击这里:

UITapGestureRecognizer UIView

Now we can ask what is location of the tap in relation to现在我们可以询问水龙头的位置与

  1. The blue UIView - It is approx x = 0, y = 0蓝色 UIView - 大约 x = 0,y = 0
  2. The ViewController - It is approx x = 15, y = 30 ViewController - 大约 x = 15, y = 30
  3. The UIView - It is approx x = 15, y = 120 UIView - 大约 x = 15,y = 120

UITapGesture 位置点击 UITouch

So based on your application, you need to decide, in relation to which view do you want the touch.因此,根据您的应用程序,您需要决定您希望触摸哪个视图。

So here is how you can get the touch based on the view:因此,您可以根据视图获得触摸:

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

For same touch as above image, I get the following output printed out:对于与上图相同的触摸,我得到以下 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