iPhone – Implement a Simple Touch Event

It is very simple to implement touch events in iPhone application. The touch event is defined in the UIResponder which is the super class UIView. So for any UI controls, simply add the following piece of code to handle the touch events.

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
	NSLog(@"You touched %d times!", [[touches anyObject] tapCount]);
}

 

Try to tap the UI controls you will get the following debug output.

2010-08-20 14:44:28.467 Test[28079:207] You touched 1 times!
2010-08-20 14:44:29.363 Test[28079:207] You touched 1 times!
2010-08-20 14:44:29.499 Test[28079:207] You touched 2 times!
2010-08-20 14:44:30.051 Test[28079:207] You touched 1 times!
2010-08-20 14:44:30.179 Test[28079:207] You touched 2 times!
2010-08-20 14:44:30.315 Test[28079:207] You touched 3 times!
2010-08-20 14:44:32.155 Test[28079:207] You touched 1 times!
2010-08-20 14:44:32.747 Test[28079:207] You touched 1 times!
2010-08-20 14:44:32.851 Test[28079:207] You touched 2 times!
2010-08-20 14:44:34.987 Test[28079:207] You touched 1 times!
2010-08-20 14:44:35.123 Test[28079:207] You touched 2 times!
2010-08-20 14:44:36.043 Test[28079:207] You touched 1 times!
2010-08-20 14:44:36.187 Test[28079:207] You touched 2 times!
2010-08-20 14:44:36.339 Test[28079:207] You touched 3 times!
2010-08-20 14:44:36.475 Test[28079:207] You touched 4 times!
2010-08-20 14:44:36.595 Test[28079:207] You touched 5 times!
2010-08-20 14:44:36.731 Test[28079:207] You touched 6 times!
2010-08-20 14:44:36.867 Test[28079:207] You touched 7 times!
2010-08-20 14:44:36.971 Test[28079:207] You touched 8 times!
2010-08-20 14:44:37.099 Test[28079:207] You touched 9 times!
2010-08-20 14:44:37.235 Test[28079:207] You touched 10 times!
2010-08-20 14:44:38.099 Test[28079:207] You touched 1 times!

 

The UIResponder also provide touchesMoved, touchesEnded, touchesCancelled in respond to touch events.

Done =)

Reference: UIResponder Class Reference

About these ads

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s