Touch Events




CS185c

Chris Pollett

Nov. 22, 2010

Outline

Introduction

Responder Chain

Forwarding Events

Quiz

Which of the following statements is true:

  1. On Phone 7 you can telnet into port 5554 of the emulator and send commands like geo fix -10.5 23.4 to specify GPS info.
  2. SensorManager is one of the classes used to get accelerometer data on Android.
  3. Expression Blend is a common way to make animations on the iPhone.

Multitouch Architecture

Gesture Notification

More on Gesture Notification

Still More on Gesture Notification

Touch Application

View Controller Header

Changes to TouchApp XIB file

View Controller Code

Touch App Screenshot

Screenshot of the touch app running showing a message that a touch event just ended

Handling More Complicated Touch Events

Swipe Example

Swipe Controller Code

#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

Android Key and Touch Events