Intercept touches on UIView over GMSMapView - ios

I'm trying to overlay a UIView over a google GMSMapView. The goal is to intercept swipes in order to know when the map is being moved manually. (I know I could check -mapView:willMove: but that gets called regardless of whether the map is moving programmatically or as result of manual touch). I'm programmatically re-centering the map as the position updates--because Google doesn't do it for you like Apple does. Like every map program out there, I want to stop centering on the current position as soon as the user manually moves the map. The only way I can think to capture that occurrence (short of switching to mapkit or waiting for Google to add to the API) is to capture a swipe as it's going past a view laid over the map.
To clarify, my question is how do I lay a view over a google map, make it respond to a swipe and still allow that event to pass through to the GMSMapView?
Both my map and interceptView are sibling children of the main view. interceptView has a gestureRecognizer, is set to opaque, not hidden and userInteractionEnabled = YES. As such the interceptView picks up touch events just fine. But the map underneath doesn't move. If I set interceptView.userInteractionEnabled = NO, the map works fine.
"Passing touches" by overriding touchesBegan and calling GMSMapView's touchesBegan doesn't do anything. And overriding hitTest doesn't seem to be an option because I can't subclass GMSMapView. Has anyone done this? I know there's a lot on here about passing particular events through. I want to process a particular event on two views, one of which is Google's map layer which I can't modify the code of.

mapView:willMove: receives a bool "gesture" which is true if the move was triggered by a user gesture and false if it wasn't. So you can stop re-centering the map only if "gesture" is true. That way you can be sure that the map is being moved manually.

Here is a delegate method check it
- (void)mapView:(GMSMapView *)mapView didTapAtCoordinate:(CLLocationCoordinate2D)coordinate;

