Subclassing UIGestureRecognizer with Swift and Xco

2019-02-16 07:15发布

You can't write to self.state in the subclass unless you import UIGestureRecognizerSubclass.h as indicated here.

In a Swift environment, I'm confused how I'd go about importing this. I tried import UIGestureRecognizerSubclass.h, and without the .h, but I still can't write to self.state.

How would I accomplish this?

2条回答
我只想做你的唯一
2楼-- · 2019-02-16 07:48

You need to have or create a -Bridging-Header.h file to import objc headers such as the one you want. The import line looks like this:

#import <UIKit/UIGestureRecognizerSubclass.h>

If you don't already have a bridge header file in your app, the easiest way to get one is to add an objc class to your project, and xcode will ask if you want one, then creates the file and ties it into the settings for you. You can then delete the objc class.

Everything in that header file is automatically made available to your Swift code, no need to add any import lines in your swift files.

查看更多
小情绪 Triste *
3楼-- · 2019-02-16 07:54

The Swift equivalent is simply:

import UIKit.UIGestureRecognizerSubclass

That imports the appropriate header.

查看更多
登录 后发表回答