4

I'm drawing a circle via a CAShapeLayer now I want that my circle is centered in the middle of the screen. The circles midpoint must be the middle point of my view.

This how I now try to do this:

- (void)_initCircle {
    [_circle removeFromSuperlayer];
    _circle = [CAShapeLayer layer];
    _radius = 100;

    // Create a circle
    _circle.path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, 2.0 * _radius, 2.0 * _radius) cornerRadius:_radius].CGPath;

    // Center the shape in self.view
    _circle.position = CGPointMake(CGRectGetMidX(self.view.bounds) - _radius,
                                   CGRectGetMidY(self.view.bounds) - _radius);

    _circle.fillColor = [UIColor clearColor].CGColor;
    _circle.lineWidth = 5;

    // Add to parent layer
    [self.view.layer addSublayer:_circle];
}

The screenshot makes it clear that the circle isn't perfect in the mid because the label is centered in the middle of the screen ( center Y ). Where the blue and the black of the circle that should be equal to the label I think.

enter image description here

I don't see the calculation error, can someone help me?

EDIT:

Changing bounds to frame and letting the radius off gives me this result:

enter image description here

user1007522
  • 7,858
  • 17
  • 69
  • 113

2 Answers2

2

Layers position themselves relatively to their anchor point, so you could try doing this:

// Center the shape in self.view
_circle.position = self.view.center;
_circle.anchorPoint = CGPointMake(0.5, 0.5);
Gorka
  • 331
  • 1
  • 5
0
import UIKit

 class DrawCircleView.UIView{

 init(frame:CGRect){
      super,init(frame:frame)
   //Initialization code
}
 init(coder aDecoder:NSCoder!){
     super.init(coder:aDecoder)
}
//Only override drawRect:if you perform custom drawing.
//An empty implementation adversely affects performance during animation.
override func drawRect(rect:CGRect)
{
//Drawing code

var context = UIGraphicsGetCurrentContext()

CGContextAddArc(context, 150,300,100,0,3.14*2,0)
//you can change (150/300/100),these arguments to point the position.
CGContextStrokePath(context)
}
}