Detecting Taps with UITapGestureRecognizer

The first UIGestureRecognizer subclass you will use is UITapGestureRecognizer. When the user taps the screen twice, all of the lines on the screen will be cleared.

Open TouchTracker.xcodeproj and DrawView.swift. Add an init?(coder:) method and instantiate a UITapGestureRecognizer that requires two taps to fire and calls the action method on its target.

required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)

    let doubleTapRecognizer = UITapGestureRecognizer(target: self,
        action: #selector(DrawView.doubleTap(_:)))
    doubleTapRecognizer.numberOfTapsRequired = 2
    addGestureRecognizer(doubleTapRecognizer)
}

Now when a double-tap occurs on an instance of DrawView, the method doubleTap(_:) will be called on that instance. Implement this method in DrawView.swift.

func doubleTap(_ gestureRecognizer: UIGestureRecognizer) {
    print("Recognized a double tap")

    currentLines.removeAll()
    finishedLines.removeAll()
    setNeedsDisplay()
}

Notice that the argument to the action method for a gesture recognizer is the instance of UIGestureRecognizer that called the method. In the case of a double-tap, you do not need any information from the recognizer, but you will need information from the other recognizers you install later in the chapter.

Build and run the application, draw a few lines, and double-tap the screen to clear them.

You may have noticed (especially on the simulator) that the first tap of a double-tap results in a small red dot being drawn. This dot appears because touchesBegan(_:with:) is called on the DrawView on the first tap, creating a very short line. Check the console and you will see the following sequence of events:

touchesBegan(_:with:)
Recognized a double tap
touchesCancelled(_:with:)

Gesture recognizers work by inspecting touch events to determine whether their particular gesture has occurred. Before a gesture is recognized, the gesture recognizer intercepts all the UIResponder method calls. If it has not recognized its gesture, each call is forwarded on to the view.

Recognizing a tap requires that a touch begin and end. This means that the UITapGestureRecognizer cannot know whether the touch is a tap when touchesBegan(_:with:) is originally called, so the method is called on the view as well. When the touch ends, the tap is recognized and the gesture recognizer claims the touch for itself. It does so by calling touchesCancelled(_:with:) on the view. After that, no more UIResponder methods will be called on the view for that particular touch.

To prevent this red dot from appearing temporarily, you must prevent touchesBegan(_:with:) from being called on the view. You can tell a UIGestureRecognizer to delay calling touchesBegan(_:with:) on its view if it is still possible that its gesture might be recognized for that touch.

In DrawView.swift, modify init?(coder:) to do just this.

required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)

    let doubleTapRecognizer = UITapGestureRecognizer(target: self,
        action: #selector(DrawView.doubleTap(_:)))
    doubleTapRecognizer.numberOfTapsRequired = 2
    doubleTapRecognizer.delaysTouchesBegan = true
    addGestureRecognizer(doubleTapRecognizer)
}

Build and run the application, draw some lines, and then double-tap to clear them. You will no longer see the red dot while double-tapping.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset