SomaMan
SomaMan

Reputation: 4164

Monotouch MKMapView UIGestureRecognizer problems

I added 3 gesture recognizers to my MapView in IB, a long press, a pan & a pinch. Their delegate is the file's owner. I set them up like so -

public override void ViewDidLoad ()
{
        base.ViewDidLoad ();

        PanGestureRecognizer.AddTarget(s => { Console.WriteLine("Pan"); } );
        LongPressGestureRecognizer.AddTarget(s => { Console.WriteLine("Long press"); } );
        PinchGestureRecognizer.AddTarget(s => { Console.WriteLine("Pinch"); } );

}

I also implement this -

public bool ShouldRecognizeSimultaneously (UIGestureRecognizer gestureRecognizer, UIGestureRecognizer otherGestureRecognizer)
{
    return true;
}

The problem is, only the Long Press gesture recognizer does anything, the others are completely ignored.

Any ideas/suggestions welcome!

Upvotes: 3

Views: 360

Answers (1)

SomaMan
SomaMan

Reputation: 4164

Being fairly new to Monotouch, I didn't realise that when I set the delegate of the MapView in IB to my ViewController, that wouldn't actually work. I needed to create a delegate which is a subclass of UIGestureRecognizerDelegate, and set the delegate of the gestureRecognizer to this, and I added the gestureRecognizer programmatically (though that's probably not necessary) -

private class GestureRecognizerDelegate : UIGestureRecognizerDelegate
{
    public override bool ShouldRecognizeSimultaneously (UIGestureRecognizer gestureRecognizer, UIGestureRecognizer otherGestureRecognizer)
    {
        return true;
    }
}


public override void ViewDidLoad ()
{
    base.ViewDidLoad ();

    UIPinchGestureRecognizer pinchGestureRecognizer = new UIPinchGestureRecognizer(s => { /* do stuff here */ } );

    GestureRecognizerDelegate gestureRecognizerDelegate = new GestureRecognizerDelegate();
    pinchGestureRecognizer.Delegate = gestureRecognizerDelegate;

    MapView.AddGestureRecognizer(pinchGestureRecognizer);
}

Then, by setting the ZoomEnabled property of the MapView to false, I can control how the map zooms (in my case, I had to prevent the map zooming in beyond a certain threshold, my client wasn't happy with the way you could zoom in & it would then bounce back out to my preset value, which I had working using RegionChanged in the MapView delegate). Don't you love clients!

Upvotes: 2

Related Questions