iOS Game Projects, Touch Events




CS175

Chris Pollett

Apr 23, 2012

Outline

Introduction

Creating an OpenGL ES Application

Understanding the OpenGL ES Example

setupGL and tearDownGL

Shader Handling

GLKView and GLKViewController delegate methods

Quiz

Which of the following is true?

  1. On Android the only way to test location services is on an actual device -- It can't be done in the emulator.
  2. On iOS, subclasses of UIView can handle touch events by overriding methods like touchesBegan, touchesMoved, etc.
  3. One of UIViewController methods is called:
    - (void) locationManager:(CLLocationManager *)manager 
    		didUpdateHeading: (CLHeading *)newHeading ;
    
    and it can be used to handle compass direction changes.

Touch Event Terms

  • Today, we will look at how to handle various kinds of touch events on mobile phones.
  • First, let's specify some common terms:
  • You can also register events for multiple taps in quick succession. (double-tap, triple-tap, etc.)
  • Responder Chain

    Forwarding Events

    Multitouch Architecture

    Gesture Notification

    More on Gesture Notification

    Still More on Gesture Notification

    Touch Application

    View Controller Header

    Changes to TouchApp Storyboard 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
    
    -(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