Swift-检测NSMutableAttributedString上的点击

时间:2018-10-04 11:07:45

标签: ios swift nsattributedstring uibezierpath

无论如何,我可以在NSMutableAttributedString上检测到点击。我有一个森伯斯特图表。单击每个元素后,我想显示一个警报并将该值保存在Core Data中。现在,我只发布了部分代码,因为原始代码太长而无法阅读。我知道我无法在UIBezierPath上显示警报,因为它是为一组元素绘制在一起的,所以我别无选择,只能在NSMutableAttributedString上显示警报。

override func draw(_ rect: CGRect) {
    let context = UIGraphicsGetCurrentContext()!
    /// Group 5
    do {
        context.saveGState()
        context.translateBy(x: 337.7, y: 334.39)
        context.rotate(by: -1654 * CGFloat.pi/180)
        context.translateBy(x: -321.5, y: -318)

        /// Group 4
        do {
            context.saveGState()

            /// Group 8
            do {
                context.saveGState()
                context.translateBy(x: 0.35, y: 0.03)

                /// Path
                let path8 = UIBezierPath()
                path8.move(to: CGPoint(x: 0, y: 32.29))
                path8.addCurve(to: CGPoint(x: 141.21, y: 0), controlPoint1: CGPoint(x: 42.61, y: 11.61), controlPoint2: CGPoint(x: 90.54, y: 0))
                path8.addCurve(to: CGPoint(x: 213.67, y: 8.12), controlPoint1: CGPoint(x: 166.13, y: 0), controlPoint2: CGPoint(x: 190.39, y: 2.81))
                path8.addLine(to: CGPoint(x: 196.1, y: 78.67))
                path8.addCurve(to: CGPoint(x: 137.83, y: 71.74), controlPoint1: CGPoint(x: 177.42, y: 74.14), controlPoint2: CGPoint(x: 157.9, y: 71.74))
                path8.addCurve(to: CGPoint(x: 28.81, y: 97.08), controlPoint1: CGPoint(x: 98.69, y: 71.74), controlPoint2: CGPoint(x: 61.69, y: 80.85))
                path8.addLine(to: CGPoint(x: 0, y: 32.29))
                path8.close()
                context.saveGState()
                context.translateBy(x: 179.79, y: 0)
                path8.usesEvenOddFillRule = true
                UIColor(hue: 0.133, saturation: 0.137, brightness: 1, alpha: 1).setFill()
                path8.fill()
                path8.lineWidth = 1
                UIColor(white: 0.592, alpha: 1).setStroke()
                path8.stroke()
                context.restoreGState()

                /// Path
                let path10 = UIBezierPath()
                path10.move(to: CGPoint(x: 77.66, y: 347.23))
                path10.addLine(to: CGPoint(x: 11.79, y: 310.67))
                path10.addCurve(to: CGPoint(x: 44.49, y: 187.63), controlPoint1: CGPoint(x: 32.59, y: 274.44), controlPoint2: CGPoint(x: 44.49, y: 232.43))
                path10.addCurve(to: CGPoint(x: 0, y: 46.03), controlPoint1: CGPoint(x: 44.49, y: 134.96), controlPoint2: CGPoint(x: 28.04, y: 86.13))
                path10.addLine(to: CGPoint(x: 61.01, y: 0))
                path10.addCurve(to: CGPoint(x: 121.96, y: 186.18), controlPoint1: CGPoint(x: 99.35, y: 52.3), controlPoint2: CGPoint(x: 121.96, y: 116.63))
                path10.addCurve(to: CGPoint(x: 77.66, y: 347.23), controlPoint1: CGPoint(x: 121.96, y: 244.96), controlPoint2: CGPoint(x: 105.81, y: 300.01))
                path10.close()
                context.saveGState()
                context.translateBy(x: 520.04, y: 131.32)
                path10.usesEvenOddFillRule = true
                UIColor(hue: 0.582, saturation: 0.882, brightness: 1, alpha: 1).setFill()
                path10.fill()
                path10.lineWidth = 1
                UIColor(white: 0.592, alpha: 1).setStroke()
                path10.stroke()
                context.restoreGState()

                /// Path
                let path12 = UIBezierPath()
                path12.move(to: CGPoint(x: 202.13, y: 121.2))
                path12.addLine(to: CGPoint(x: 141.15, y: 167.21))
                path12.addCurve(to: CGPoint(x: 0, y: 70.57), controlPoint1: CGPoint(x: 107.58, y: 119.98), controlPoint2: CGPoint(x: 57.83, y: 85.06))
                path12.addLine(to: CGPoint(x: 17.57, y: 0))
                path12.addCurve(to: CGPoint(x: 202.13, y: 121.2), controlPoint1: CGPoint(x: 92.8, y: 17.64), controlPoint2: CGPoint(x: 157.78, y: 61.47))
                path12.close()
                context.saveGState()
                context.translateBy(x: 377.77, y: 8.56)
                path12.usesEvenOddFillRule = true
                UIColor(hue: 0.208, saturation: 0.328, brightness: 0.91, alpha: 1).setFill()
                path12.fill()
                path12.lineWidth = 1
                UIColor(white: 0.592, alpha: 1).setStroke()
                path12.stroke()
                context.restoreGState()

                context.restoreGState()
            }

            context.restoreGState()
        }


        /// Geranium
        let geranium = NSMutableAttributedString(string: "Geranium")
        geranium.addAttribute(.font, value: UIFont(name: "Helvetica-Bold", size: 10)!, range: NSRange(location: 0, length: geranium.length))
        context.saveGState()
        context.translateBy(x: 130.07, y: 523.08)
        context.rotate(by: 490 * CGFloat.pi/180)
        context.translateBy(x: -23.5, y: -6)
        geranium.draw(at: CGPoint.zero)
        context.restoreGState()

        /// Lavender
        let lavender = NSMutableAttributedString(string: "Lavender")
        lavender.addAttribute(.font, value: UIFont(name: "Helvetica-Bold", size: 10)!, range: NSRange(location: 0, length: lavender.length))
        context.saveGState()
        context.translateBy(x: 81.5, y: 464.82)
        context.rotate(by: 872 * CGFloat.pi/180)
        context.translateBy(x: -22.5, y: -6)
        lavender.draw(at: CGPoint.zero)
        context.restoreGState()
    }
}

输出:

Output

1 个答案:

答案 0 :(得分:4)

您无法检测到绘制的NSAttributedString上的轻敲,因为tap是UIKit层的抽象,而rect则不能。因此,您可以使用hitTest(_:with:)进行抽头检测,并使用其坐标与NSAttributedString的坐标进行比较。