Created
February 25, 2014 15:48
-
-
Save davidortinau/9211526 to your computer and use it in GitHub Desktop.
UITapGestureRecognizer with UIWebView
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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