Created
June 30, 2010 02:10
-
-
Save nolim1t/458126 to your computer and use it in GitHub Desktop.
How to visualize UITouch events on the iPhone/iPad
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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