我正在尝试实现一个自定义 UIView,它基本上是一个饼图菜单(类似于分成几片的蛋糕)。
为此,我试图从中心绘制一个圆和一系列线,就像图表轮中的光线一样。
我已经成功地绘制了圆圈,现在我想绘制将圆圈分成几部分的线。
这是我到目前为止:
-(void)drawRect:(CGRect)rect{
[[UIColor blackColor] setStroke];
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGFloat minDim = (rect.size.width < rect.size.height) ? rect.size.width : rect.size.height;
CGRect circleRect = CGRectMake(0, rect.size.height/2-minDim/2, minDim, minDim);
CGContextAddEllipseInRect(ctx, circleRect);
CGContextSetFillColor(ctx, CGColorGetComponents([[UIColor yellowColor] CGColor]));
CGContextFillPath(ctx);
CGPoint start = CGPointMake(0, rect.size.height/2);
CGPoint end = CGPointMake(rect.size.width, rect.size.height/2);
for (int i = 0; i < MaxSlices(6); i++){
CGFloat degrees = 1.0*i*(180/MaxSlices(6));
CGAffineTransform rot = CGAffineTransformMakeRotation(degreesToRadians(degrees));
UIBezierPath *path = [self pathFrom:start to:end];
[path applyTransform:rot];
}
}
- (UIBezierPath *) pathFrom:(CGPoint) start to:(CGPoint) …Run Code Online (Sandbox Code Playgroud) 我有一个自定义的UIView,它绘制了一条这样的路径
- (void)drawRect:(CGRect)rect{
[[UIColor blackColor] setStroke];
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextSetFillColor(ctx, CGColorGetComponents(self.color));
CGContextFillPath(ctx);
UIBezierPath *thePath = [UIBezierPath bezierPath];
thePath.lineWidth = _lineWidth;
_center = CGPointMake(rect.size.width, rect.size.height);
[thePath moveToPoint:_center];
[thePath addArcWithCenter:_center radius:rect.size.width startAngle:M_PI endAngle:M_PI+degreesToRadians(_angle)clockwise:YES];
[thePath closePath];
[thePath fill];
}
Run Code Online (Sandbox Code Playgroud)
我的自定义UIView也有一种改变填充颜色的方法
- (void) changeColor:(UIColor *) newColor {
self.color = [newColor CGColor];
[self setNeedsDisplay];
}
Run Code Online (Sandbox Code Playgroud)
如果我使用任何预定义颜色调用changeColor:方法,例如
[UIColor redColor]
Run Code Online (Sandbox Code Playgroud)
一切正常.相反,如果我试图给它一个自定义的颜色,如
UIColor* color = [UIColor colorWithRed:1.0f green:0.0f blue:0.0f alpha:1.0f];
Run Code Online (Sandbox Code Playgroud)
我自定义UIView的颜色随机闪烁在白色,红色和蓝色之间.
任何想法为什么?
先感谢您!