If you have not read already, the long-awaited iPhone SDK has been released…
So say hello to CocoaTouch from Apple Inc. TM & double copyrighted.. patented 10x… and all that good fun stuff.. :X There are already some doubts on its limited deployment routes (thought iTunes “App Store") as well as the ”$99 USD membership fee” to use the SDK on your own hardware devices, however obviously those things will change rather quickly. It will work on both iPhones and iPod Touch hardware devices and will be fully deployed (to every legit iphone) via Apple Updates in “late June”. (Available in iPhone OS 2.0 and later)
Now to move onto the software implementation of their touch event system, which is written in Objective-C (OSX’s primary development language aka Cocoa)
A-1 : Touch Event Introduction:
Each event is made up of a collection of one or more touches. You need to distinguish between the event (
UIEvent
) and its constituent touches (UITouch
). A touch begins when a finger makes contact with the screen, persists as the finger lingers or moves, and ends when the finger lifts from the screen. The touch has aphase
property, which represents these actions (began
,moved
,stationary
,ended
). It also has alocation
property, a currentview
andwindow
property, and a uniquetimestamp
property, which you use both as an identifier and to determine the touche’s current duration.Only one event takes place at a time. If another finger touches the screen before an event ends, that touch is added to the event. When the last finger lifts from the screen, and all the touches in the event have ended, the event ends.
To receive touch input, you need to instantiate a
UIView
and add an instance method that handles touch events. In most cases, you should implement the methodtouchesChangedWithEvent:
. Your method is invoked when touch events occur.If you implement the method
touchesChanged
, you receive notification when any touch within an event changesphase
. Access the set of touches belonging to the event, and then access the properties of the touches. Sets are not ordered, so you must iterate though the set to determine the properties of each touch. Note, however, that theUITouch
object that tracks a corresponding touch will remain the same for the life of this touch; that is, you can useUITouch
instances as keys in dictionaries.If you choose to implement
touchesBegan:withEvent:
,touchesMoved:withEvent:
, ortouchesEnded:withEvent:
, your event handler receives a set ofUITouch
touches for convenience, in addition to the event itself. Sets are not ordered, so even though you implement atouchesBegan:withEvent:
handler, for example, the touch that triggered the event may be any member of the set. You need to iterate though the set (just as you do fortouchesChangedWithEvent:
) to determine the properties of each touch.
A-2 : Touch Event Properties:
A UITouch object represents the presence or movement of a finger on the screen for a particular event. You access UITouch objects through UIEvent objects passed into responder objects for event handling. A UITouch object includes methods for accessing the view or window in which the touch occurred and for obtaining the location of the touch in a specific viewor window. it also lets you find out when the touch occurred, whether the user tapped more than once, whether the finger is swiped (and if so, in which direction), and the phase of a touch—that is, whether it began, moved, or ended the gesture, or whether it was canceled. See “Event Handling” in iPhone OS Programming Guide for further information on event handling.
info - The constants indicating whether the touch is a swipe and, if so, its direction.
locationInView - The location of the touch in the view in which it occurred.
previousLocationInView - The previous location of the touch in the view in which it occurred.
tapCount - The number of times the finger was tapped for this given touch.
timestamp - The time when the touch occurred or when it was last mutated.
view - The view in which the touch initially occurred.
window - The window in which the touch initially occurred.
phase - The type of touch. (See Below A-3)
A-3 : Touch Event (phase):
Thephase
property tells you whether a particular touch event began, moved, or ended. See the UITouch Class Reference for further information. You can choose to have separate handlers for different touch phases, such as the when touches begin, move, end, change with the event, or are canceled by the system. You need to implement handlers only for the phases you are interested in. See below for the available set of handlers A-4a.
touchesBegan - A finger for a given event touched the screen.
touchesMoved - A finger for a given event moved on the screen.
touchesEnded - A finger for a given event was lifted from the screen.
touchesChangedWithEvent - ???
touchedStationary - A finger is touching the surface but hasn’t moved since the previous event.
touchedCancelled - The system canceled tracking for the touch.
A-4a : Touch Event Handlers:
(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;
(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;
(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event;
(void)touchesChangedWithEvent:(UIEvent *)event;
(void)touchedCancelled;
A-4b : Handling Touch Events :
(void)touchesChangedWithEvent:(UIEvent*)event
{
NSSet *touches = [event allTouches];
for (UITouch *myTouch in touches)
{
if (myTouch.phase == UITouchPhaseBegan) {
// new touch handler
}
if (myTouch.phase == UITouchPhaseMoved) {
// touch moved handler
}
if (myTouch.phase == UITouchPhaseEnded) {
// touch end handler
}
CGPoint touchLocation = [myTouch locationInView];
// do something with location
NSTimeInterval *myTouchID = [myTouch timestamp ];
// subtract from current time to obtain duration
// store in a array if tracking touches by ID
}
}
A-5 : Additional Resources:
You can learn more and get the free SDK here: http://developer.apple.com/iphone/
For additional sample code and documentation, see the attached zip files…