iOS SDK-如何检测饼图切片上的触摸?
我开发了一个动画饼图,当用户单击饼图切片时,它就会从图表中出来。目前,我正在不同的 CGLayer 上绘制饼图的每一片。当用户点击切片时,我会选择该特定像素的颜色并将其与 RGB 代码(硬编码)相匹配,从而拉出相应的图层。
我不想使用任何第三方图表库,例如核心图。
有没有办法使每一层成为单独的图形实体,以便可以检测到该对象上的触摸?
谢谢
I have developed an animated Pie Chart, in which when a user clicks on a Pie Chart slice, it comes out of the chart. Currently, I am drawing each slice of the Pie on a different CGLayer. When the user taps on a slice I pick the color of that particular pixel and match it with the RGB code (hard-coded) which pulls out the corresponding layer.
I don't want to use any third-party charting library like core plot.
Is there a way to make each layer an individual graphic entity, so that a touch on that object can be detected?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不能只使用触摸的 X,Y 坐标来计算到饼图中心的角度以及距离吗?
如果距离小于半径,则使用角度来确定点击了哪个图表。
Can't you just use the X,Y coordinate of the touch to calculate the angle to the center of the pie chart along with the distance?
If the distance is lesser than the radius then use the angle to figure out which chart was tapped on.
我使用 CAShapeLayer 来表示各个饼图切片。
CAShapeLayer
实例具有 path 属性,您可以使用该属性来测试某个点是否位于该路径内。只需检测父 UIView 上的触摸点,然后迭代所有饼图切片并使用 CGPathContainsPoint 函数来测试该点是否位于该饼图切片内。
I used
CAShapeLayer
to represent individual pie slices.CAShapeLayer
instances have a path property which you can use to test if a point is within that path.Just detect the touch point on the parent UIView and then iterate over all your pie slices and use the
CGPathContainsPoint
function to test if the point is within that pie slice.如果您使用 CG,那么您可以测试某个点是否在路径内。
在此处查看
CGPathContainsPoint
。If you are using CG then you can test if a point is within a path.
Checkout
CGPathContainsPoint
here.