J'ai une vue dans laquelle je veux dessiner un cercle avec UIBezierPath bezierPathWithArcCenter
. initWithFrame
de mon point de vue est le suivant:UIBezierPath bezierPathWithArcCenter pas correctement centré
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if(self == nil) {
return nil;
}
self.circleLayer = [CAShapeLayer layer];
self.circleLayer.fillColor = UIColor.clearColor.CGColor;
self.circleLayer.strokeColor = UIColor.blackColor.CGColor;
self.circleLayer.lineWidth = 4;
self.circleLayer.bounds = self.bounds;
self.startAngle = 0.0;
self.endAngle = M_PI * 2;
CGPoint center = CGPointMake(self.frame.size.width * 0.5, self.frame.size.height * 0.5);
CGFloat radius = self.frame.size.height * 0.45;
self.circleLayer.path = [UIBezierPath bezierPathWithArcCenter:center radius:radius startAngle:self.startAngle endAngle:self.endAngle clockwise:YES].CGPath;
[self.layer addSublayer:self.circleLayer];
self.backgroundColor = UIColor.greenColor;
return self;
}
Je m'y attendais produirait un cercle noir centré à mon avis. Cependant, la sortie réelle ressemble à ceci:
Qu'est-ce que je fais mal?
EDIT Le plus étrange partie est qu'il est dessiné correctement après une nouvelle installation de l'application - des lancements consécutifs en sorte qu'il soit établi comme dans l'image ci-joint. Pourquoi?
Essayez d'utiliser ce code dans la méthode drawRect –
'self.circleLayer.frame = self.bounds;'? – Larme
@Larme bien, c'est une sous-couche, elle doit donc avoir l'origine '(0, 0)'. Ou ai-je tort? –