gpt4 book ai didi

ios - 检测变换后的 CAShapeLayer 路径上的点击

转载 作者:行者123 更新时间:2023-11-28 23:26:54 25 4
gpt4 key购买 nike

我在一个 View 上有多个 CAShapeLayers,它们都可以使用 CATransform3D 进行转换。

我找到了一种方法来准确检测这些图层路径上或内部的点击,但它仅在图层未转换时才有效。

我怎样才能让它在所有情况下都能正常工作?

@objc private func userTapped(recognizer: UITapGestureRecognizer) {
let tapLocation = recognizer.location(in: canvas)

for shapeLayer in shapeLayers {
guard let path = shapeLayer.path else { continue }

let tempPath = path.copy(strokingWithWidth: CGFloat(lineWidth * 2), lineCap: .round, lineJoin: .round, miterLimit: .zero)

if tempPath.contains(tapLocation) {
// DO SOMETHING
}
}
}

最佳答案

您需要在变换层自己的几何体中获取点击位置。

@objc private func userTapped(recognizer: UITapGestureRecognizer) {
let canvasLocation = recognizer.location(in: canvas)

for shapeLayer in shapeLayers {
guard let path = shapeLayer.path else { continue }
let shapeLocation = shapeLayer.convert(canvasLocation, from: canvas.layer)

let tempPath = path.copy(strokingWithWidth: CGFloat(lineWidth * 2), lineCap: .round, lineJoin: .round, miterLimit: .zero)

if tempPath.contains(shapeLocation) {
// DO SOMETHING
}
}
}

关于ios - 检测变换后的 CAShapeLayer 路径上的点击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58366027/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com