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

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.

Related

iOS: Change alpha of image view with gestures

I want to change the alpha of image with the gestures like up and down or right and left.
I don't want to use slider for that.
Is it possible to do this if yes then please tell me how or i have to use slider for that?
Thank you.
You can implement method on view or view controller
- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInView:self];
NSLog(#"location: %#",NSStringFromCGPoint(location));
// Example:
// alpha depends on location.x related to view bounds width
CGFloat alpha = (self.bounds.size.width - location.x) / self.bounds.size.width;
NSLog(#"alpha: %#",#(alpha));
}
where you can check location.x or location.y.
Try to do like following :
First attach tap GestureRecognizer to view:
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(yourTapMethod:)];
tap.numberOfTapsRequired = 1;
tap.numberOfTouchesRequired = 1;
[imageView addGestureRecognizer:tap];
Then, in gesture recognizer handler do what do you want:
-(void)yourTapMethod{
[UIView animateWithDuration:0.5 animations:^(void){
imageView.alpha = //add any value (like 0.2f);
}

Trying to move UIImageView, moving whole Screen View

I want my UIViewImages to be movable with touch. I'm trying to use code implemented in my ViewController:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
if ([touches count]==1) {
UITouch *touch = [touches anyObject];
CGPoint p0 = [touch previousLocationInView:self.view];
CGPoint p1 = [touch locationInView:self.view];
CGPoint center = self.view.center;
center.x += p1.x - p0.x;
center.y += p1.y - p0.y;
self.view.center = center;
}
}
When I try to drag an UIImageView, I'm dragging whole screen, which is incorrect.
Need help!
Karol
You create a gesture recognizer and add it to a view like this:
UIPanGestureRecognizer *panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(gestureRecognizerMethod:)];
[self.imageView addGestureRecognizer:panGestureRecognizer];
you can adjust the position of the image view
- (void)gestureRecognizerMethod:(UIPanGestureRecognizer *)recogniser
{
if (recognizer.state == UIGestureRecognizerStateBegan || recognizer.state == UIGestureRecognizerStateChanged)
{
CGPoint touchLocation = [recognizer locationInView:self.view];
self.imageView.center = touchLocation;
}
}
read this article.
If I read your code right self.view actually IS the whole screen.
Maybe you mean self.yourImageView instead?

iOS: Drag Effect not working well

I have implemented the drag effect on an image but during my test I see that the image is moving only on the click mouse event.
I cannot move my image with the mouse on my screen through the drag event. But when I click on a side of my screen the image take the place where I have clicked.
I followed many topics on youtube but finally, I haven't the same behavior.
This my code:
ScreenView1.h
IBOutlet UIImageView *image;
ScreenView1.m
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [[event allTouches] anyObject];
CGPoint location = [touch locationInView:touch.view];
image.center = location;
[self ifCollision];
}
-(void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
[self touchesBegan:touches withEvent:event];
}
If you want to drag an image view, you will be so much happier using a UIPanGestureRecognizer. It makes this sort of thing trivial. Using touchesBegan is so iOS 4!
UIPanGestureRecognizer* p =
[[UIPanGestureRecognizer alloc] initWithTarget:self
action:#selector(dragging:)];
[imageView addGestureRecognizer:p];
// ...
- (void) dragging: (UIPanGestureRecognizer*) p {
UIView* vv = p.view;
if (p.state == UIGestureRecognizerStateBegan ||
p.state == UIGestureRecognizerStateChanged) {
CGPoint delta = [p translationInView: vv.superview];
CGPoint c = vv.center;
c.x += delta.x; c.y += delta.y;
vv.center = c;
[p setTranslation: CGPointZero inView: vv.superview];
}
}
You're not doing the right thing in the touchesMoved:withEvent:, which is why the drag won't work. Here's a little code that works:
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInView:self];
[CATransaction begin];
[CATransaction setDisableActions:YES];
[image setCenter:location];
[CATransaction commit];
}
For the others, I have implemented my issue in that way:
- (IBAction)catchPanEvent:(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];
}
thank you again Matt!

How to get a CGPoint from a tapped location?

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))`
}
}

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