Need event when UIWebView scrolls and option to cancel the scroll

We are building an app using xamarin.forms, which has a certain page that displays a webview. In the IOS version of the app, this webview is an UIWebView control. What I need, is a way to catch touch start, move and end events on the UIWebView control and be able to calculate the distance that the touch has moved. Furthermore, i need to be able to cancel the touchmove event from firing in the UIWebView, so the content doesn't scroll until I want it to.

I've already accomplished this in the android version by using the Touch eventhandler of the android webview. It can do exactly what i want, and the touch event has a "handled" property that can be used to avoid the event from triggering in the webview.

I've found all kinds ways to catch scroll events. The most promising seems to be implementing a gestureRecognizer to the UIWebView's scrollview. This way i can catch touch start, move and end events. However, I have a few problems with this implementation. During touchmove, the events seem to stop firing. The touchend doesn't fire at all most of the time. Furthermore, I cannot find a way to cancel the event when i want it to be canceled.

The way i currently attach the gesture recognizer:

var test = new MyRecognizer();
webView.ScrollView.AddGestureRecognizer(test);

The gesture recognizer:

public class MyRecognizer : UIGestureRecognizer
{
    private nfloat _startY;

    public MyRecognizer()
    {
        this.CancelsTouchesInView = true;
    }

    public override void TouchesBegan(NSSet touches, UIEvent evt)
    {
        base.TouchesBegan(touches, evt);
        var location = (touches.AnyObject as UITouch).LocationInView(View);
        _startY = location.Y;
    }

    public override void TouchesMoved(NSSet touches, UIEvent evt)
    {
        base.TouchesMoved(touches, evt);
        var location = (touches.AnyObject as UITouch).LocationInView(View);
        var intCurrentY = location.Y;

        if(someCondition)
        {
            //cancel the event somehow to prevent the webview from scrolling

            //a post mentioned this should do the trick, but it doesnt work:
            this.Enabled = false;
            this.Enabled = true;
        }
    }

    public override void TouchesEnded(NSSet touches, UIEvent evt)
    {
        base.TouchesEnded(touches, evt);
    }
}

Am I on the right track here? Why do the events stop firing? And how can I cancel the event to prevent the webview from scrolling in SOME cases (I don't want to ALWAYS disable scrolling on webview)

Tagged:

Posts

Sign In or Register to comment.