I am looking to get the location of a touch, preferably corresponding to the pixels of the screen. I'm new to objective-c and cocoa and haven't been able to find anything on the web about this so i'm not sure if there is even a way to do it. any ideas or direction of where to look would be really helpful. thanks.
A:
i think i found the solution i'm just not sure how to use it (i.e. where am i supposed to put the code):
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
// gets the coordinats of the touch with respect to the specified view.
CGPoint touchPoint = [touch locationInView:self];
}
what would i have to do to continuously output the coords in an NSLog statement?
Joe
2010-07-26 20:33:23
`NSLog(@"X:%d Y:%d", touchPoint.x, touchPoint.y);`
Kurbz
2010-07-26 20:35:41
It goes in your view controller - should be appNameViewController in the classes folder. Its inherited from UIResponder, but it doesn't do anything until you overload it by including it in your controller class (which is a subclass of UIResponder).
jasongetsdown
2010-07-26 20:40:48
okay thank you for your responses,however im new to all this so maybe someone can metaphorically hold my hand and explain step by step what i need to do. i would greatly appreciate it :)
Joe
2010-07-26 20:44:23
@Joe: http://developer.apple.com/iphone/library/documentation/EventHandling/Conceptual/EventHandlingiPhoneOS/Introduction/Introduction.html
Chuck
2010-07-26 20:53:36
just put the above code in the implementation section of appNameViewController.m.
jasongetsdown
2010-07-26 21:54:20
A:
On iPhone you make a subclass of the UIView and implement the funktion -(void)touchesBegan:(NSSet *)touches withEvent:(NSEvent *)anEvent:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *myTouch = [[touches allObjects] objectAtIndex: 0];
CGPoint currentPos = [myTouch locationInView: self];
NSLog(@"Point in myView: (%f,%f)", currentPos.x, currentPos.y);
}
jollyCocoa
2010-07-26 20:34:03
awesome, but can you explain how i make a subview of UIView and where exactly i should put the code in my project file(s)?
Joe
2010-07-26 20:36:53
Sorry, being a bit tired last night. Of course I meant subclass of the UIView, not subview. Thanks for pointing that out...
jollyCocoa
2010-07-27 06:40:50