20
Touch Events and UIResponder

In your Hypnosister project from Chapter 6, you created a UIScrollView that handled touch events to scroll a view and even handled a multi-touch event to zoom. The UIScrollView class makes scrolling and zooming easy to implement. But what if you want to do something else, something special or unique, with touch events?

In this chapter, you are going to create a view that lets the user draw lines by dragging across the view (Figure 20.1). Using multi-touch, the user will be able to draw more than one line at a time. Double-tapping will clear the screen and allow the user to begin again.

Figure 20.1  A drawing program

A drawing program

Touch Events

As a subclass of UIResponder, your view 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 move 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 touch event occurs, that 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. (If your touches are sluggish, then one of your methods is hogging the CPU, and events are waiting in line to be delivered. Chapter 21 will show you how to catch these problems.)

Delivering a touch event means sending one of the UIResponder messages to the view the event occurred on. In these methods, there is always a UIEvent instance you have access to. The UIEvent can tell you the type of event and when this event took place. It can also tell you all of the active touches in an application. For touch events, however, you typically do not use the event object; you use UITouch objects instead.

When the user touches the screen, an instance of UITouch is created and associated with that finger. The UITouch knows where that finger is on the screen. As that finger moves, the same UITouch object is updated so that it always holds the current position of that finger on the screen. When the finger leaves the screen, the UITouch is discarded. (UITouch instances also keep track of things like the previous location of the finger and how many times this finger tapped the screen.)

As these touch events occur, the appropriate UIResponder messages are sent to the touched view, and the UITouch objects involved with those events are passed as arguments. Because more than one finger can trigger the same event at the same time, the argument passed to the view is a set of those touches. For example, if two fingers touch a view at the same time, the view will be sent touchesBegan:withEvent: and the first argument will be an NSSet that contains two UITouch instances. However, if two fingers touch the same view one after the other, you will get two separate touchesBegan:withEvent: messages, and each NSSet will contain one UITouch.

When a touch moves or ends, touchesMoved:withEvent: or touchesEnded:withEvent: will be sent to the view that the touch originated on. Thus, once a touch begins its life, it is tied to the view that it first touched. Also, the NSSet that is passed to these methods only contains the touches that triggered the event. For example, if there are two touches on the view but only one of them moved, only the moving touch will be in the set delivered via touchesMoved:withEvent:.

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

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