您将需要使用包含 CALayer 的 UIView 来处理触摸事件,CALayer 没有内置的触摸事件。- (CALayer *)hitTest:(CGPoint)thePoint
返回触摸事件点所在的“最深”CALayer。因此,如果您调用[self.layer hitTest:point]
它,它将检查您的所有子层并返回正确的CALayer
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
CGPoint point = [[touches anyObject] locationInView:self];
CALayerSubclass *taplayer = [self.layer hitTest:point]
NSArray *points = [taplayer getPoints];
}
没有办法从 CGPath 中取出你输入的点。你能做的最好的就是这些从路径中获取信息的方法。所以,就像你说的那样,你最好的办法是继承 CALayer 并将你需要的所有信息放在一个数据结构中以便以后检索。
// .h
@interface CALayerSubclass : CALayer
@property (nonatomic, strong) NSMutableArray *points;
@end
// .m
-(void)drawInContext:(CGContextRef)ctx {
...
[bezierPath addCurveToPoint:controlPoint1:point1 controlPoint2:point2];
[points addObject:[NSValue valueWithCGPoint:point1]];
[points addObject:[NSValue valueWithCGPoint:point2]];
...
}
只需将所有 CGPoints(或大多数其他核心图形结构)存储在 NSValue 中并将其放入数组中。