主要代碼
1、遍歷所有扇形的比例,計(jì)算出每個(gè)扇形區(qū)域的開始和結(jié)束角度,然后繪制FanShapeLayer
int max = 0;
for (NSNumber * obj in self.angles) {
max += [obj intValue];
}
CGFloat startAngle = self.startAngle;// 起點(diǎn) 0 代表x軸正方向
for (int i = 0; i < self.angles.count; i ++) {
NSNumber * obj = [self.angles objectAtIndex:i];
CGFloat rate = [obj intValue];
CGFloat angle = rate / max * M_PI * 2.0;
// 計(jì)算繪制角度-終點(diǎn) 繪制的角度 繪制的方向是逆時(shí)針的所以是減
CGFloat endAngle = startAngle - angle;
// 設(shè)置顏色
UIColor * color = randomColor;
// 創(chuàng)建FanShapeLayer
FanShapeLayer * layer = [[FanShapeLayer alloc]init];
layer.fillColor = color.CGColor;
layer.name = [NSString stringWithFormat:@"圖層 = %d",i];
layer.startAngle = startAngle;
layer.endAngle = endAngle;
layer.radius = self.radius;
layer.centerPoint = self.chartPoint;
// 繪制路徑(先設(shè)置參數(shù))
[layer drawPath];
[self.layer addSublayer:layer];
[self.fansArray addObject:layer];
// 計(jì)算下一次繪制起點(diǎn)
startAngle -= angle;
}
2稠歉、在設(shè)置FanShapeLayer的參數(shù)后,先繪制CGMutablePathRef即一個(gè)封閉的扇形,然后把CGMutablePathRef賦給FanShapeLayer
CGMutablePathRef path = CGPathCreateMutable();
CGAffineTransform transform = CGAffineTransformMakeTranslation(1, 1);
CGPathMoveToPoint(path, &transform, self.centerPoint.x, self.centerPoint.y);
CGPathAddArc(path, &transform, self.centerPoint.x, self.centerPoint.y, radius, self.startAngle, self.endAngle, YES);
CGPathCloseSubpath(path);
3檀轨、主要是點(diǎn)擊放大,CALyer是不響應(yīng)點(diǎn)擊事件的,這里是通過獲得觸摸對象的點(diǎn)是否在layer區(qū)域內(nèi)實(shí)現(xiàn)的花鹅。
網(wǎng)上查到可以通過下面方法獲得點(diǎn)擊的圖層,但這里得到的是nil,不知道為什么着撩。
CALayer * layer = [self.layer hitTest:point];
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
// 獲取點(diǎn)擊的中心點(diǎn)
UITouch * touch = [touches anyObject];
CGPoint point = [touch locationInView:self];
// 獲取點(diǎn)擊區(qū)域 是否包含的CAShapeLayer
FanShapeLayer * layer = [self touchLayer:point];
if (layer) {
NSLog(@"%@", layer.name);
// 設(shè)置選中的為未選中
NSPredicate * predicate = [NSPredicate predicateWithFormat:@"isSelected = YES"];
NSArray * selectArray = [self.fansArray filteredArrayUsingPredicate:predicate];
[selectArray makeObjectsPerformSelector:@selector(setIsSelected:) withObject:@NO];
if ([selectArray containsObject:layer]) {
return ;
}
// 添加動(dòng)畫
layer.isSelected = YES;
}
}
- (FanShapeLayer *)touchLayer:(CGPoint)point{
// 獲取點(diǎn)擊的區(qū)域layer
for (FanShapeLayer * layer in self.fansArray) {
// 獲取inPoint夜矗,在layer中的坐標(biāo)
CGPoint inPoint = [layer convertPoint:point fromLayer:self.layer];
if (CGPathContainsPoint(layer.path, 0, inPoint, YES)) {
return layer;
}
}
return nil;
}
4点骑、最后在點(diǎn)擊的時(shí)候添加縮放動(dòng)畫
// 添加動(dòng)畫
CABasicAnimation *animation = [CABasicAnimation animation];
// keyPath內(nèi)容是對象的哪個(gè)屬性需要?jiǎng)赢? animation.keyPath = @"path";
// 設(shè)置代理
animation.delegate = self;
// 所改變屬性的結(jié)束時(shí)的值
animation.toValue = (__bridge id _Nullable)(self.endPath);
// 動(dòng)畫時(shí)長
animation.duration = 0.25;
// 結(jié)束后不移除
animation.removedOnCompletion = NO;
animation.fillMode = kCAFillModeForwards;
// 添加動(dòng)畫
[self addAnimation:animation forKey:nil];