简体   繁体   中英

UIBezierPath not drawing arc

I'm trying to draw a very simple circle. Here is my code:

CameraButtonView *buttonView = [[CameraButtonView alloc]initWithFrame:CGRectMake((self.view.frame.size.width / 2) - 45, self.view.frame.size.height * 0.8, 90, 90)];
buttonView.layer.cornerRadius = buttonView.frame.size.width / 2;
buttonView.layer.borderWidth = 2;
buttonView.backgroundColor = [UIColor clearColor];
buttonView.layer.borderColor = [UIColor greenColor].CGColor;
buttonView.clipsToBounds = YES;

[previewView addSubview:buttonView];

then, in drawRect:

 UIBezierPath *path1 = [UIBezierPath bezierPath];
[path1 addArcWithCenter:self.center radius:(20) startAngle:0 endAngle:6.28 clockwise:YES];
[path1 setLineWidth:6];
[[UIColor redColor] setStroke];
[path1 stroke];

I have set the center and radius, specified a line width and stroke color, and called stroke. I have set breakpoints in drawRect to make sure the path is not nil, and it is indeed not. Just nothing is drawn.

The only thing I see on screen is a green circle from the code in the view controller where I set the corner radius. I tried calling [buttonView setNeedsDisplay]; as well, and that did not help. I've tried different initializations of the bezier path as well, such as initializing with the arc instead of creating the path and then calling addArcWithCenter.

what am I doing wrong?

Pay attention that a view center is not the center of the view itself, but is the center position of the view in superview coordinates. Probably you are drawing out of screen.

The center is specified within the coordinate system of its superview and is measured in points. Setting this property changes the values of the frame properties accordingly.

If you want to know the center of your view do this:

CGPointMake(CGRectGetMidX(self.bounds), CGRectGetMidY(self.bounds))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM