具有3种颜色描边路径的自定义clusterAnnotationView

时间:2019-01-21 23:03:05

标签: ios swift mapkit

如Apple的示例代码中所示,如何获得比率为三种不同类型而不是两种的自定义聚类注释视图?

如何在圆形的簇图像上显示三个不同的笔划路径(轮廓为3色的圆环/笔划路径)?

查看示例:

Apple,TANDm-App“整理地图”,WWDC2017No237与 单轮车,自行车和三轮车作为单个注释。 他们只作为UIImage做比例的自行车和三轮车。

问题:

如何在功能“ drawRatio”中获取附加的piePath-“ unicycleColor”作为自定义clusterAnnotationView?

TANDm-应用-代码

ClusterAnnotationView.swift类:

import MapKit

/// - Tag: ClusterAnnotationView
class ClusterAnnotationView: MKAnnotationView {

override init(annotation: MKAnnotation?, reuseIdentifier: String?) {
    super.init(annotation: annotation, reuseIdentifier: reuseIdentifier)
    collisionMode = .circle
    centerOffset = CGPoint(x: 0, y: -10) // Offset center point to animate better with marker annotations
}

required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

/// - Tag: CustomCluster
override func prepareForDisplay() {
    super.prepareForDisplay()

    if let cluster = annotation as? MKClusterAnnotation {
        let totalBikes = cluster.memberAnnotations.count

        if count(cycleType: .unicycle) > 0 {
            image = drawUnicycleCount(count: totalBikes)
        } else {
            let tricycleCount = count(cycleType: .tricycle)
            image = drawRatioBicycleToTricycle(tricycleCount, to: totalBikes)
        }

        if count(cycleType: .unicycle) > 0 {
            displayPriority = .defaultLow
        } else {
            displayPriority = .defaultHigh
        }
    }
}

private func drawRatioBicycleToTricycle(_ tricycleCount: Int, to totalBikes: Int) -> UIImage {
    return drawRatio(tricycleCount, to: totalBikes, fractionColor: UIColor.tricycleColor, wholeColor: UIColor.bicycleColor)
}

private func drawUnicycleCount(count: Int) -> UIImage {
    return drawRatio(0, to: count, fractionColor: nil, wholeColor: UIColor.unicycleColor)
}
// func zeichneVerhältnis(Farbelement: Ganzzahl, zum Gesamtkreis: Ganzzahl, FragmentFarbwahl, GesamtFarbwahl) Rückgabewert Standardfarbe { let Vorausberechnung der Breite und Höhe sowie Rückgabe des berechneten Images { Füllung des Kreisen = wholeColor, Farbsegment auf Pie übertragen,

 private func drawRatio(_ fraction: Int, to whole: Int, fractionColor: UIColor?, wholeColor: UIColor?) -> UIImage {
    let renderer = UIGraphicsImageRenderer(size: CGSize(width: 40, height: 40))
    return renderer.image { _ in
        // Fill full circle with wholeColor
        wholeColor?.setFill()
        UIBezierPath(ovalIn: CGRect(x: 0, y: 0, width: 40, height: 40)).fill()

        // Fill pie with fractionColor
        fractionColor?.setFill()
        let piePath = UIBezierPath()
        piePath.addArc(withCenter: CGPoint(x: 20, y: 20), radius: 20,
                       startAngle: 0, endAngle: (CGFloat.pi * 2.0 * CGFloat(fraction)) / CGFloat(whole),
                       clockwise: true)
        piePath.addLine(to: CGPoint(x: 20, y: 20))
        piePath.close()
        piePath.fill()

        // Fill inner circle with white color
        UIColor.white.setFill()
        UIBezierPath(ovalIn: CGRect(x: 8, y: 8, width: 24, height: 24)).fill()

        // Finally draw count text vertically and horizontally centered
        let attributes = [ NSAttributedString.Key.foregroundColor: UIColor.black,
                           NSAttributedString.Key.font: UIFont.boldSystemFont(ofSize: 20)]
        let text = "\(whole)"
        let size = text.size(withAttributes: attributes)
        let rect = CGRect(x: 20 - size.width / 2, y: 20 - size.height / 2, width: size.width, height: size.height)
        text.draw(in: rect, withAttributes: attributes)
    }
}

private func count(cycleType type: Cycle.CycleType) -> Int {
    guard let cluster = annotation as? MKClusterAnnotation else {
        return 0
    }

    return cluster.memberAnnotations.filter { member -> Bool in
        guard let bike = member as? Cycle else {
            fatalError("Found unexpected annotation type")
        }
        return bike.type == type
    }.count
}
}

在tat函数中是否有更接近于其他piePath / strokePath的想法,提示或解决方案?

0 个答案:

没有答案