ios - 如何在 UIView 中引用 BezierPath

标签 ios swift uibezierpath uitapgesturerecognizer

我正在尝试检测 UIView 中 BezierPaths 内的 Tap,并发现许多对 containsPoint 方法的引用。但是我似乎无法找到如何从我的 ViewController 中实际引用 BezierPaths。

我已设置:

class func drawSA(frame targetFrame: CGRect = CGRect(x: 0, y: 0, width: 69, height: 107), resizing: ResizingBehavior = .aspectFit, SACountries: [String: ViewController.CountryStruct])
{

    let myPath = UIBezierPath()
    myPath.move(to: CGPoint(x: 32.24, y: 8.61))
    myPath.addLine(to: CGPoint(x: 31.99, y: 8.29))
    myPath.addLine(to: CGPoint(x: 31.78, y: 8.19))
    myPath.close()
}

贝塞尔曲线是在此函数中绘制的,调用者:

override func draw(_ rect: CGRect)

在主 ViewController 中,我有以下函数来检测 UIView 上的点击:

@objc func SATap(sender: UITapGestureRecognizer)
{
    let location = sender.location(in: self.SAView)

    // how do I call containsPoint from here?
}

如何从这里调用 containsPoint?

bezierPaths 在运行时正确绘制。

最佳答案

由于路径是在单独的类中的类函数中创建的,因此我无法将它们直接保存到 View 中。

我通过将 UIBezierPaths 放入字典中然后返回字典来解决这个问题。数组也可以,但这样我可以轻松访问特定路径。

class func drawSA(frame targetFrame: CGRect = CGRect(x: 0, y: 0, width: 71, height: 120), resizing: ResizingBehavior = .aspectFit, SACountries: [String: ViewController.CountryStruct]) -> ([String: UIBezierPath])
{
    var Paths = [String: UIBezierPath]()

    let myPath = UIBezierPath()
    myPath.move(to: CGPoint(x: 32.24, y: 8.61))
    myPath.addLine(to: CGPoint(x: 31.99, y: 8.29))
    myPath.addLine(to: CGPoint(x: 31.78, y: 8.19))
    myPath.close()

    Paths["mP"] = myPath


    let myPath2 = UIBezierPath()
    myPath2.move(to: CGPoint(x: 32.24, y: 8.61))
    myPath2.addLine(to: CGPoint(x: 31.99, y: 8.29))
    myPath2.addLine(to: CGPoint(x: 31.78, y: 8.19))
    myPath2.close()

    Paths["mP2"] = myPath2


    return Paths
}

然后,我使用 View.addLayer 在 View 中创建图层,并在使用字典上的 For In 循环创建图层时向每个图层添加 Layer.name 属性:

for path in Paths.keys.sorted()
    {
        self.layer.addSublayer(CreateLayer(path)) // The Function simply creates a CAShapeLayer
    }

然后我在手势功能中使用了字典:

@objc func ATap(sender: UITapGestureRecognizer)
{
    let location = sender.location(in: self.SAView)
    // You need to Scale Location if your CAShapeLayers are Scaled

    for path in SAView.Paths
    {
        let value = path.value

        value.contains(location)

        if value.contains(location)
        {
            for (index, layer) in SAView.layer.sublayers!.enumerated()
            {
               if layer.name == path.key
                {
                    // Do something when specific layer is Tapped
                }
            }
        }
    }
}

很可能有更好的方法来做到这一点,但它们都有效并且性能良好。

关于ios - 如何在 UIView 中引用 BezierPath,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47023374/

相关文章:

ios - Swift - 在应用程序中打开网页,按钮没有反应

ios - 标题标签文本颜色在 UIButton 中无法正确设置动画

ios - 从 SBJSON 解析器返回的数组无效 (iOS)

ios - ios 中的回调未返回进行身份验证

ios - 宽度动态变化的贝塞尔路径

ios - Swift - 如何在 CAShapeLayer 或 UIBezierPath(ovalInRect : ovalRect)

objective-c - 沿着 UIBezierPath 将 UIImage 分成两部分

ios - 单独的用户类 [解析]

ios - 有没有办法获取 UILabel 文本的 shadowPath?

ios - 如何在不引用铃声音量的情况下播放音频?