Eric's Homemade Stuff

An infrequent blog of stuff I make, including music.

Using Touchposé With a Swift Project

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.

Installing Steps

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 git@github.com:toddreed/Touchpose.git

2. Add Touchposé Class

  • Drag these two files from the Finder to your project in Xcode.
    • QTouchposeApplication.h
    • QTouchposeApplication.m

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.h will be created and added to the project.

5. Edit the Bridging Header

  • Import the QTouchposeApplication.h file in the bridging header.
1
2
3
4
5
//
//  Use this file to import your target's public headers that you would like to expose to Swift.
//

#import "QTouchposeApplication.h"

6. Add a main.swift file

  • Create a new Swift file.

  • Name it main.swift and 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 @UIApplicationMain line just above the class statement.
1
2
3
4
import UIKit

//@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

7. Use the QTouchposeApplication subclass

  • Edit the main.swift file. First remove the import Foundation statement.
  • Add the following code to the file.
1
2
3
import UIKit

UIApplicationMain(C_ARGC, C_ARGV, NSStringFromClass(QTouchposeApplication.self), NSStringFromClass(AppDelegate.self))

8. Always show touches

I want my app to always show touches when I’m running it.

  • Add (application as QTouchposeApplication).alwaysShowTouches = true to 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
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

    (application as QTouchposeApplication).alwaysShowTouches = true
    return true
}

9. Done!

Now you should see the touches on the screen when you run the app in the simulator or on a device.

 

 

 

Comments