Finishing Touches




CS185c

Chris Pollett

Nov. 30, 2009

Outline

Announcements

Introduction

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

Quiz

Which of the following statements is true:

  1. In iPhone terminology a touch can involve more than one finger.
  2. You can put your code to handle gestures in either a UIView object or in your UIViewController.
  3. OpenGL 2.0 is backward compatible with OpenGL 1.1 .

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 and Touch Events