Created
April 29, 2012 01:27
-
-
Save davidortinau/2523148 to your computer and use it in GitHub Desktop.
Detect Single Tap on UIWebView
This file contains hidden or 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
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