I’m working on a project where the user interface has a lot of gestures. I want the touches to show when I’m giving demos and in demo videos. The library I found which does this well is Touchposé. The only issue is that it is an Objective-C class and my project is in Swift. I got it to work, but I could not find a single source for using Touchposé in a Swift project. This post is my attempt to supply such a resource to anyone else looking to do this. This includes me in the future.
1. Clone Touchposé
There are other ways to get the code but this is the most common.
- Clone the Touchposé repository with this command
$ git clone firstname.lastname@example.org:toddreed/Touchpose.git
2. Add Touchposé Class
- Drag these two files from the Finder to your project in Xcode.
3. Copy Option
Most likely you should choose the option to copy the files to your project.
- Check the Copy items if needed checkbox.
4. Create a Bridging Header
If your project does not already have a bridging header then this dialog will appear when you drag Objective-C files into it.
- Click on the Yes button and a file named
<project-name>-Bridging-Header.hwill be created and added to the project.
5. Edit the Bridging Header
- Import the
QTouchposeApplication.hfile in the bridging header.
1 2 3 4 5
6. Add a
- Create a new Swift file.
- Name it
main.swiftand save the file into the project. Once you have this file in your project it becomes the starting point of the app. If you try to compile right now you will get this error in the AddDelegate.swift file. The delegate file is no longer the top-level file.
'UIApplicationMain' attribute cannot be used in a module that contains top-level code
- Fix this issue by commenting out the
@UIApplicationMainline just above the class statement.
1 2 3 4
7. Use the QTouchposeApplication subclass
- Edit the
main.swiftfile. First remove the
- Add the following code to the file.
1 2 3
8. Always show touches
I want my app to always show touches when I’m running it.
(application as QTouchposeApplication).alwaysShowTouches = trueto the AppDelegate.swift file in this method. You could also change the type in the method declaration but this way makes it easier to back all this code out when you need to.
1 2 3 4 5
Now you should see the touches on the screen when you run the app in the simulator or on a device.