You can intercepte the tap using the GMSMapViewDelegete and implementing this method:
-(void)panoramaView:(GMSPanoramaView *)panoramaView didTap:(CGPoint)point;
Example:
-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
CLLocationCoordinate2D panoramaNear = {latitudine,longitudine};
GMSPanoramaView *panoView = [GMSPanoramaView panoramaWithFrame:CGRectZero
nearCoordinate:panoramaNear];
[panoView setDelegate:self];
self.view = panoView;
}
-(void)panoramaView:(GMSPanoramaView *)panoramaView didTap:(CGPoint)point{
NSLog(#"Tap");
}

Related

Proper UIGestureRecognizer and Delegate design

This is a pretty hypothetical question just to understand proper design but lets say I have two custom UIViews.
One of them is essentially a container that I'll call a drawer. Its purpose is to hide and show content. It's a lot like the notification center on iOS where you swipe to pull it open and flick it back up to close it. It's a generic container than can contain any other UIView. It has a UIPanGestureRecognizer to track the finger that's pulling it open/closed. It might also have a UISwipeGestureRecognizer to detect a "flick".
The other view is a custom map widget that has UIPan/Rotation/Pinch GestureRecognizers.
I think the drawer view should be the UIGestureRecognizerDelegate for the Pan/Swipe GestureRecognizers so that it can prevent touches from being delivered unless the user is grabbing "the handle".
My first instinct is for the map to be the UIGestureRecognizerDelegate of the pan/rotation/pinch gestures so that it can allow them to all run simultaneously.
The problem I'm having is that, I really don't want the map to receive any touches or begin recognizing gestures until the drawer is completely open. I'd like to be able to enforce this behavior automatically in the drawer itself so that it works for all subviews right out of the box.
The only way that I can think to do this is to wire all of the gestures handlers to the ViewController and let it do everything, but to me that breaks encapsulation as now it has to know that the map gestures need to run simultaneously, that the drawer should only get touches on it's handle and that the map should only get touches when it's open.
What are some ways of doing this where the logic can stay in the Views where I think it belongs?
I would do something like this to make the subviews of the drawer disabled while panning. Essentially loop through the drawer's subviews and disbale interaction on them.
[self.subviews enumerateObjectsUsingBlock:^(UIView *subview, NSUInteger idx, BOOL *stop){
subview.userInteractionEnabled = NO;
}];
And something similar again for when you want to re-enable user interaction on the subviews.
This should already Just Work™. A gesture recogniser is attached to a view; when a continuous gesture is recognised, all subsequent touches associated with that gesture are associated with that view.
So in your case, when the drawer pan is recognised, no touches associated with that pan should ever cause behaviour in your map view's pan/pinch/rotation gestures (unless you explicitly specify that they should using the appropriate delegate methods).
Or do you mean that you want to prevent the user from, halfway through opening the drawer, using another finger (i.e. another gesture) to start scrolling the (half-visible) map? If so, you should just set userInteractionEnabled on the drawer's contentView (or equivalent) to NO at UIGestureRecognizerStateBegan/Changed and YES again at UIGestureRecognizerStateEnded/Cancelled.

iOS - Filtering and forwarding touches to subviews

The application I'm building has a full-screen MKMapView, with another UIView subclass placed over it, full-screen as well and completely transparent. I would like for the UIView subclass to handle single touch gestures, such as taps and single finger drags, and ignore anything else. This would allow the MKMapView to be interacted with using other means, especially panning/scrolling with two fingers by disabling 3D functions.
My issue here is that MKMapView does not use the touchesXXX:withEvent: methods for its user interaction. So, I can't detect touch count in those methods on the view and forward to the map. Likewise, the hitTest:withEvent: method can't be used to determine which view handles the touches, because the UIEvent object there returns an empty set of touches.
I've considered letting all touches forward through the view and using a gesture recognizer to handle events, but I really need the single touch/drag on the overlay view to have no effect on the map view.
Is there a way to accomplish this filtering based on the number of touches? Or a way to disable the single touch gestures on the map view?
The solution to this is actually very simple.
Give the map view a parent view that it fills completely
Give the parent view pan and tap gesture recognizers configured to only respond to one finger touches
On the MKMapView, set the scrollEnabled property to NO (the "Allows Scrolling" checkbox in IB)
The gesture recognizers allow you to get the gestures, and setting scrollEnabled to NO prevents the MapView from swallowing the pan gestures.
Sample project here: https://github.com/Linux-cpp-lisp/sample-no-gesture-mapview

How to make UIView stop receiving touch events?

I'm working on an app where the user is expected to rapidly touch and swipe across multiple UIViews, each of which is supposed to do an action once the user's finger has reached it. I've got a lot of views and so the typical thing to do, where I'd iterate over each view to see if a touch is inside of its bounds, is a no-go - there's just too much lag. Is there any other way to get touch events from one view to another (that is beside the first one)? I thought maybe there is some way to cancel the touch event, but I've searched and so far have come up empty.
One of the big problems I have is that if I implement my touch handling in my view controller, touchesBegan only fires for the first touch - if the user touches something and then, without moving the first finger, taps on something else, that tap is not recorded in either touchesBegan or touchesMoved. But if I implement my touch handling in the UIViews themselves, once a view registers a touch, if the user does not lift their finger up and moves it, the views around the first view do not register the touch. Only if the user lifts his finger and then puts it back down will the surrounding views register the touch.
So my question is, lets say I have two views side by side, my touch handling code is implemented in the views, and I put my finger down on view 1. I then slide my finger over to view 2 - what do I need to do to make view 2 register that touch, which started in view 1 and never "ended"?
Set userInteractionEnabled property of UIView to NO.
view.userInteractionEnabled = NO;
UIView has the following property:
#property(nonatomic, getter=isUserInteractionEnabled) BOOL userInteractionEnabled
Ok, I figured out what was going on. Thing is, I have my views as subviews of a scrollview, which is itself a subview of my main view. With scrollEnabled = NO, I could touch my subviews - but apparently the scrollview was only forwarding me the initial touch event, and all subsequent touches were part of that initial event. Because of that, I had many weird problems such as touching two views one after the other, both would select and highlight, but if I took the first finger off the screen both views would de-select. This was not the desired behavior.
So what I did is I subclassed the scrollview and overrode the touch handling methods to send the events to its first responder, which is its superview, which is the view where I'm doing my touch handling. Now it works!

How to detect touch while presenting views with gesture recognizers?

I have been developing collage like application. In which user can add images, they can scale, move and rotate them. Whenever, I am trying to drag an image which is on top but added earlier than the one behind it, it is not receiving the gesture, rather than the one behind it which is added later gets detected and brought into front. How to resolve this issue?
I am Using this to remove the gesture in second view...
[imageView1 removeGestureRecognizer:GestureInView2];
This are the delegate method of UIGestureRecognizer you can use for your different requirement.
touchesBegan:withEvent:
touchesMoved:withEvent:
touchesEnded:withEvent:
I didn't understand your issue perfectly, But.
I think you have to refer for more detail according to your requirement - https://developer.apple.com/library/ios/documentation/EventHandling/Conceptual/EventHandlingiPhoneOS/GestureRecognizer_basics/GestureRecognizer_basics.html.
I have done it. We need to change the index of subviews if you want to make them receive touches. Changing zposition alone will not help to make the view to receive touches.

how to find where the user tapped on the screen in ios

I want to find where user tapped on MapKit. I cannot find a action to make a connection so i am wondering there is an alternate method for the same. please let me know.
As #Disco S2 says, add the instance of MKMapView as a subview to your view. To know where your user tapped on the map, use this method:
- (CGPoint)convertCoordinate:(CLLocationCoordinate2D)coordinate toPointToView:(UIView *)view
Put the mapkit inside a view and listen for touches in that view. There is then a method to convert the touch in the mapView to a touch in the view and vice versa. I can't remember the method for converting but it will be in the map kit documentation

Resources