Mystogan
Mystogan

Reputation: 577

Xamarin WebView iOS detect Scrolling event

Setup: I'm using Xamarin (and Xamarin.Forms) developing an app with an embedded browser.

My issue: I have a WebView and I'm trying to detect when a user is scrolling, and whether it is Up or Down.

So far, I have created a Custom WebViewRenderer for the iOS-project. But how does one subscribe to scrolling events? Googling makes me believe that overriding the WebViewRenderer's

OnElementChanged(VisualElementChangedEventArgs e)

should be the right way, but I can't seem to find any documentation/leads on how to go further. Does anyone know, or can point me in the right direction?

Upvotes: 2

Views: 1119

Answers (1)

SushiHangover
SushiHangover

Reputation: 74184

Both MKWebView and UIWebView contain a UIScrollView that you can assign a UIScrollViewDelegate to its ScrollView.Delegate property and track scrollViewDidScroll to determine the panning direction:

public class UIScrollViewDelegate : NSObject, IUIScrollViewDelegate
{
    [Export("scrollViewDidScroll:")]
    public void Scrolled(UIScrollView scrollView)
    {
        var translation = scrollView.PanGestureRecognizer.TranslationInView(scrollView.Superview);
        Console.WriteLine($"Scrolling {(translation.Y > 0 ? "Down" : "Up")}");
    }
}

Upvotes: 3

Related Questions