Add gesture recognizer to BezierPath created by PaintCode

380 views Asked by At

I have no idea about the following situation:

I had exported a NSObject from PaintCode and made a .swift file (someObject.swift).

public class PlanATrip: NSObject {

    class func drawRectangle1(frame targetFrame: CGRect = CGRect(x: 0, y: 0, width: 200, height:100), resizing: ResizingBehavior = .aspectFit) {

    ....

}

I also overrode the draw() function in a UIView (someObjectView.swift).

So how can add a gesture recognizer to a bezierPath (for example, a rectangle1 = UIBezierPath(...) ) which is in the someObject.swift ?

I tried to add some functions like:

let tapGestureA:UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(touchAction))

However, the scope confused me; like if I put the touchAction function out of the drawRectangle1 function, I will not be able to access rectangle1.

How can I modify to make such a gesture recognizer work?

2

There are 2 answers

1
backslash-f On BEST ANSWER

You are trying to manipulate UIBezierPath objects generated by PaintCode directly, which isn't PaintCode's purpose.

What you are trying to achieve is possible, but not without some hacking.
For example: How to access a layer inside UIView custom class generate with Paintcode?

In my opinion, you are "misusing" PaintCode.

Instead, you would be much better adding the tapping logic inside your custom UIView. The thing you called someObjectView.swift.
For example: https://github.com/backslash-f/paint-code-ui-button

In case you really need to check if gestures occur inside the boundaries of a UIBezierPath then you need to create a public reference to it (e.g.: a var that points to it outside of the drawRectangle1 function) and finally use the code proposed by @Sparky.

The main problem with this approach is that your changes will be overwritten every time you use PaintCode's export feature. I wouldn't recommend it.

0
Marcus On

If I understand your question correctly, you'd like to add a UITapGestureRecognizer that will recognize a gesture within a portion of your view defined by a UIBezierPath.

In this case, assign the selector to the gesture recognizer as you have in your question, then in the body of touchAction(recognizer:), test whether the gesture lies within the UIBezierPath. The simple case where you only care if the gesture is inside the UIBezierPath might be handled as follows:

func touchAction(recognizer: UITapGestureRecognizer) -> Void {
           guard rectangle1.contains(recognizer.location(in: self)) else { return }

           // Execute your code for the gesture here

           // ...

}