CS185c
Chris Pollett
Nov. 22, 2010
-(void)respondToMyEvent: (UIEvent *) event { if(condition) { [self handleEvent: event]; } else { [self.nextResponder respondToMyEvent: event]; } }
Which of the following statements is true:
- (void)touchesBegan: (NSSet *)touches withEvent:(UIEvent *)event { NSUInteger numTaps = [[touches anyObject] tapCount]; NSUInteger numTouches = [touches count]; }
NSSet *myTouches = [event touchesForView:self.view];
CGPoint point = [touch locationInView:self];
#import <UIKit/UIKit.h> @interface TouchAppViewController : UIViewController { IBOutlet UILabel *messageLabel; IBOutlet UILabel *tapsLabel; IBOutlet UILabel *touchesLabel; } @property (nonatomic, retain) UILabel *messageLabel; @property (nonatomic, retain) UILabel *tapsLabel; @property (nonatomic, retain) UILabel *touchesLabel; -(void)updateLabelsFromTouches: (NSSet *)touches; @end
#import "TouchAppViewController.h" @implementation TouchAppViewController @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; } - (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 Ended"; [self updateLabelsFromTouches:touches]; } - (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { messageLabel.text = @"Drag detected"; [self updateLabelsFromTouches:touches]; } @end
case 2: //double tap [NSObject cancelPreviousPerformRequestWithTarget:self selector:@selector(singleTap) object:nil]; [self performSelector:@selector(doubleTap) withObject:nil afterDelay: .4] break; case 3: //triple tap [NSObject cancelPreviousPerformRequestWithTarget:self selector:@selector(doubleTap) object:nil]; [self performSelector:@selector(tripleTap) withObject:nil afterDelay: .4] break;in touchesBegan.
#import <UIKit/UIKit.h> #define kMinimumGestureLength 25 #define kMaximumVariance 5 @interface SwipeAppViewController : UIViewController { IBOutlet UILabel *label; CGPoint gestureStartPoint; } @property (nonatomic, retain) UILabel *label; @property CGPoint gestureStartPoint; -(void)eraseText; @end
#import "SwipeAppViewController.h" @implementation SwipeAppViewController @synthesize label; @synthesize gestureStartPoint; -(void)eraseText { label.text = @""; } - (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; } - (void)dealloc { [label release]; [super dealloc]; } #pragma mark - -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { UITouch *touch = [touches anyObject]; gestureStartPoint = [touch locationInView:self.view]; } -(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { UITouch *touch = [touches anyObject]; CGPoint currentPosition = [touch locationInView:self.view]; CGFloat deltaX = fabsf(gestureStartPoint.x - currentPosition.x); CGFloat deltaY = fabsf(gestureStartPoint.y - currentPosition.y); if(deltaX >= kMinimumGestureLength && deltaY <= kMaximumVariance) { label.text = @"Horizontal swipe detected"; [self performSelector:@selector(eraseText) withObject:nil afterDelay: 2]; } else if(deltaY >= kMinimumGestureLength && deltaX <= kMaximumVariance) { label.text = @"Vertical swipe detected"; [self performSelector:@selector(eraseText) withObject:nil afterDelay: 2]; } } @end
boolean onKey(View v, int keyCode, KeyEvent event)the key code tells you what key was pressed.
boolean onTouch(View v, MotionEvent event)