获取饼图中被点击的扇形部分:
- (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); }