How to get a CGPoint from a tapped location? - ios

I'm working on a graphing calculator app for the iPad, and I wanted to add a feature where a user can tap an area in the graph view to make a text box pop up displaying the coordinate of the point they touched. How can I get a CGPoint from this?

you have two way ...
1.
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [[event allTouches] anyObject];
CGPoint location = [touch locationInView:touch.view];
}
here,you can get location with point from current view...
2.
UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapped:)];
[tapRecognizer setNumberOfTapsRequired:1];
[tapRecognizer setDelegate:self];
[self.view addGestureRecognizer:tapRecognizer];
here,this code use when you want to do somthing with your perticular object or subview of your mainview

Try This
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
// Get the specific point that was touched
CGPoint point = [touch locationInView:self.view];
NSLog(#"X location: %f", point.x);
NSLog(#"Y Location: %f",point.y);
}
You can use "touchesEnded" if you'd rather see where the user lifted their finger off the screen instead of where they touched down.

Just want to toss in a Swift 4 answer because the API is quite different looking.
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
if let touch = event?.allTouches?.first {
let loc:CGPoint = touch.location(in: touch.view)
//insert your touch based code here
}
}
OR
let tapGR = UITapGestureRecognizer(target: self, action: #selector(tapped))
view.addGestureRecognizer(tapGR)
#objc func tapped(gr:UITapGestureRecognizer) {
let loc:CGPoint = gr.location(in: gr.view)
//insert your touch based code here
}
In both cases loc will contain the point that was touched in the view.

it's probably better and simpler to use a UIGestureRecognizer with the map view instead of trying to subclass it and intercepting touches manually.
Step 1 : First, add the gesture recognizer to the map view:
UITapGestureRecognizer *tgr = [[UITapGestureRecognizer alloc]
initWithTarget:self action:#selector(tapGestureHandler:)];
tgr.delegate = self; //also add <UIGestureRecognizerDelegate> to #interface
[mapView addGestureRecognizer:tgr];
Step 2 : Next, implement shouldRecognizeSimultaneouslyWithGestureRecognizer and return YES so your tap gesture recognizer can work at the same time as the map's (otherwise taps on pins won't get handled automatically by the map):
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer
shouldRecognizeSimultaneouslyWithGestureRecognizer
:(UIGestureRecognizer *)otherGestureRecognizer
{
return YES;
}
Step 3 : Finally, implement the gesture handler:
- (void)tapGestureHandler:(UITapGestureRecognizer *)tgr
{
CGPoint touchPoint = [tgr locationInView:mapView];
CLLocationCoordinate2D touchMapCoordinate
= [mapView convertPoint:touchPoint toCoordinateFromView:mapView];
NSLog(#"tapGestureHandler: touchMapCoordinate = %f,%f",
touchMapCoordinate.latitude, touchMapCoordinate.longitude);
}

If you use an UIGestureRecognizer or UITouch object you can use the locationInView: method to retrieve the CGPoint within the given view the user touched.

func handleFrontTap(gestureRecognizer: UITapGestureRecognizer) {
print("tap working")
if gestureRecognizer.state == UIGestureRecognizerState.Recognized {
`print(gestureRecognizer.locationInView(gestureRecognizer.view))`
}
}

Related

How to pan without touch action getting executed in Sprite Kit?

In touchesBegan I have my logic for single touch. And I'm trying to add ability to change camera position with pan. For pan I use touchesMoved. Everything is kind of okay, but once I pan, action for touch gets executed too.
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
for (UITouch *touch in touches) {
CGPoint location = [touch locationInNode:self];
NSArray *sprites = [self nodesAtPoint:location];
for (SKSpriteNode *sprite in sprites)
{
//*
//* How to stop executing this block when panning?
//*
}
}
}
-(void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint positionInScene = [touch locationInNode:self];
CGPoint previousPosition = [touch previousLocationInNode:self];
CGPoint translation = CGPointMake((-1)*(positionInScene.x - previousPosition.x), (-1)*(positionInScene.y - previousPosition.y));
CGPoint cameraPos = [self camera].position;
[self camera].position = CGPointAdd(cameraPos, translation);
}
Look at how to use the Pan Gesture that is built into IOS, with it you will have to option to allow it to also execute the touch event or not.
I will give you an answer using the view controller, you may use it somewhere else though
Objective C:
Open up ViewController.h and add the following declaration:
#interface ViewController : UIViewController<UIGestureRecognizerDelegate>
...
- (IBAction)handlePan:(UIPanGestureRecognizer *)recognizer;
Then implement it in ViewController.m as follows:
- (IBAction)handlePan:(UIPanGestureRecognizer *)recognizer {
CGPoint translation = [recognizer translationInView:self.view];
recognizer.view.center = CGPointMake(recognizer.view.center.x + translation.x,
recognizer.view.center.y + translation.y);
[recognizer setTranslation:CGPointMake(0, 0) inView:self.view];
}
http://www.raywenderlich.com/6567/uigesturerecognizer-tutorial-in-ios-5-pinches-pans-and-more
At this point you can link it via UI like in the tutorial above, or declare somewhere in the beginning
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(handlePan:)];
[self.view addGestureRecognizer:pan];
Swift:
class ViewController : UIViewController, UIGestureRecognizerDelegate
... then in your code
#IBAction func handlePan(recognizer:UIPanGestureRecognizer) {
let translation = recognizer.translationInView(self.view)
if let view = recognizer.view {
view.center = CGPoint(x:view.center.x + translation.x,
y:view.center.y + translation.y)
}
recognizer.setTranslation(CGPointZero, inView: self.view)
}
http://www.raywenderlich.com/76020/using-uigesturerecognizer-with-swift-tutorial
At this point you can link it via UI like in the tutorial above, or declare somewhere in the beginning stages like init:
let pan = UIPanGestureRecognizer(target: self, action: "handlePan:")
self.view.addGestureRecognizer(pan);

