picard-facepalm

Captain on the bridge

How to fix “Method does not override” when subclassing UIGestureRecognizer

So I tried to create an UI Element in Swift that responds to a specific Gesture. Easy enough let’s look up how that works. The almighty Internet has answers for everything:

Subclass UIGestureRecognizer and override the methods touchesBegan, touchesMoved and touchesEnded.

override func touchesBegan - Method does not override any method from its superclass
Method does not override any method from its superclass – wat?

Okay, I can do that – but wait, what?

It seems like the most recent version of Swift is missing some public declarations for those override-able methods. Probably because access control has just landed for Xcode/Swift the other day. At least Jared Sinclair supports this theory.

He also offers a Solution: Import UIGestureRecognizerSubclass.h in your Bridging-Header. I am new to this, how do you do that? Turns out Xcode creates the Bridging-Header file for you if you create an Objective-C file in your project. Due to some back and forth I learned that it unfortunately only does that once. If you ever deleted the file it will not come back. So how do you create it manually?

  1. Create a new Objective-C Header file and name it <projectName>-Bridging-Header.h
  2. Go to your Project Root and then find “Build Settings”
  3. Scroll all the way down and locate the “Swift Compiler – Code Generation” section
  4. Insert the path to your header file into the “Objective-C Bridging Header” field (Notice that the file might be in the root folder [like mine] or somewhere else)
  5. Set “Install Objective-C Compatibility Header” to true
Objective-C Bridging Header
My Settings for the Bridging Header

Back to the actual solution. Insert this one line into the header file:

https://gist.github.com/552655716a2016b11566

And voila – it works. Suddenly all the desired Methods are avaiable. Well, that was simple.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>