Skip to content

Instantly share code, notes, and snippets.

@nolim1t
Created June 30, 2010 02:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nolim1t/458126 to your computer and use it in GitHub Desktop.
Save nolim1t/458126 to your computer and use it in GitHub Desktop.
How to visualize UITouch events on the iPhone/iPad
/*
* This bit of code is to visualize how touches works
* so that we can use it to drag around objects on a screen.
*/
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint touchPoint = [touch locationInView:self.view];
NSLog(@"touchesBegan: X: %2.2f Y: %2.2f", touchPoint.x, touchPoint.y);
}
-(void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint touchPoint = [touch locationInView:self.view];
// Some simple code to demonstrate that you can move a
// UIView (which you can customize beyond your wildest dreams :D)
// around.
[self moveBox:someViewName WithTouch:touch WithTouchPoint:touchPoint];
}
-(void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint touchPoint = [touch locationInView:self.view];
NSLog(@"touchesEnded: X: %2.2f Y: %2.2f", touchPoint.x, touchPoint.y);
}
#pragma mark -
#pragma mark Moving UIViews around
-(void) moveBox:(CustomViewName *)viewname WithTouch:(UITouch *)touch WithTouchPoint:(CGPoint)touchPoint {
if (touch.view == viewname) {
viewname.center = touchPoint;
// TODO: Collision detection/handling code
// This can get trickier.
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment