I am trying to use several UIGestureReconizers with MKMapView , on which the user can reset the pin and drag it. He has a callout . I implement this in the TabbarController as well as in the NavigationController . I currently have:
1) A PanGestureRecognizer animates the Tabbar and Navigation elements from the screen. This works great without interfering with panning the map.
2) A TapGestureRecognizer , installed with one click, animates two elements from 1) back to the screen.
3) A TapGestureRecognizer , installed on two cranes, allows you to use the basic zoom functionality of MKMapView . This GestureRecognizer's delegate has a gestureRecognizer. shouldRecognizeSimultaneouslyWithGestureRecognizer set to true
These are the settings in viewDidLoad as follows:
My problem arises when I try to implement a UILongPressGestureRecognizer to allow pin output to the map. I am trying to use the following in viewDidLoad :
// This sets up the long tap to drop the pin. let longTap: UILongPressGestureRecognizer = UILongPressGestureRecognizer(target: self, action: "didLongTapMap:") longTap.delegate = self longTap.numberOfTapsRequired = 0 longTap.minimumPressDuration = 0.5 mapView.addGestureRecognizer(longTap)
This is my action method:
func didLongTapMap(gestureRecognizer: UIGestureRecognizer) { // Get the spot that was tapped. let tapPoint: CGPoint = gestureRecognizer.locationInView(mapView) let touchMapCoordinate: CLLocationCoordinate2D = mapView.convertPoint(tapPoint, toCoordinateFromView: mapView) var viewAtBottomOfHierarchy: UIView = mapView.hitTest(tapPoint, withEvent: nil) if let viewAtBottom = viewAtBottomOfHierarchy as? MKPinAnnotationView { return } else { if .Began == gestureRecognizer.state { // Delete any existing annotations. if mapView.annotations.count != 0 { mapView.removeAnnotations(mapView.annotations) } annotation = MKPointAnnotation() annotation.coordinate = touchMapCoordinate mapView.addAnnotation(annotation) _isPinOnMap = true findAddressFromCoordinate(annotation.coordinate) updateLabels() } } }
This really allows the pin to be pushed out on a long crane, and one crane displays the leader, and the second crane for holding and dragging causes the second pin to fall if the drag does not start quickly enough. This second contact falls into the space in which the previous pin was located, and it can be dragged by the user, but the finger is awkward and wrong with the new pin.
I am trying to use the line:
if let viewAtBottom = viewAtBottomOfHierarchy as? MKPinAnnotationView {
to return the MKMapView faucet and prevent another pin from being reset, but the return will never be called, although the breakpoint on this line indicates that viewAtBottom is of type MapKit.MKPinAnnotationView . Any ideas I'm wrong about?