PDA

View Full Version : LocationInView X & Y Coordinates?




Darkroom
Jun 3, 2009, 05:57 PM
i'm trying to print out a string using CGPoint locationInView, but i only receive strange numbers.


UITouch *touch = [touches anyObject];
CGPoint touchLocation = [touch locationInView:self.view];
NSString *locationMessage = [[NSString alloc] initWithFormat:@"Location: %f", touchLocation];
locationLabel.text = locationMessage;
[locationMessage release];


how can i receive X and Y coordinates as a string?

locationLable.text (example) of the above code:

Location: 3527480165821054976.000000



dejo
Jun 3, 2009, 06:13 PM
touchLocation is a CGPoint and so won't respond correctly to %***** it in a string format. Try something like this instead:
NSString *locationMessage = [[NSString alloc] initWithFormat:@"Location: x=%f, y=%f", touchLocation.x, touchLocation.y];

Darkroom
Jun 3, 2009, 06:20 PM
touchLocation is a CGPoint and so won't respond correctly to %***** it in a string format. Try something like this instead:
NSString *locationMessage = [[NSString alloc] initWithFormat:@"Location: x=%f, y=%f", touchLocation.x, touchLocation.y];


ah! thanks :)... works great.

at first i tried casting the object as a string but that was crashville and it's suburbs.

thanks again.

revised code:

CGPoint touchLocation = [[touches anyObject] locationInView:self.view];
NSString *locationMessage = [[NSString alloc] initWithFormat:@"Location: X = %.0f Y = %.0f", touchLocation.x, touchLocation.y];
locationLabel.text = locationMessage;
[locationMessage release];

Kpeters213
Jun 9, 2010, 02:18 PM
Thank you so much guys. This really helped me out.

Showing the before and after code really helped too!

-KP

PhoneyDeveloper
Jun 9, 2010, 06:51 PM
You can also use the functions in UIGeometry.h, like NSStringFromCGPoint()