iOS: Draw clear circle with UIBezierPath bezierPathWithOvalInRect:

时光总嘲笑我的痴心妄想 提交于 2019-12-11 08:15:32

问题


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.

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)

Code:

LTTTableViewCell:

- (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:

- (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
}

回答1:


In the init method of your LTTDrawBallView, include the code:

self.opaque = NO;
self.backgroundColor = [UIColor clearColor];


来源:https://stackoverflow.com/questions/18115639/ios-draw-clear-circle-with-uibezierpath-bezierpathwithovalinrect

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!