0
我正在使用SVGKit在我的應用程序中顯示不同的SVG內容。要求的功能之一是檢測svg路徑內的區域內發生的水龍頭,並填充一些顏色。CGPathContainsPoint相對於絕對座標iOS
CGPathContainsPoint似乎是這裏的合適解決方案,但問題是我的svg路徑是用相對座標設置的(而tap point在整個圖像區域內是絕對的)。
下面是一些代碼,我使用的是:
class ViewController: UIViewController {
@IBOutlet weak var imageArea: UIView!
var iv : SVGKLayeredImageView!
let im = SVGKImage(named: "connections.svg")
override func viewDidLoad() {
super.viewDidLoad()
im.size = self.imageArea.frame.size
iv = SVGKLayeredImageView(SVGKImage: im)
let recognizer = UITapGestureRecognizer(target: self, action:#selector(ViewController.handleTap(_:)))
iv.addGestureRecognizer(recognizer)
self.imageArea.addSubview(iv)
}
func handleTap(recognizer: UITapGestureRecognizer) {
let point = recognizer.locationInView(iv)
changeFillColorRecursively(iv.layer.sublayers!, color: UIColor.redColor(), point: point)
}
func changeFillColorRecursively(sublayers: [AnyObject], color: UIColor, point: CGPoint) {
for layer in sublayers {
if let l = layer as? CAShapeLayer {
if CGPathContainsPoint(l.path, nil, point, false) {
l.fillColor = color.CGColor
}
}
if let l = layer as? CALayer, sub = l.sublayers {
changeFillColorRecursively(sub, color: color, point: point)
}
}
}
}
灌裝代碼的工作,不過分的比較是錯誤的,導致沒有填充或多個區域。
我的想法是,我應該玩CGPathContainsPoint func affineTransform參數,但我真的不知道如何正確設置轉換矩陣。