MonoTouch

MonoTouch: How to Tell If The User Is Selecting Text or Tapping

Goal

Show and hide a toolbar at the bottom of the view when the user taps.

Why

Several apps, particularly those focus on reading the content in the view, offer a UIToolbar (or a custom component with buttons) at the bottom of the view that shows or hides according to the user’s taps.

What we want to detect is a single tap. Not a swipe. Not drag. Not if the user is selecting text.

How

Using a UITapGestureRecognizer we can handle taps on the UIWebView.

I setup a delegate for it and assign a Selector to handle the event.

From there we need to determine if the user is selecting text or just tapping, and show/hide the toolbar.

The Code

 


Categories: iPhoneMonoTouchXamarinPermalink

MonoTouch: Handle Events from UIWebView

Goal

Handle taps on buttons and links within a UIWebView.

Why

I really like how Readability, Pinterest and other apps hide or altogether don’t use a visible UINavigationBar on appropriate views.

image image

Instead, the view is all about the content. I suspect many are using a UIWebView for the content.

The question then becomes, how does the user navigate the view stack from within a UIWebView.

Readability has a left to right swipe gesture that pulls he page away. I suspect this may be one UIViewController that shows a UIWebView over the top of the other content instead of pushing another view onto the stack. If you take a moment in Readability to pull the article away to partially reveal the list, you can scroll the list and see the article. I could be wrong…of anyone knows different do tell. I’m sure there’s more than one way to do this.

image

In the meantime, I want to add a back image link at the top of my HTML content, and handle that action from the view code.

image

How

UIWebView provides ShouldStartLoad where we can see what the component is about to load. At that point we can intercept the request and take a different action.

The Code:

In the HTML I have a link to the anchor #back like this:

[csharp]

[/csharp]

To load images into the UIWebView from the local resource bundle, I set the base URL of the request to NSBundle.MainBundle.BundleUrl.

When the user touches that link the webViewShouldStartLoad handles it and pops the view controller as desired.


Categories: iPhoneMonoTouchXamarinPermalink
blog comments powered by Disqus