views:

55

answers:

2

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
`NSLog(@"X:%d Y:%d", touchPoint.x, touchPoint.y);`
Kurbz
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
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
@Joe: http://developer.apple.com/iphone/library/documentation/EventHandling/Conceptual/EventHandlingiPhoneOS/Introduction/Introduction.html
Chuck
just put the above code in the implementation section of appNameViewController.m.
jasongetsdown
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
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
I'm pretty sure he meant "make a sub*class* of the UIView" there.
David Liu
just put it in your viewcontroller.m
Kurbz
Sorry, being a bit tired last night. Of course I meant subclass of the UIView, not subview. Thanks for pointing that out...
jollyCocoa