提问人:Victor 提问时间:6/8/2023 最后编辑:Victor 更新时间:6/8/2023 访问量:94
如何更新 svg 图像中的路径坐标 swift
How to update path coordinates in a svg image swift
问:
我有一个 svg 图像,当我触摸图像时,我选择了 shapeLayer 并更改了它的颜色,但是当我转换图像以在设备中移动它时,我需要触摸原始位置来检测 shapeLayer。我正在使用 PocketSVG。
这是我的代码:
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(handleTap(_:)))
view.addGestureRecognizer(tapGesture)
view.isUserInteractionEnabled = true
svgView = SVGImageView.init(contentsOf: svgURl)
paths = SVGBezierPath.pathsFromSVG(at: svgURl)
for (index, path) in paths.enumerated() {
let shapeLayer = CAShapeLayer()
shapeLayer.path = path.cgPath
shapeLayer.fillColor = colors[index].cgColor
gamerCat.addSublayer(shapeLayer)}
var transform = CATransform3DMakeScale(0.4, 0.4, 1.0)
transform = CATransform3DTranslate(transform, 200, 400, 0)
gamerCat.transform = transform
gamerCat.shouldRasterize = false
svgView.layer.addSublayer(gamerCat)
view.addSubview(svgView)
@objc func handleTap(_ gesture: UITapGestureRecognizer) {
print("-----------------------------------")
let location = gesture.location(in: view)
print("Location : \(location)")
rects.removeAll(keepingCapacity: false)
indiceRect.removeAll(keepingCapacity: false)
elementTouched(location: location)
}
func elementTouched(location:CGPoint) {
let shapeLayer = CAShapeLayer()
for (index, path) in paths.enumerated() {
shapeLayer.path = path.cgPath
isRectContained(rectA: shapeLayer.path!.boundingBox, rectB: CGRect(x: location.x, y: location.y, width: 1, height: 1), index: index)
}
var smallestRect = rects.first
for rect in rects.dropFirst() {
if let currentSmallest = smallestRect {
if rect.width * rect.height < currentSmallest.width * currentSmallest.height {
smallestRect = rect
}
} else {
smallestRect = rect
}
}
if let smallestRect = smallestRect {
print("El CGRect más pequeño es: \(smallestRect)")
var elegido = indiceRect[rects.firstIndex(of: smallestRect)!]
shapeLayer.path = paths[elegido].cgPath
shapeLayer.fillColor = UIColor.red.cgColor
} else {
print("Array is empty.")
}
gamerCat.addSublayer(shapeLayer)
}
这就是我所做的,我不知道我是否需要进行转换来移动图像。我尝试使用 svgView.frame 移动,但图像的大小没有改变,我尝试使用 clips.blouns,但我的图像消失了
答: 暂无答案
评论
SVGImageView