J'ai codé comme ci-dessous pour demi-cercle dans iOS en utilisant UIBezierPath et CAShapeLayer .
clockWiseLayer = [[CAShapeLayer alloc] init]; CGFloat startAngle = -M_PI_2; CGFloat endAngle = M_PI + M_PI_2; CGFloat width = CGRectGetWidth(imageView.frame)/2.0f + 30; CGFloat height = CGRectGetHeight(imageView.frame)/2.0f +50; CGPoint centerPoint = CGPointMake(width, height); float radius = CGRectGetWidth(imageView.frame)/2+10; clockWiseLayer.path = [UIBezierPath bezierPathWithArcCenter:centerPoint radius:radius startAngle:startAngle endAngle:endAngle clockwise:YES].CGPath; clockWiseLayer.fillColor = [UIColor clearColor].CGColor; clockWiseLayer.strokeColor = [UIColor blueColor].CGColor; clockWiseLayer.borderColor = [UIColor greenColor].CGColor; clockWiseLayer.backgroundColor = [UIColor redColor].CGColor; clockWiseLayer.strokeStart = 0.0f; clockWiseLayer.strokeEnd = 0.5f; clockWiseLayer.lineWidth = 2.0f; clockWiseLayer.borderWidth = 5.0f; clockWiseLayer.shouldRasterize = NO; [self.layer addSublayer:clockWiseLayer];
Cela résulte comme ci-dessous.
Je veux ce demi-cercle bleu de l'autre côté du World Globe.
C'est à moitié cercle, mais je le veux de l'autre côté, aussi CounterClockWise.
Je suis incapable de régler l'angle de début et de fin pour cela, alors que dans le sens des aiguilles d'une montre: NON .
Merci.
Vous devez dessiner à partir de M_PI + M_PI_2 Et ensuite vous définissez strokeEnd à 1.0f
Consultez la documentation pour les coordonnées: http://i.stack.imgur.com/1yJo6.png
C'est un peu déroutant mais 0 n'est pas le sumt du cercle
CGFloat startAngle = M_PI_2; CGFloat endAngle = startAngle + M_PI; CGFloat width = CGRectGetWidth(imageView.frame)/2.0f; CGFloat height = CGRectGetHeight(imageView.frame)/2.0f; CGPoint centerPoint = CGPointMake(width, height); float radius = CGRectGetWidth(imageView.frame)/2+10; CAShapeLayer* clockWiseLayer = [[CAShapeLayer alloc] init]; clockWiseLayer.path = [UIBezierPath bezierPathWithArcCenter:centerPoint radius:radius startAngle:startAngle endAngle:endAngle clockwise:YES].CGPath; clockWiseLayer.fillColor = [UIColor clearColor].CGColor; clockWiseLayer.strokeColor = [UIColor blueColor].CGColor; clockWiseLayer.borderColor = [UIColor greenColor].CGColor; clockWiseLayer.backgroundColor = [UIColor redColor].CGColor; clockWiseLayer.strokeStart = 0.0f; clockWiseLayer.strokeEnd = 1.0f; clockWiseLayer.lineWidth = 2.0f; clockWiseLayer.borderWidth = 5.0f; [self.layer addSublayer:clockWiseLayer];
Vous commencez par le haut du cercle avec -M_PI_2
et -M_PI_2
par M_PI + M_PI_2
(si vous voulez avoir un cercle complet et le limiter en utilisant strokeEnd
, strokeEnd
). Ensuite, définissez le path du cercle pour dessiner à partir de la moitié de la fin du path (côté gauche de l'image) au lieu de commencer à la moitié (côté droit de l'image)
CGFloat startAngle = -M_PI_2; CGFloat endAngle = M_PI + M_PI_2; clockWiseLayer.strokeStart = .5f; clockWiseLayer.strokeEnd = 1.0f;
J'ai joué avec bezierPathWithArcCenter
et cela fonctionne assez étrange si bezierPathWithArcCenter
clockwise = NO
. Mais si vous voulez créer un cercle Bézier avec une direction dans le sens inverse des aiguilles d'une montre, vous pouvez créer un path dans le sens des aiguilles d'une montre et le rétablir avec bezierPathByReversingPath
Un nouvel object trajectoire avec la même forme de path mais pour lequel le path a été créé dans le sens inverse.