Skip to content

Instantly share code, notes, and snippets.

@davidortinau
Created February 25, 2014 15:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davidortinau/9211526 to your computer and use it in GitHub Desktop.
Save davidortinau/9211526 to your computer and use it in GitHub Desktop.
UITapGestureRecognizer with UIWebView
// In your ViewDidLoad setup
public override void ViewDidLoad ()
{
base.ViewDidLoad ();
// ... other setup
var tapRecognizer = new UITapGestureRecognizer (this, new Selector ("HandleTapFrom"));
tapRecognizer.NumberOfTapsRequired = 1;
tapRecognizer.Delegate = new GestureDelegate ();
webView.AddGestureRecognizer (tapRecognizer); // my webView is an outlet on a xib
}
// Setup the handler
[Export ("HandleTapFrom")]
public void HandleTapFrom (UITapGestureRecognizer recognizer)
{
Console.WriteLine ("getting taps");
// so the question is how long were we holding it down? Has the user initialized text selection?
var selection = webView.EvaluateJavascript ("window.getSelection().toString()");
Console.WriteLine ("selection {0}", selection);
// in my case I'm going to show/hide a toolbar when tapped
if (selection.Length <= 0) {
if (toolbar.Alpha.Equals (1f)) {
toolbar.Alpha = 0f;
} else {
toolbar.Alpha = 1f;
}
} else {
Console.WriteLine ("user is selecting text");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment