Codesnipp.it Social Code Sharing

Amber Weinberg

Detect Touches & Gestures

by Amber Weinberg on Oct 22, 2010

/* From Beginning iPhone Development 3 by Dave Mark*/ /*This app plays with the touches and gestures feature of the iPhone and lets you know three things: that a touch began/ended, how many taps detected and how many touches detected*/ /* in your viewController.h file*/ // // _3_Touch_ExplorerViewController.h // 13 Touch Explorer // // Created by Amber Weinberg on 10/22/10. // Copyright __MyCompanyName__ 2010. All rights reserved. // #import <UIKit/UIKit.h> @interface _3_Touch_ExplorerViewController : UIViewController { UILabel *messageLabel; UILabel *tapsLabel; UILabel *touchesLabel; } @property (nonatomic, retain) IBOutlet UILabel *messageLabel; @property (nonatomic, retain) IBOutlet UILabel *tapsLabel; @property (nonatomic, retain) IBOutlet UILabel *touchesLabel; - (void)updateLabelsFromTouches:(NSSet *)touches; @end /*In your viewController.m file*/ // // _3_Touch_ExplorerViewController.m // 13 Touch Explorer // // Created by Amber Weinberg on 10/22/10. // Copyright __MyCompanyName__ 2010. All rights reserved. // #import "_3_Touch_ExplorerViewController.h" @implementation _3_Touch_ExplorerViewController @synthesize messageLabel; @synthesize tapsLabel; @synthesize touchesLabel; - (void)updateLabelsFromTouches:(NSSet *)touches { NSUInteger numTaps = [[touches anyObject] tapCount]; NSString *tapsMessage = [[NSString alloc] initWithFormat:@"%d taps detected", numTaps]; tapsLabel.text = tapsMessage; [tapsMessage release]; NSUInteger numTouches = [touches count]; NSString *touchMsg = [[NSString alloc] initWithFormat:@"%d touches detected", numTouches]; touchesLabel.text = touchMsg; [touchMsg release]; } - (void)didReceiveMemoryWarning { // Releases the view if it doesn't have a superview. [super didReceiveMemoryWarning]; // Release any cached data, images, etc that aren't in use. } - (void)viewDidUnload { // Release any retained subviews of the main view. // e.g. self.myOutlet = nil; self.messageLabel = nil; self.tapsLabel = nil; self.touchesLabel = nil; [super viewDidUnload]; } - (void)dealloc { [messageLabel release]; [tapsLabel release]; [touchesLabel release]; [super dealloc]; } #pragma mark - - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { messageLabel.text = @"Touches Began"; [self updateLabelsFromTouches:touches]; } - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event { messageLabel.text = @"Touches Cancelled"; [self updateLabelsFromTouches:touches]; } - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { messageLabel.text = @"Touches Stopped"; [self updateLabelsFromTouches:touches]; } - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { messageLabel.text = @"Drag Detected"; [self updateLabelsFromTouches:touches]; } @end

Can't see the comments? Please login first :)