12
Touch Events and UIResponder

For the next three chapters, you are going to step away from Homepwner and build a new application named TouchTracker to learn more about touch events and gestures, as well as debugging applications.

In this chapter, you will create a view that lets the user draw lines by dragging across the view (Figure 12.1). Using multi-touch, the user will be able to draw more than one line at a time.

Figure 12.1  A drawing program

A drawing program

Touch Events

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

  • a finger or fingers touches the screen

    -​ ​(​v​o​i​d​)​t​o​u​c​h​e​s​B​e​g​a​n​:​(​N​S​S​e​t​ ​*​)​t​o​u​c​h​e​s​
     ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​w​i​t​h​E​v​e​n​t​:​(​U​I​E​v​e​n​t​ ​*​)​e​v​e​n​t​;​
    
  • a finger or fingers moves across the screen (this message is sent repeatedly as a finger moves)

    -​ ​(​v​o​i​d​)​t​o​u​c​h​e​s​M​o​v​e​d​:​(​N​S​S​e​t​ ​*​)​t​o​u​c​h​e​s​
     ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​w​i​t​h​E​v​e​n​t​:​(​U​I​E​v​e​n​t​ ​*​)​e​v​e​n​t​;​
    
  • a finger or fingers is removed from the screen

    -​ ​(​v​o​i​d​)​t​o​u​c​h​e​s​E​n​d​e​d​:​(​N​S​S​e​t​ ​*​)​t​o​u​c​h​e​s​
     ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​w​i​t​h​E​v​e​n​t​:​(​U​I​E​v​e​n​t​ ​*​)​e​v​e​n​t​;​
    
  • a system event, like an incoming phone call, interrupts a touch before it ends

    -​ ​(​v​o​i​d​)​t​o​u​c​h​e​s​C​a​n​c​e​l​l​e​d​:​(​N​S​S​e​t​ ​*​)​t​o​u​c​h​e​s​
     ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​w​i​t​h​E​v​e​n​t​:​(​U​I​E​v​e​n​t​ ​*​)​e​v​e​n​t​;​
    

When a finger touches the screen, an instance of UITouch is created. The UIView that this finger touched is sent the message touchesBegan:withEvent: and the UITouch is in the NSSet of touches.

As that 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:withEvent:. The NSSet 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 a finger is removed from the screen, the touch object is updated one last time to contain the current location of the finger, and the view that the touch began on is sent the message touchesEnded:withEvent:. After that method finishes executing, the UITouch object is destroyed.

From this information, we 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 no matter what. If the finger moves outside of the UIView’s frame that it began on, that view still receives the touchesMoved:withEvent: and touchesEnded:withEvent: messages. 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 when it changes state.

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. (If your touches are sluggish, then one of your methods is hogging the CPU, and events are waiting in line to be delivered. Chapter 14 will show you how to catch these problems.)

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 NSSet 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.

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

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