Search code examples
swifttouchtouchesbegan

Timer for touchesBegan function


I want to have an app that detects when your finger touches. If the app detects that the screen is tapped I want to place an UIImage on that place. This app needs to have multiple users. I have this so far:

var users = 0
@IBOutlet weak var Person_1: UIImageView!
@IBOutlet weak var Person_2: UIImageView!
@IBOutlet weak var Person_3: UIImageView!
@IBOutlet weak var Person_4: UIImageView!
@IBOutlet weak var Person_5: UIImageView!
var fingers = [String?](repeating: nil, count:5)

   override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        super.touchesBegan(touches, with: event)
        for touch in touches{
            let point = touch.location(in: self.view)
            for (index,finger)  in fingers.enumerated() {
                if finger == nil {
                    fingers[index] = String(format: "%p", touch)
                    print("finger \(index+1): x= \(point.x) , y= \(point.y)")
                    if index == 0 {
                        Person_1.center = CGPoint(x: point.x , y: point.y)
                        Person_1.isHidden = false
                        users = 1
                    }
                    if index == 1 {
                        Person_2.center = CGPoint(x: point.x , y: point.y)
                        Person_2.isHidden = false
                        users = 2
                    }
                    if index == 2 {
                        Person_3.center = CGPoint(x: point.x , y: point.y)
                        Person_3.isHidden = false
                        users = 3
                    }
                    if index == 3 {
                        Person_4.center = CGPoint(x: point.x , y: point.y)
                        Person_4.isHidden = false
                        users = 4
                    }
                    if index == 4 {
                        Person_5.center = CGPoint(x: point.x , y: point.y)
                        Person_5.isHidden = false
                        users = 5
                    }
                    print("Users: \(users)")
                    break
                }
            }
        }
        check_users()
    }
    func check_users(){
        if users == 5 {
            let timer = Timer.scheduledTimer(timeInterval: 5.0, target: self, selector: #selector(self.choosewinner4), userInfo: nil, repeats: false)
        }
        if users == 4 {
            let timer = Timer.scheduledTimer(timeInterval: 5.0, target: self, selector: #selector(self.choosewinner3), userInfo: nil, repeats: false)
        }
        if users == 3 {
            let timer = Timer.scheduledTimer(timeInterval: 5.0, target: self, selector: #selector(self.choosewinner2), userInfo: nil, repeats: false)
        }
        if users == 2 {
            let timer = Timer.scheduledTimer(timeInterval: 5.0, target: self, selector: #selector(self.choosewinner1), userInfo: nil, repeats: false)
        }
    }

@objc func choosewinner1(){
        let diceRoll1 = Int(arc4random_uniform(2) + 1)
        print(diceRoll1)
        if diceRoll1 == 1 {
            Person_1.isHidden = true
        }
        if diceRoll1 == 2{
            Person_2.isHidden = true
        }
    }

Everytime when a new user touches the screen the users Int will count 1 user on the users Int. I have a function that will check what it needs to do when you have x amount of users, called "check_users()". The problem of this code is that, if you touch the screen with 2 people, the users Int will be 2. If you touch the screen with 3 people, the users Int will be first 2 and go trough the check_users() function and then go through check_users() function with the users Int = 3. I want the app to wait a few seconds before it goes through the users_check() function with the final users Int value. Does anybody knows how to do this?


Solution

  • You can use methods for performing a selector with delay. To call a method after a delay use:

    perform(#selector(check_users), with: nil, afterDelay: 1)
    

    To cancel any previous requests, that has not been fulfilled:

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        super.touchesBegan(touches, with: event)
        NSObject.cancelPreviousPerformRequests(withTarget: self)
        ...
    }