iOS:使用UIBezierPath bezierPathWithOvalInRect绘制清晰的圆圈: [英] iOS: Draw clear circle with UIBezierPath bezierPathWithOvalInRect:
问题描述
在我的 UITableViewController
的单元格中,我想显示一个带数字的圆圈。我正在使用 UIBezierPath
的 bezierPathWithOvalInRect:
绘制圆圈。
In my UITableViewController
's cells, I want to display a circle with a number in it. I am using UIBezierPath
's bezierPathWithOvalInRect:
to draw the circle.
不幸的是,虽然我可以将 fill
颜色设置为 clearColor
,传递给 bezierPathWithOvalInRect:
的 CGRect
中未使用的部分为黑色。
Unfortunately, while I can set the fill
color to be clearColor
, the unused portion of the CGRect
passed to bezierPathWithOvalInRect:
is black.
如何摆脱创建的黑色区域?
How do I get rid of the black area created?
部分屏幕截图供参考:
(我最终希望将这个数字放入圆圈内)
Partial screenshot for reference:
(I eventually hope to get that number inside the circle)
代码:
- (void)awakeFromNib {
[super awakeFromNib];
// Create a square view using the height of the cell
CGRect positionFrame = CGRectMake(0, 0, self.bounds.size.height, self.bounds.size.height);
LTTDrawBallView *drawBallView = [[LTTDrawBallView alloc] initWithFrame:positionFrame];
[self.contentView addSubview:drawBallView];
}
LTTDrawBallView:
LTTDrawBallView:
- (void)drawRect:(CGRect)rect
{
// Create a new rect with some padding
// + create a circle from this new rect:
CGRect box = CGRectInset(self.bounds, self.bounds.size.width * 0.1f, self.bounds.size.height * 0.1f);
UIBezierPath *ballBezierPath = [UIBezierPath bezierPathWithOvalInRect:box];
[[UIColor whiteColor] setStroke];
[[UIColor greenColor] setFill]; // Green here to show the black area
[ballBezierPath stroke];
[ballBezierPath fill];
[self setBackgroundColor:[UIColor clearColor]]; // Happens with and without this line
}
推荐答案
在LTTDrawBallView的init方法中,包括以下代码:
In the init method of your LTTDrawBallView, include the code:
self.opaque = NO;
self.backgroundColor = [UIColor clearColor];
这篇关于iOS:使用UIBezierPath bezierPathWithOvalInRect绘制清晰的圆圈:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!