TouchesMoved only moves a little

I am trying to allow the user to drag a label around the screen, but in the simulator it only moves a little each time I touch somewhere on the screen. It will jump to the location and then drag slightly, but then it will stop dragging and I have to touch a different location to get it to move again. Here is my code in my .m file.
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *Drag = [[event allTouches] anyObject];
firstInitial.center = [Drag locationInView: self.view];
}
My ultimate goal is to be able to drag three different labels on the screen, but I'm just trying to tackle this problem first. I would greatly appreciate any help!
Thanks.
Try using a UIGestureRecognizer instead of -touchesMoved:withEvent:. And implementing something similar to the following code.
//Inside viewDidLoad
UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc]initWithTarget:self action:#selector(dragonMoved:)];
panGesture.minimumNumberOfTouches = 1;
[self addGestureRecognizer:panGesture];
//**********
- (void)dragonMoved:(UIPanGestureRecognizer *)gesture{
CGPoint touchLocation = [gesture locationInView:self];
static UIView *currentDragObject;
if(UIGestureRecognizerStateBegan == gesture.state){
for(DragObect *dragView in self.dragObjects){
if(CGRectContainsPoint(dragView.frame, touchLocation)){
currentDragObject = dragView;
break;
}
}
}else if(UIGestureRecognizerStateChanged == gesture.state){
currentDragObject.center = touchLocation;
}else if (UIGestureRecognizerStateEnded == gesture.state){
currentDragObject = nil;
}
}

Is it possible to get the x and y coordinates of a touch?

is it possible to get the x and y coordinates of a touch? If so could someone please provide a very simple example where the coordinates are just logged to the console.
Using touchesBegan Event
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
CGPoint touchPoint = [touch locationInView:self.view];
NSLog(#"Touch x : %f y : %f", touchPoint.x, touchPoint.y);
}
This event is triggered when touch starts.
Using Gesture
Register your UITapGestureRecognizer in viewDidLoad: Method
- (void)viewDidLoad {
[super viewDidLoad];
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapGestureRecognizer:)];
[self.view setUserInteractionEnabled:YES];
[self.view addGestureRecognizer:tapGesture];
}
Setting up the tapGestureRecognizer function
// Tap GestureRecognizer function
- (void)tapGestureRecognizer:(UIGestureRecognizer *)recognizer {
CGPoint tappedPoint = [recognizer locationInView:self.view];
CGFloat xCoordinate = tappedPoint.x;
CGFloat yCoordinate = tappedPoint.y;
NSLog(#"Touch Using UITapGestureRecognizer x : %f y : %f", xCoordinate, yCoordinate);
}
Sample Project
First you need to add a gesture recognizer to the view you want.
UITapGestureRecognizer *myTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(myTapRecognizer:)];
[self.myView setUserInteractionEnabled:YES];
[self.myView addGestureRecognizer:myTap];
Then in the gesture recognizer method you make a call to locationInView:
- (void)myTapRecognizer:(UIGestureRecognizer *)recognizer
{
CGPoint tappedPoint = [recognizer locationInView:self.myView];
CGFloat xCoordinate = tappedPoint.x;
CGFloat yCoordinate = tappedPoint.y;
}
You may want to take a look at apple's UIGestureRecognizer Class Reference
Here's a very basic example (place it inside your view controller):
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint currentPoint = [touch locationInView:self.view];
NSLog(#"%#", NSStringFromCGPoint(currentPoint));
}
This triggers every time the touch moves. You can also use touchesBegan:withEvent: which triggers when a touch starts, and touchesEnded:withEvent: which triggers when a touch ends (i.e. a finger is lifted).
You can also do this using a UIGestureRecognizer, which in many cases is more practical.

