Poll
Are you planning on developing on the iPhone SDK?
Yes 24
Maybe 13
No 2
Gimme an iPhone!!! 14
Total Votes: 53
You must be a logged-in member to vote
CocoaTouch : UITouch Introduction (iPhone SDK)
Posted: 10 March 2008 07:23 PM   [ Ignore ]
Avatar
Rank
Joined  2008-03-10
Total Posts:  5
New Member


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 a phase property, which represents these actions (began, moved,stationary, ended). It also has a location property, a current view and window property, and a unique timestamp 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 method touchesChangedWithEvent:. Your method is invoked when touch events occur.

If you implement the method touchesChanged, you receive notification when any touch within an event changes phase. 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 the UITouch object that tracks a corresponding touch will remain the same for the life of this touch; that is, you can use UITouch instances as keys in dictionaries.

If you choose to implement touchesBegan:withEvent:, touchesMoved:withEvent:, or touchesEnded:withEvent:, your event handler receives a set of UITouch touches for convenience, in addition to the event itself. Sets are not ordered, so even though you implement a touchesBegan: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 for touchesChangedWithEvent:) 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):

The phase 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…

File Attachments
UITouch.zip  (File Size: 41KB - Downloads: 2439)
SampleCode.zip  (File Size: 31KB - Downloads: 2807)
UITouch_Documentation.pdf  (File Size: 196KB - Downloads: 7719)
 Signature 

~

Profile
 
 
Posted: 11 March 2008 08:26 PM   [ Ignore ]   [ # 1 ]
Rank
Joined  2007-10-15
Total Posts:  24
New Member

Thanks nuiman (?)

I wanted to look into this stuff.

Looks like they don’t give you access to too much granularity of touch information. (like pressure / area ?) But enough to build some really neat applications / games with.
My notes /questions on the API :

1. Phases can tell you whether or not it’s a swipe, if so it’s direction.
2. Event = number of phrases together ?
3. Tap count ? How much of a time delay between taps does it allow for them to be in the same event ?

There should be a couple of people here who have worked (are working) on the iphone by now :p

Let us know what you’ve done !

Profile
 
 
Posted: 12 March 2008 07:37 AM   [ Ignore ]   [ # 2 ]
Avatar
RankRank
Joined  2007-04-03
Total Posts:  241
Moderator

hmm i need an iphone :(

 Signature 

My multitouch blog: http://www.multigesture.net
Howto: Compile touchlib on windows XP/Vista
Howto: Compile touchlib on Ubuntu Linux
Downloads: Touchlib SVN builds

Profile
 
 
Posted: 12 March 2008 10:08 AM   [ Ignore ]   [ # 3 ]
Avatar
RankRank
Joined  2007-05-05
Total Posts:  191
Member

hhm i need to lear objective-c, or even c at all. have an iphone at least smile

 Signature 

Blog: http://iad.projects.zhdk.ch/multitouch/
180 Project: http://www.timroth.de/180/

Profile
 
 
Posted: 13 March 2008 01:34 PM   [ Ignore ]   [ # 4 ]
RankRank
Joined  2007-03-23
Total Posts:  216
Member

Would you be able to use cocoaTouch SDK to develop applications for Ipod Touch?

Profile
 
 
Posted: 13 March 2008 09:28 PM   [ Ignore ]   [ # 5 ]
Avatar
RankRankRankRank
Joined  2006-11-09
Total Posts:  1499
Administrator

Yes nima the iPod Touch supports the SDK. I am going to buy one tonight I think smile

I wonder if they use same hardware.. processor/memory etc… anyone know hardware spec?

 Signature 

~

Profile
 
 
Posted: 17 March 2008 06:12 PM   [ Ignore ]   [ # 6 ]
Rank
Joined  2007-10-15
Total Posts:  24
New Member

Another important question i want to figure out, the web doesn’t seem to help…

What are the maximum number of fingers the iphone can detect (the max that can fit on the screen ?)

Profile
 
 
Posted: 17 March 2008 11:37 PM   [ Ignore ]   [ # 7 ]
Avatar
RankRankRankRank
Joined  2006-11-09
Total Posts:  1499
Administrator

I have personally seen it support 5…

 Signature 

~

Profile
 
 
Posted: 04 September 2008 03:36 PM   [ Ignore ]   [ # 8 ]
Avatar
Rank
Joined  2008-07-01
Total Posts:  54
New Member

this is really cool i just dont have a mac to try it on i wish this sdk was for windows too… lol i guess it would also help if i had an iphone…

 Signature 

“Good ideas are only bad ideas that work!”

Profile