I don't have an IPhone and don't really want to pay $130 a month for a cell phone. (I leave mine in the car most of the time, sometimes the entire weekend.) But I covet the technology as a mobile computing platform. (Cruel Fate.) One of the things I like about it the most is the multitouch capability.
How does it look from an API standpoint?
Does the OS have "Gestures" that it knows and passes on an event based on what the user did, or is the application required to interpret a list of "touch and release" events?
How many points can it read? 2, 3... unlimited?
Does Mac OS X proper have this capability if you have a "Multitouch" capabible monitor?
You can always take a look at the documentation to get a better idea of what is supported, but the general methods to implement are:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event;
When you tap down, touchesBegan is called with the set of touches that just began. To retrieve all touches currently on the screen, retrieve event.allTouches.
Similarily obvious actions occur when a finger moves or is removed from the screen. The touchesCancelled method is mostly used to support the UIScrollView which allows you to tap something inside a scroll view, then drag the scroll view itself rather that interact with the subview, if certain criteria are met (the scroll view would send a touches cancelled message to the subview when it starts scrolling).
There are no built-in gestures you can watch for to speak of, but there are built-in gestures that the system handles, like swiping across a row in a table to delete it, and pinch-zooming on a UIScrollView.
Related
I am working on a semi-piano app with a diffrent keyboard-layout then a usual one.
I created the view manually with UIButtons,
My problem was I that I didn't know how to slide from a UIButton to another,
I figured that out with addTarget with the option of withEvent, which gave me the access to the touches.
Now, after I added the target like this:
[C addTarget:self action:#selector(outsideOfKey: forEvent:) forControlEvents:UIControlEventTouchDragOutside|UIControlEventTouchDragInside];
[C addTarget:self action:#selector(keyGetsLeft: forEvent:) forControlEvents:UIControlEventTouchUpOutside | UIControlEventTouchUpInside];
(also for all of the other keys),
I maneged to make them slideable,
outsideOfKey:forEvent: is as follows:
-(void) outsideOfKey:(id)sender forEvent:(UIEvent *)event
{
for(UITouch *t in [event allTouches])
{
CGPoint touchPoint = [t locationInView:window];
if(CGRectContainsPoint(C.frame, touchPoint))
{
C.highlighted = YES;
}
else{
C.highlighted = NO;
}
(Done for all the other keys as well)
I can slide from and into other keys, and when I leave them in keyGetsLeft:forEvent: I have just used the same syntx without the else, and the highlighted became NO.
Up to here it's easy,
But then when I try to do multi-touch, I can slide only one of the touches all around and the others must stay in the same position.
And even more, If I take one of the fingers away all of them are becoming non-highlighted,
I know the reasons to all of that, but I don't know how to fix it and make it to work.
I would probably move away from UIButtons altogether and implement my own custom touch tracking code. See Handling Multitouch Events in the docs. Namely, you will be implementing the following methods:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
I would probably just implement hit areas, which you could setup in IB, with perhaps a custom UIView touch overlay (just a UIView with a custom subclass). This would let you setup the view in IB with images, titles, etc., but do all of your touch tracking in your custom subclass.
I am afraid, bensnider is right. But I'd implement it via GestureRecognizer:
https://developer.apple.com/documentation/uikit/touches_presses_and_gestures/handling_uikit_gestures
https://developer.apple.com/documentation/uikit/uigesturerecognizer
Each key has one TapRecognizer, while a parent view has a SwipeRecognizer to detect slides form one key to another.
Very useful, on Apple Developer Video Archivelogin with development account required:
WWDC2010: Session 120 — Simplifying Touch Event Handling with Gesture Recognizers
WWDC2010: Session 121 — Advanced Gesture Recognition
I'd use one view for all buttons and manually implement touch tracking as bensnider said. Manually drawing backgrounds/titles also is not so difficult.
Heres a link to an open source iOS piano app I made https://github.com/meech-ward/iOSPiano
I used CALayers for the piano keys and I used
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;
to detect if the user is currently touching a piano key.
It works really well even with multiple touches.
What is the coding to detect a "quick touch" on a UITableView for the purpose of toggling to/from full screen?
In particular this is because the user is on a UITableView they would still need the ability to drag the list of items up/down, and potentially click on a cell/row to dig deeper. But if they touched quickly then this could be the trigger to toggle between full-screen mode (i.e. nav bar & tool bar).
Background - When I say toggling to/from full screen I'm refering to carrying out what is described here, however in this answer there was no mention in terms of how to plug this into the callbacks for a UITableView which is being displayed within a UINavigationController stack.
The following code might help you
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
timeStampStart = event.timestamp;
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
timeStampEnd = event.timestamp;
touchDuration = timeStampEnd - timeStampStart;
if(touchDuration > smallTimeStamp)
[super touchesEnded:touches withEvent:event];
else
[self zoomMyTableView];
}
Wanted to put up this as a possible answer, the answer being there is no relative straight way to achieve what I've asked for this. That is a way for a tap or double-tap to be detected in a UITableView page, which is already picking up row touches and scroll up/down etc.
Haven't verified whether this is the case or not, but people could up-vote this answer if they believe this to be true. (also waiting on sample from ypk who perhaps has an answer)
Hi i am new iphone i want to developing a application that is work on touch event. in this when user touch on screen then show a label msg u touch on right and same.
how to do this i im confused plz tell me.
Basically What you will want to look at is subclassing UIView objects (which themselvs inherit from UIResponder), inside your subclass you can implement the following methods and add the required behavior to them:
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event:
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event;
-(void)touchesCanceled:(NSSet *)touches withEvent:(UIEvent *)event;
For more information I would look at the UIResponder.h file and the related documentation in xCode.
Another great place to continue your research and read some sample code is the Event Handling section in the iPhone Application Programming Guide
I dont know if anyone of you guys know the Spin the Bottle application for the iphone. I want to make a simple app which offers the user a simple image with a bottle. The user should be able to rotate the bottle by using finger gestures. He/She should be able to rotate it with a rotation with the fingers and after releasing (touchesEnd) the bottle should spin further and slow down until it stop at a position based on the speed.
any clues or hints for a solution?
While it's better if you understand everything yourself, if you want a more concrete example the code here works
Narut's post which is like the 5th response has nice clean code for what you want.
In order to respond to a custom multi-touch gesture such as a rotation, you'll need to subclass either the image view or the view containing it and build a custom implementation of the following methods:
- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;
- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;
- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event;
- (void) touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event;
Essentially, think of the iPhone screen as a graph, and fingers as points. You use the above methods in a view to figure out how/where fingers are on the graph.
Good starting points:
http://developer.apple.com/IPhone/library/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/EventHandling/EventHandling.html
https://stackoverflow.com/questions/tagged/multi-touch+iphone
How can you tell when a button is being pressed? Specifically, how can you do something while a button is down?
While the screen is touched, there will be touch events. There are also touch up and touch down events.
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(#"touchesBegan");
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(#"touchesMoved");
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(#"touchesEnded");
}
Then just register your controller as the touchController.
Also check out Apple's GLPaint example:
https://developer.apple.com/iphone/library/samplecode/GLPaint/
You can also do it by creating an IBAction and connecting it IB. Interface builder for iPhone can actually demux different types of events. If you ctrl-click a UIButton in your UI, and release the mouse button instead of dragging a connection you will see an inspector that shows a bunch of different actions you can wire up. When you just drag it defaults to wiring up "Touch Up Inside", but if you want you can wire up "Touch Down" or "Touch Down Repeat" and that action will be cause the appropriate selector to be called.
If you are doing complicated stuff with your own gestures then using the touchController based stuff will definitely be a better approach, but for simple things just using the built in nib based actions may be simpler.