简体   繁体   English

将手势识别器添加到PaintCode创建的BezierPath中

[英]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 ). 我已经从PaintCode导出了一个NSObject并制作了一个.swift文件( 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 ). 我还覆盖了UIViewsomeObjectView.swift )中的draw()函数。

So how can add a gesture recognizer to a bezierPath (for example, a rectangle1 = UIBezierPath(...) ) which is in the someObject.swift ? 那么,怎样才能添加一个手势识别的bezierPath(例如, rectangle1 = UIBezierPath(...)这是在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 . 就像我将touchAction函数从drawRectangle1函数中移出一样,我将无法访问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. 如果我正确理解了您的问题,则您想添加一个UITapGestureRecognizer,它可以识别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. 在这种情况下,将问题中的选择器分配给手势识别器,然后在touchAction(recognizer :)的正文中测试手势是否位于UIBezierPath中。 The simple case where you only care if the gesture is inside the UIBezierPath might be handled as follows: 您只关心手势是否在UIBezierPath内部的简单情况可以按以下方式处理:

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. 您正在尝试直接操作UIBezierPath生成的UIBezierPath对象,这不是PaintCode的目的。

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? 例如: 如何访问用Paintcode生成的UIView自定义类中的图层?

In my opinion, you are "misusing" PaintCode. 我认为您在“滥用” PaintCode。

Instead, you would be much better adding the tapping logic inside your custom UIView . 相反,您最好在自定义UIView添加点击逻辑。 The thing you called someObjectView.swift . 您将其称为someObjectView.swift
For example: https://github.com/backslash-f/paint-code-ui-button 例如: 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. 如果确实需要检查手势是否在UIBezierPath的边界内发生,则需要创建对其的公共引用(例如:在drawRectangle1函数外部指向它的var ),最后使用@Sparky建议的代码。

The main problem with this approach is that your changes will be overwritten every time you use PaintCode's export feature. 这种方法的主要问题是,每次使用PaintCode的导出功能时,所做的更改都会被覆盖 I wouldn't recommend it. 我不推荐它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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