Posts Tagged ‘handling touches events’

Touches

April 26th, 2009

Touches are handled by three methods:

  • touchesBegan:
  • touchesMoved:
  • touchesEnded:

To have access to touches you need to code these 3 methods (or one/two of them).

The code is very simple:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
} 
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
}

touchesBegan: will be called whenever user touch the screen with one finger, and each time new touch is declared for example with the second finger, nose ear, battery, foot…

touchesEnded: will be called whenever any single touch is ended

touchesMoved: hmmm it’s not so clear as you might think. Yes, this method will be called if any finger moves while touching the screen, but on real device this method is called almost immidietely after touchesBegan. Why is that? On iPhone simulator mouse plays role of the finger, so you can press and release the mouse/touch-pad button and both touchesBegan and touchesEnded will be called, but not touchesMoved provided that you won’t move a pointer. If you touch an iPhone / iPod screen in 99% the coordinates of where the touch started and ended will be different, so meanwhile touchesMoved will be called.

You probably woud like to know what are the touch coordinates, it’s very easy to get them. Extend any of above three methods with this code:

	UITouch *touch = [touches anyObject];
	CGPoint touchCoordinates = [touch locationInView:self.view];

touchCoordinates.x and touchCoordinates.y give you information you need.

If you would like to have details of more than single touch (you can touch the screen with as many fingers you want), this might be helpful:

	NSSet *allTouches = [event allTouches];
	UITouch *singleTouch;
	for (int i = 0; i < [allTouches count]; i++) {
		singleTouch = [[allTouches allObjects] objectAtIndex:i];
		// whatever you need goes here
	}

If you would like to simply know how many fingers are touching the screen use [allTouches count]; with first line of above example.

If you want to do something when users double tap the screen for example try:

	if ([touch tapCount] == 2) {
		// whatever you need goes here
	}

You can use only tapCount for the single touch. Taps aren’t counted for multiple touches.

Now something more interesting. How to check if user touched an object like image, label…? Before you enter any code in the attributes in Interface Builder of your item enable option: User Interaction Enabled and Multiple Touches if needed. views have User Iteraction enabled by default. You can also do it in XCode:

	myObject.userInteractionEnabled = YES;
	myObject.multipleTouchEnabled = YES;

Once User Interaction is enabled you can use if statement to perform any task:

	if ([touch view] == myObject) {
		// whatever you need goes here
	}

touchessimulator I guess it’s all you need to know about touches. I prepared for you a project you can download. It gives you all information:

  • where the touch began (x,y)
  • where the touch moved (x,y)
  • where the touch ended (x,y)
  • how many fingers touched the screen (in touch began)
  • the tap count
  • and a Move Me square you moves by moving your finger

xcodeproj

Download the project