Add an image to touch location iOS - ios

I have an app that connects points in an array with lines, what I want to do next is to add a marker to every line's end points, I chose a red dot for that. But I can't figure out how to even start. The image should appear every time the user taps the screen, because that's when a new point is created. Thanks.

The simplest solution would be to add a UITapGestureRecognizerto your view with the following method as the GR's action. Supposing you're using e.g an image asset for your dot it would be:
-(void)screenTapped:(UITapGestureRecognizer*)tap {
CGPoint tapLocation = [tap locationInView:self.view];
UIImageView *redDotImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"redDot"]];
redDotImageView.center = tapLocation;
[self.view addSubview:redDotImageView];
}

For example you can add gesture recognized to your view:
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(addDotToTapPoint:)];
[view addGestureRecognizer:tapGesture];
then implement method addDotToTapPoint: to find point of tap:
- (void)addDotToTapPoint:(UITapGestureRecognizer *)sender {
CGPoint pointOfTouch = [sender locationInView:sender.view];
//... draw circle or something
}
Second approach.
You can create your own subclass of UIView and implement method touchesBegan:withEvent:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
CGPoint pointOfTouch = [(UITouch *)[touches anyObject] locationInView:self];
//... draw circle or something
}
In both cases You will get location of touch and can draw something in that point

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

Dragging coordinates with CGPoint

I want to get the coordinates from the user's finger while dragging. I tried this code, but it says the coordinates are always {0, 0},
What's wrong?
- (IBAction)Drag{
UIPanGestureRecognizer *Recognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(dragged)];
[self.view addGestureRecognizer:Recognizer];
}
-(void) dragged{
UITouch *touch ;
CGPoint location = [touch locationInView:touch.view];
NSLog(#"%#", NSStringFromCGPoint (location));
}
I also tried NSLog(#"%.2f %.2f" location.x, location.y); and got the same.
Thanks
It's quite normal, you're using touch without having ever assigned a value to it.
The action for a gesture recognizer takes a parameter, which is the recognizer itself, which in turn has a locationInView: method, so you should use that. Also, you need to check the state of the recognizer. Finally, you probably don't want to add the gesture recognizer when you need it, just add it from the start.
// probably in your viewDidLoad
UIPanGestureRecognizer *Recognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self
action:#selector(panGestureRecognizerAction:)];
[self.view addGestureRecognizer:Recognizer];
- (void)panGestureRecognizerAction:(UIPanGestureRecognizer *)recognizer
{
if (recognizer.state == UIGestureRecognizerStateBegan ||
recognizer.state == UIGestureRecognizerStateChanged)
{
CGPoint location = [recognizer.state locationInView:touch.view];
NSLog(#"%#", NSStringFromCGPoint (location));
}
}

Define a rectangle of locations in objective-C

I have an Image in an Iphone App, I would like when a user taps a location within a defined area (range of locations or an area defined by the rectangle), it triggers another event. I know how to get a single location, But I don't know how to define a rectangular area. I am looking for a simple way to implement it. Thank you
If you want to make the rectangle visible you can add image view to your view and set up the tap recogniser. But if you don't want to make the rectangle visible you can override touchesBegan:withEvent: method and use CGRectContainsPoint:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
CGPoint touchLocation = [touch locationInView:self.view];
CGRect rect = CGRectMake(0.0, 0.0, 100, 100); //<- this is the rectangle you do check on
if (CGRectContainsPoint(rect, touchLocation)) {
NSLog(#"You tapped inside rectangle");
}
else {
NSLog(#"You missed rectangle");
}
}
Define your area as a CGRect and get the CGPoint of the touch. Then use CGRectContainsPoint to check for a hit.
If you need to create the CGRect from a list of points then you need to iterate the points and find the max and min x and y values, then you can create the CGRect with:
CGRectMake(minX, minY, maxX - minX, maxY - minY);
Maybe you can create a UIView and add a gesture recognizer like this:
UIView *customView = [UIView new];
customView.frame = CGRectMake(....); // as Wain suggest
[self.view addSubview:customView];
UITapGestureRecognizer *tapGesture =
[[UITapGestureRecognizer alloc] initWithTarget: self
action: #selector(someMethod)];
[customView addGestureRecognizer:tapGesture];

To set Uianimation and Touch Event for UIImageview

I'm Working in a project it need to animate the image from one place to another i complete that but my problem is while i animating i don't get the touch event from that UIImageview .so any know please give the solution asap.
- (void) imageSpawn
{
NSArray *images= [NSArray arrayWithObjects:[UIImage imageNamed:#"fish_right1.png"],
[UIImage imageNamed:#"fish_right2.png"], [UIImage imageNamed:#"fish_right3.png"], [UIImage imageNamed:#"fish_right4.png"], [UIImage imageNamed:#"fish_right14.png"], [UIImage imageNamed:#"fish_right15.png"], [UIImage imageNamed:#"fish_right20.png"], nil];
int currentImageIndex=0;
[UIView animateWithDuration:2.0 delay:0.0 options:UIViewAnimationOptionAllowUserInteraction animations:^{
[self.first_fish setImage:[images objectAtIndex:currentImageIndex] ];
}completion:Nil ];
UITapGestureRecognizer *tapped = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(ballTapped:)];
tapped.numberOfTapsRequired = 1;
[rocket addGestureRecognizer:tapped];
[rocket setUserInteractionEnabled:YES];
}
-(void)ballTapped:(UIGestureRecognizer *)gesture
{
//here also you can get the tapped point if you need
CGPoint location = [gesture locationInView:gesture.view];
NSLog(#"LOCA X:%d",gesture.view.tag);
NSLog(#"LOCA y:%f",location.y);
}
Regards,
Raja.I
There's an option, UIViewAnimationOptionAllowUserInteraction, that you can pass to the options parameter in animateWithDuration:delay:options:animations:completion:. You need to set that to allow interactions during an animation.
After Edit: This has to do with the nature of the way you're doing the animation. If you click on the place where your image view ends up (while the animation is running) you will see that the gesture recognizer fires. In effect, the location of the view is already set to the final value when the animation starts.
To make this work, I think you have to do it with a timer instead of animateWithDuration. Create a repeating timer, and increment the x position of your view with each call, and invalidate the timer when you reach the destination. This will allow you to interact with the view while it moves.
When you animate a view you set the view in its final state. This way you can only detect touches in the final position where you are animating.
However, it is possible to go around that issue. You need to catch the touch event and compare with the presentationLayer.
-(void) touchesBegan:(NSSet*) touches withEvent:(UIEvent *) event {
CGPoint point = [[touches anyObject] locationInView:self.view];
if([self.cloudAnimate.layer.presentationLayer hitTest:point]) {
//do something
}
}
The presentation layer has information about the visual position of the CALayer that is associated with your cloudAnimate.
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint touchLocation = [touch locationInView:self.view];
if([imageView1.layer.presentationLayer hitTest:touchLocation]){
// TODO
}
}

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

Resources