Skip to content

Instantly share code, notes, and snippets.

@davidortinau
Created April 29, 2012 01:27
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davidortinau/2523148 to your computer and use it in GitHub Desktop.
Save davidortinau/2523148 to your computer and use it in GitHub Desktop.
Detect Single Tap on UIWebView
namespace App.iOS
{
public partial class PaperView : UIViewController
{
// webView is a UIWebView
// toolbar is a UIToolbar
public override void ViewDidLoad ()
{
base.ViewDidLoad ();
var tapRecognizer = new UITapGestureRecognizer (this, new Selector ("HandleTapFrom"));
tapRecognizer.NumberOfTapsRequired = 1;
tapRecognizer.Delegate = new GestureDelegate ();
webView.AddGestureRecognizer (tapRecognizer);
}
[Export("HandleTapFrom")]
public void HandleTapFrom (UITapGestureRecognizer recognizer)
{
// call this js in the webview to see if we selected anything
var selection = webView.EvaluateJavascript ("window.getSelection().toString()");
if (selection.Length <= 0) {
if (toolbar.Alpha.Equals (1f)) {
toolbar.Alpha = 0f;
} else {
toolbar.Alpha = 1f;
}
} else {
Console.WriteLine ("user is selecting text");
}
}
}
// delegate methods for UITapGestureRecognizer
public class GestureDelegate : UIGestureRecognizerDelegate
{
public override bool ShouldReceiveTouch (UIGestureRecognizer recognizer, UITouch touch)
{
return true;
}
public override bool ShouldBegin (UIGestureRecognizer recognizer)
{
return true;
}
public override bool ShouldRecognizeSimultaneously (UIGestureRecognizer gestureRecognizer, UIGestureRecognizer otherGestureRecognizer)
{
return true;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment