18
Touch Events and UIResponder

In the next two chapters, you will create TouchTracker, an app that lets the user draw by touching the screen. In this chapter, you will create a view that draws lines in response to the user dragging across it (Figure 18.1). Using multitouch, the user will be able to draw more than one line at a time.

Figure 18.1  TouchTracker

Screenshot shows a detail view controller that has a drawing of an iPhone device at the center.

Touch Events

As a subclass of UIResponder, a UIView can override four methods to handle the four distinct touch events:

  • one or more fingers touch the screen

    func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?)
    
  • one or more fingers move across the screen (this message is sent repeatedly as a finger moves)

    func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?)
    
  • one or more fingers are removed from the screen

    func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?)
    
  • a system event, like an incoming phone call, interrupts a touch before it ends

    func touchesCancelled(_ touches: Set<UITouch>, with event: UIEvent?)
    

Let’s walk through the typical lifecycle of a touch. When the user’s finger touches the screen, an instance of UITouch is created. The touchesBegan(_:with:) method is called on the UIView that the finger touched, and the UITouch is passed in through the Set of touches.

As the finger moves around the screen, the touch object is updated to contain the current location of the finger on the screen. Then, the same UIView that the touch began on is sent the message touchesMoved(_:with:). The Set that is passed as an argument to this method contains the same UITouch that originally was created when the finger it represents touched the screen.

When the finger is removed from the screen, the touch object is updated one last time to contain the final location of the finger, and the view that the touch began on is sent the message touchesEnded(_:with:). After that method finishes executing, the UITouch object is destroyed.

From this information, you can draw a few conclusions about how touch objects work:

  • One UITouch corresponds to one finger on the screen. This touch object lives as long as the finger is on the screen and always contains the current position of the finger on the screen.

  • The view that the finger started on will receive every touch event message for that finger. Even if the finger moves beyond the frame of the UIView that the touch began on, the touchesMoved(_:with:) and touchesEnded(_:with:) methods will still be called on that view. Thus, if a touch begins on a view, then that view owns the touch for the life of the touch.

  • You do not have to – nor should you ever – keep a reference to a UITouch object. The application will give you access to a touch object via the UIResponder methods called at the distinct points in the touch’s lifecycle.

Every time a touch does something – like begins, moves, or ends – a touch event is added to a queue of events that the UIApplication object manages. In practice, the queue rarely fills up, and events are delivered immediately. The delivery of these touch events involves sending one of the UIResponder messages to the view that owns the touch.

What about multiple touches? If multiple fingers do the same thing at the exact same time to the same view, all of these touch events are delivered at once. Each touch object – one for each finger – is included in the Set passed as an argument in the UIResponder messages. However, the window of opportunity for the exact same time is fairly short. So, instead of one responder message with all of the touches, there are usually multiple responder messages with one or more of the touches. You will see how to handle multiple touches later in this chapter.

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

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