How to get UITouch location from UIGestureRecognizer

I want to get the UITouch location of my tap from UIGestureRecognizer, but I can not figure out how to from looking at both the documentation and other SO questions. Can one of you guide me?
- (void)handleTap:(UITapGestureRecognizer *)tapRecognizer
{
CCLOG(#"Single tap");
UITouch *locationOfTap = tapRecognizer; //This doesn't work
CGPoint touchLocation = [_tileMap convertTouchToNodeSpace:locationOfTap];
//convertTouchToNodeSpace requires UITouch
[_cat moveToward:touchLocation];
}
FIXED CODE HERE - THIS ALSO FIXES INVERTED Y AXIS
CGPoint touchLocation = [[CCDirector sharedDirector] convertToGL:[self convertToNodeSpace:[tapRecognizer locationInView:[[CCDirector sharedDirector] openGLView]]]];
You can use the locationInView: method on UIGestureRecognizer. If you pass nil for the view, this method will return the location of the touch in the window.
- (void)handleTap:(UITapGestureRecognizer *)tapRecognizer
{
CGPoint touchPoint = [tapRecognizer locationInView: _tileMap]
}
There is also a helpful delegate method gestureRecognizer:shouldReceiveTouch:. Just make sure to implement and set your tap gesture's delegate to self.
Keep a reference to the gesture recognizer.
#property UITapGestureRecognizer *theTapRecognizer;
Initiailze the gesture recognizer
_theTapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget: self action: #selector(someMethod:)];
_theTapRecognizer.delegate = self;
[someView addGestureRecognizer: _theTapRecognizer];
Listen for delegate methods.
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
CGPoint touchLocation = [_tileMap convertTouchToNodeSpace: touch];
// use your CGPoint
return YES;
}
In Swift:
func handleFrontTap(gestureRecognizer: UITapGestureRecognizer) {
print("tap working")
if gestureRecognizer.state == UIGestureRecognizerState.Recognized
{
print(gestureRecognizer.locationInView(gestureRecognizer.view))
}
}
Try this:
-(void) didMoveToView:(SKView *)view{
oneFingerTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(oneTapDetected:)];
oneFingerTap.numberOfTapsRequired=1;
oneFingerTap.numberOfTouchesRequired=1;
[view addGestureRecognizer:oneFingerTap];
}
-(void)oneTapDetected:(UITapGestureRecognizer *)recognizer{
NSLog(#"one tap detec");
tapPositionOneFingerTap = [oneFingerTap locationInView:self.view];
NSLog(#"%f, %f",tapPositionOneFingerTap.x,tapPositionOneFingerTap.y);
}
This prints the coordinates of each tap in your console.
Apple Docs say
UIGestureRecognizer
- (NSUInteger)numberOfTouches
The number of UITouch objects in a private array maintained by the receiver.
So you shouldn't access them.
Using the value returned by this method in a loop, you can ask for the location of individual touches using the locationOfTouch:inView: method.

add mkannotation on touch map

I want to add a mkannotation to my mkmapview when an user taps over the map so they can choose a location.
I've read about the drag&drop but that's a bit annoying if you want to move to the other corner of the city because you have to move step by step the pin.
How can I get the coordinate where a user taps and move my pin there?
Thanks!
Use UITapGestureRecognizer to get the CGPoint and coordinate of tapped point.
UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(addPin:)];
[recognizer setNumberOfTapsRequired:1];
[map addGestureRecognizer:recognizer];
[recognizer release];
then add your target action
- (void)addPin:(UITapGestureRecognizer*)recognizer
{
CGPoint tappedPoint = [recognizer locationInView:map];
NSLog(#"Tapped At : %#",NSStringFromCGPoint(tappedPoint));
CLLocationCoordinate2D coord= [map convertPoint:tappedPoint toCoordinateFromView:map];
NSLog(#"lat %f",coord.latitude);
NSLog(#"long %f",coord.longitude);
// add an annotation with coord
}
On ios < 3.2, you can use this snippet:
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
if ( touch.tapCount == 1 ) {
UITouch *touch = [[event allTouches] anyObject];
if (CGRectContainsPoint([map frame], [touch locationInView:self.view]))
{
CLLocationCoordinate2D coord=
[map convertPoint:tappedPoint toCoordinateFromView:map];
NSLog(#"lat %f",coord.latitude);
NSLog(#"long %f",coord.longitude);
// add an annotation with coord
// or (as example before)
// [self addPin];
}
}
}
it's similar, but don't use UIGestureRecognizer.
Hope this helps.

Resources