Touchscreen Visualisation on Xamarin iOS

Seems to Have an Invisible Touch

To produce a demonstration video on Android you can turn on screen visualisations from the Developer settings menu and this will shows screen interactions on a screen capture. No such option exists on iOS so you need to write some custom code in your app instead. A couple of solutions exist for native iOS app but to use these from Xamarin would require additional code to wrap. The alternative is to write the code from C# to achieve the same result.

Wrap or Write

My requirements were quite simple, for a first version I only needed to show single touch events and I wanted something which looked visually similar to the Android equivalent. It turns out that the solution is to inherit from UIWindow and add some extra code to capture touch events and draw to the screen. This means a single solution works anywhere in your app as a single Window is used regardless of what views you have. This should work in Xamarin Forms on iOS too by adding similar code to the FinishedLaunching method in your AppDelegate.

When you create a blank Xamarin iOS app it will generate a Main.storyboard for the UI and this is hooked up in the Info.plist manifest so there will be no code in FinishedLaunching to setup the Window and root ViewController. The first step therefore is to un-set this:-

visible-touch-info

Then you can add code in the FinishedLaunching method to create the Window and load the storyboard.

public override bool FinishedLaunching(UIApplication application, NSDictionary launchOptions)
{
	// Set a custom Window which handles touch events
	Window = new InTheHand.TouchWindow(UIScreen.MainScreen.Bounds);

	// Load root view controller from Storyboard into Window.
	// You can't set this from Info.plist as it'll use a regular UIWindow.
	Window.RootViewController = UIStoryboard.FromName("Main", null).InstantiateInitialViewController();
	Window.MakeKeyAndVisible();

	return true;
}

Some solutions to this use graphics to display the touch circle but I’ve gone for straightforward UIKit drawing code with a circular UIBezierPath. The full code for a working demo app is on GitHub here:-

https://github.com/inthehand/VisibleTouch

You can see the effect of this code in this video:-

There are some limitations with this code. It supports single touch gestures only, if you use multiple fingers you’ll get a circle jumping around to the latest event. The circle can disappear quite abruptly, it will probably look better with a simple animation to fade out. I haven’t included code here to turn the visualisations on or off. Chances are you won’t want them on all the time so it would make sense to add a setting somewhere to toggle this. I did this myself using a settings bundle so you could toggle the setting before even running the app. That was interesting in itself and easier to integrate with Xamarin iOS than I expected but that is a topic for another blog post.

If there is significant interest in this I can release this as a NuGet package for easy integration into your Xamarin iOS project. For now you can take the TouchWindow.cs code and drop it into a project and modify the AppDelegate as described above.

One thought on “Touchscreen Visualisation on Xamarin iOS

Comments are closed.