如何使用子类化自定义 CALayer
在 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您将需要使用包含 CALayer 的 UIView 来处理触摸事件,CALayer 没有内置触摸事件。
- (CALayer *)hitTest:(CGPoint)thePoint
返回触摸事件中的点所在的“最深”CALayer。因此,如果您调用 [self.layer hitTest:point] 它将检查所有子层并返回正确的 CALayer没有办法从 CGPath 中获取点你投入其中。你能做的最好的事情是 这些关于从路径获取信息的方法。因此,就像您所说,最好的选择是子类化 CALayer 并将所需的所有信息放入数据结构中以便稍后检索。
只需将所有 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 correctCALayer
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.
Just store all the CGPoints (or most other Core Graphics structure) in an NSValue and throw it into an array.