在 Swift 中检测到 CAShapeLayer 上的点击?

Detecting a tap on a CAShapeLayer in Swift?

我对 iOS 开发还很陌生(所以请原谅我的无能 - 我到处都看过!),并且正在寻找一种方法来检测 CAShapeLayer 上的点击.到目前为止,我遇到了 hitTesthitTest 是最好的方法吗?如果是,它在 Swift 中如何使用,尤其是在 CAShapeLayer 中?另外,如果我有很多 CAShapeLayers,我将如何使用 hitTest 方法来单独引用它们?

这就是我创建 CAShapeLayer 的方式:

    let newBounds = CGRect(x: 0, y: 0, width: 200, height: 200)
    let newShape = CAShapeLayer()
    newShape.bounds = newBounds
    newShape.position = view.center
    newShape.cornerRadius = newBounds.width / 2
    newShape.path = UIBezierPath(ovalInRect: newShape.bounds).CGPath

    newShape.lineWidth = 42
    newShape.strokeColor = UIColor(red: 222/255.0, green: 171/255.0, blue: 66/255.0, alpha: 1.0).CGColor
    newShape.fillColor = UIColor.clearColor().CGColor

    newShape.strokeStart = 0.2
    newShape.strokeEnd = 0.4

    view.layer.addSublayer(newShape)

使用以下代码获取 CAShapeLayer

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    for (UITouch *touch in touches) {
        CGPoint touchLocation = [touch locationInView:self.view];
        for (id sublayer in self.view.layer.sublayers) {
            BOOL touchInLayer = NO;
            if ([sublayer isKindOfClass:[CAShapeLayer class]]) {
                CAShapeLayer *shapeLayer = sublayer;
                if (CGPathContainsPoint(shapeLayer.path, 0, touchLocation, YES)) {
                    // Here your code for do any opration.
                    touchInLayer = YES;
                }
            } else {
                CALayer *layer = sublayer;
                if (CGRectContainsPoint(layer.frame, touchLocation)) {
                    // Touch is in this rectangular layer
                    touchInLayer = YES;
                }
            }
        }
    }
}

我认为这是实现您想要实现的目标的最佳方式:

// First add the shapelayer
let layer = CAShapeLayer()
layer.anchorPoint = CGPointZero
layer.path = UIBezierPath(ovalInRect: CGRect(x: 0, y: 0, width: 100, height: 200)).CGPath
layer.bounds = CGPathGetBoundingBox(layer.path) // IMPORTANT, without this hitTest wont work
layer.fillColor = UIColor.redColor().CGColor
self.view.layer.addSublayer(layer)


// Check for touches
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
    let point = touches.anyObject()!.locationInView(self.view) // Where you pressed

    if let layer = self.view.layer.hitTest(point) as? CAShapeLayer { // If you hit a layer and if its a Shapelayer
        if CGPathContainsPoint(layer.path, nil, point, false) { // Optional, if you are inside its content path
            println("Hit shapeLayer") // Do something
        }
    }
}

Swift 4 & 5

我的 UIImageView 有多个嵌入的 CAShapeLayer 对象。这是我如何能够检测到它们的水龙头。引用自 this tutorial.

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    let touch = touches.first

    guard let point = touch?.location(in: imageView) else { return }
    guard let sublayers = imageView.layer.sublayers as? [CAShapeLayer] else { return }

    for layer in sublayers {
        if let path = layer.path, path.contains(point) {
            print(layer)
        }
    }
}

我使用了 Arbitur 的代码,但出现了一些错误。 这是我没有错误的代码。对于 swift 3.2 / 4.0

override func viewDidLoad() {
    super.viewDidLoad()

    let layer = CAShapeLayer()
    layer.anchorPoint = CGPoint.zero
    layer.path = UIBezierPath.init(ovalIn: CGRect(x: 0, y: 0, width: 100, height: 200)).cgPath
    layer.bounds = (layer.path?.boundingBox)! // IMPORTANT, without this hitTest wont work
    layer.fillColor = UIColor.red.cgColor
    self.view.layer.addSublayer(layer)

}
    // Check for touches

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    let point = touches.first?.location(in: self.view) // Where you pressed

    if let layer = self.view.layer.hitTest(point!) as? CAShapeLayer { // If you hit a layer and if its a Shapelayer
        if (layer.path?.contains(point!))! { // Optional, if you are inside its content path
            print("Hit shapeLayer") // Do something
        }
    }
}