nRewik
nRewik

Reputation: 9148

Prevent simultaneously tapping on views

I have many views, and I want the behaviour that when I tap at the first view, other views interaction will be disabled until the task finish.

Now, my code looks like this.

private var lock = false
@IBAction func firstViewTapped(sender: UITapGestureRecognizer) {
    if lock{
        return
    }
    lock = true
    doSomeTask{ error in
        println("finish 1!!")
        self.lock = false
    }
}
@IBAction func secondViewTapped(sender: UITapGestureRecognizer) {
    if lock{
        return
    }
    lock = true
    doSomeTask{ error in
        println("finish 2!!")
        self.lock = false
    }
}

I am wondering. Is there another elegant way to do this ?

Upvotes: 0

Views: 48

Answers (1)

sahara108
sahara108

Reputation: 2859

You can use self.view.userInteractionEnabled = false and self.view.userInteractionEnabled = true to lock and unlock it. Be careful you app may be unresponsive if you don't unlock the view properly.

Upvotes: 3

Related Questions