Touch Events




CS185c

Chris Pollett

Apr 23, 2012

Outline

Introduction

Responder Chain

Forwarding Events

Quiz

Which of the following is true?

  1. On iPhone to say one is going to handle location data one implements CLLocationManagerDelegate.
  2. Core Graphics is a pure Objective-C API.
  3. The same getSystemService call can be used to get a manager for both location and accelerometer data on Android.

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