获取饼图中被点击的扇形部分:
- (NSInteger)getCurrentSelectedOnTouch:(CGPoint)point {
//初始化selectIndex
__block NSInteger selectIndex = -1;
//坐标重置
CGAffineTransform transform = CGAffineTransformIdentity;
//遍历饼图中的子layer
[self.layer.sublayers enumerateObjectsUsingBlock:^(CALayer * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
CAShapeLayer *shapeLayer = (CAShapeLayer *)obj;
CGPathRef path = [shapeLayer path];
//CGPathContainsPoint: 如果点包含在路径中,则返回true
if (CGPathContainsPoint(path, &transform, point, 0)) {
selectIndex = idx;
}
}];
return selectIndex;
}
点击事件:
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
[super touchesEnded:touches withEvent:event];
//得到触摸点
CGPoint point = [[touches anyObject] locationInView:self];
//调用上面确定selectIndex的方法
NSInteger selectIndex = [self getCurrentSelectedOnTouch:point];
NSLog(@"被点击的部分------%ld",(long)selectIndex);
}
本文介绍如何在饼图中实现对扇形区域的点击事件监听,详细讲解了获取被点击扇形部分的方法和点击事件的处理流程。
3626

被折叠的 条评论
为什么被折叠?



