Detecting a tap on a CAShapeLayer in Swift?

前端 未结 4 576
时光取名叫无心
时光取名叫无心 2020-12-29 13:18

I\'m quite new to iOS Development (so please forgive my ineptitude - I\'ve looked everywhere!), and was looking to find a way to detect a tap on a CAShapeLayer.

4条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-29 13:43

    Use following code for get touch of 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;
                    }
                }
            }
        }
    }
    

提交回复
热议问题