检测点击转换后的 CAShapeLayer 路径
Detect tap on a transformed CAShapeLayer’s path
我在一个视图上有多个 CAShapeLayer,它们都可以使用 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
}
}
}
我在一个视图上有多个 CAShapeLayer,它们都可以使用 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
}
}
}