如何使用子类化自定义 CALayer

发布于 2024-12-27 14:25:52 字数 379 浏览 4 评论 0原文

在 UIView 上有许多 CALayers。每个 CALayer 由 CAShapeLayers 组成。 CAShapeLayers 的路径属性由 UIBezierPath 组成。

我的目标是,当我点击 CALayer 时,我想获得在 UIBezierPath 绘图中使用的点。为此,我想到了 CALayer 的子类化,它有 NSMutableArray 来存储点。当我在特定的 CALayer 中绘图时,我会节省积分。因此,每当我点击特定的 CALayer 时,我都会获得与之相关的积分。我还想给每个 CALayer 赋予标签。我不知道该怎么做。

第二种方法是使用 CALayer 的内置属性,这将为我提供在 CALayer 中捕获的点。但我不知道这种财产。

请分享您的想法如何完成这项任务?

On UIView there are number of CALayers. Each CALayer consists of CAShapeLayers. CAShapeLayers' path property consists of UIBezierPath.

My objective is when i tap on CALayer i want to get points which i used in the drawing of UIBezierPath. For that i thought of subclassing CALayer which has NSMutableArray to store points. I will be saving points when i am drawing in particular CALayer. So whenever i tap on particular CALayer i will get points associated to that. Also i want to give tag to each CALayer. I do not know how to do this.

The second way is to use CALayer's inbuilt property which will give me points captured in CALayer. But i am not aware of this kind of property.

Please share your ideas how to accomplish this task?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烛影斜 2025-01-03 14:25:52

您将需要使用包含 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(或大多数其他 Core Graphics 结构)存储在 NSValue 中并将其放入数组中即可。

You will need to use the UIView that contains the CALayer to handle the touch events, there is no built in Touch events for CALayers. - (CALayer *)hitTest:(CGPoint)thePoint returns the "deepest" CALayer that the point from the touch event is within. So, if you call [self.layer hitTest:point] it will check all your sublayers and return the correct CALayer

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    CGPoint point = [[touches anyObject] locationInView:self];
    CALayerSubclass *taplayer = [self.layer hitTest:point]
    NSArray *points = [taplayer getPoints];
}

There is no way to get out of a CGPath the points you fed into it. The best you can do are these methods about getting info from the path. So, like you said, your best bet is to subclass CALayer and put all the info you need in a data structure to retrieve later.

// .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]];  
    ...
}

Just store all the CGPoints (or most other Core Graphics structure) in an NSValue and throw it into an array.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文