0

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?

Tamás Sengel
  • 55,884
  • 29
  • 169
  • 223
Caspar
  • 21
  • 1
  • What is you final goal? Would you like to create a custom button? I'm asking because I see you are trying to add a "UITapGestureRecognizer". If that's the case (you would like to create a custom button), the answer is very straightforward (I can provide more details). Just wanted to confirm what your final goal is first. – backslash-f Sep 08 '17 at 23:04
  • @backslash-f I draw a bezierPath and want to add a TapGestureRecognizer to it. I'm not quite sure it is a "custom button". – Caspar Sep 09 '17 at 01:13
  • I see, I got the picture. Please refer to my answer below. Cheers! – backslash-f Sep 09 '17 at 02:30

2 Answers2

0

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

           // ...

}
Marcus
  • 2,153
  • 2
  • 13
  • 21
0

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.

backslash-f
  • 7,923
  • 7
  • 52
  • 80