简体   繁体   中英

Add gesture recognizer to BezierPath created by PaintCode

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?

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

           // ...

}

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 (eg: 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM