标签:
用Quartz2d在layer上用非零绕数原则绘制扇形,最终形成一个圆,同时在圆的内部改变中心点连线,形成角,拼合成任意角星
以下代码是在控制器创建View
//创建View
- (void)loadViewForCenter
{
//创建显示的View
TempView *tempView = [[TempView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
tempView.backgroundColor = [UIColor colorWithRed:0.7625 green:0.8462 blue:1.0 alpha:1.0];
[self.view addSubview:tempView];
self.tempView = tempView;
tempView.colorArray = self.colorArray;
self.tempView.startAngle = -M_PI_2;
self.tempView.starCenterPoint = 0;
self.tempView.loadTime = self.loadTime;
self.tempView.cutNum = self.pathNum;
tempView.backgroundColor = [UIColor colorWithRed:arc4random_uniform(255)/255.0 green:arc4random_uniform(255)/255.0 blue:arc4random_uniform(255)/255.0 alpha:0.5];
tempView.center = self.view.center;
//创建View设置为大小1的点,作为偏移中心点的连线
UIView *centerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 0, 0)];
centerView.center = CGPointMake(tempView.bounds.size.width/ 2,tempView.bounds.size.height/ 2);
tempView.centerView = centerView;
centerView.backgroundColor = [UIColor redColor];
//将中心View初始化的时候转向第一部分的中间
centerView.transform = CGAffineTransformRotate(centerView.transform,M_PI /self.pathNum);
[tempView addSubview:centerView];
}
以下是设置layer上的View
- (void)drawRect:(CGRect)rect { // 找到 宽和高一半最小的一个, 把这个计算的值, 当做 中心点和半径 CGFloat centerX = self.frame.size.width/2; CGFloat centerY = self.frame.size.height/2; // 中心View的中心点 CGPoint center = CGPointMake(centerX, centerY); // 起始位置, 结束位置 //CGFloat startAngle = 0; CGFloat endAngle = 0; CGAffineTransform centerTransForm = self.centerView.transform; //循环创建每一个扇形 for (int i = 0; i < self.cutNum; i++) { //记录初始的transform self.centerView.transform = centerTransForm; CGFloat path = 1.0 / self.cutNum; endAngle = 2 * M_PI * path + self.startAngle; // // 绘制大圆形 UIBezierPath *bezierBigPath = [UIBezierPath bezierPathWithArcCenter:center radius:[UIScreen mainScreen].bounds.size.width/2 startAngle:self.startAngle endAngle:endAngle clockwise:YES]; // 向中心点添加一根线 [bezierBigPath addLineToPoint:center]; //非零绕数原则创建小圆 UIBezierPath *bezierSmallPath = [UIBezierPath bezierPathWithArcCenter:center radius:([UIScreen mainScreen].bounds.size.width/2 - 20) startAngle:endAngle endAngle:self.startAngle clockwise:NO]; //把中心View转向对应部分的中间 并向上偏移 // self.starCenterPoint = 2 * M_PI /self.cutNum; self.centerView.transform = CGAffineTransformRotate(self.centerView.transform,i *(2 * M_PI /self.cutNum) + self.starCenterPoint); self.centerView.transform = CGAffineTransformTranslate(self.centerView.transform, 0, -50); CGPoint point = CGPointMake(CGRectGetMaxX(self.centerView.frame), CGRectGetMaxY(self.centerView.frame)); [bezierSmallPath addLineToPoint:point]; //路径组合 [bezierBigPath appendPath:bezierSmallPath]; // 设置颜色 [self.colorArray[i] setFill]; [bezierBigPath fill]; // 当一次绘制结束的时候 self.startAngle = endAngle; } self.centerView.transform = centerTransForm; }
标签:
原文地址:http://www.cnblogs.com/hao-liu/p/5796795.html