UIBezierPath e applyTransform
-
26-10-2019 - |
Domanda
Sto cercando di implementare un UIView personalizzato che è fondamentalmente un menu a torta (qualcosa di simile a una torta divisa in fette).
Per questo sto cercando di disegnare un cerchio e una serie di linee dal centro, come i raggi in rotella di un grafico.
ho disegnato con successo il cerchio e e vorrei ora per disegnare le linee che dividono il cerchio a fette.
Questo è quello che ho finora:
-(void)drawRect:(CGRect)rect{
[[UIColor blackColor] setStroke];
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGFloat minDim = (rect.size.width < rect.size.height) ? rect.size.width : rect.size.height;
CGRect circleRect = CGRectMake(0, rect.size.height/2-minDim/2, minDim, minDim);
CGContextAddEllipseInRect(ctx, circleRect);
CGContextSetFillColor(ctx, CGColorGetComponents([[UIColor yellowColor] CGColor]));
CGContextFillPath(ctx);
CGPoint start = CGPointMake(0, rect.size.height/2);
CGPoint end = CGPointMake(rect.size.width, rect.size.height/2);
for (int i = 0; i < MaxSlices(6); i++){
CGFloat degrees = 1.0*i*(180/MaxSlices(6));
CGAffineTransform rot = CGAffineTransformMakeRotation(degreesToRadians(degrees));
UIBezierPath *path = [self pathFrom:start to:end];
[path applyTransform:rot];
}
}
- (UIBezierPath *) pathFrom:(CGPoint) start to:(CGPoint) end{
UIBezierPath* aPath = [UIBezierPath bezierPath];
aPath.lineWidth = 5;
[aPath moveToPoint:start];
[aPath addLineToPoint:end];
[aPath closePath];
[aPath stroke];
return aPath;
}
Il problema è che applyTransform sul percorso non sembra di fare nulla. Il primo gest percorso disegnato correttamente 'e nelle successive sono influenzate dalla rotazione. Fondamentalmente quello che vedo è solo un percorso. Controllare la schermata qui http://img837.imageshack.us/img837/9757/iossimulatorscreenshotf. png
Grazie per il vostro aiuto!
Soluzione
You're drawing the path (with stroke
) before you transform it. A path is just a mathematical representation. It isn't the line "on the screen." You can't move what you've already drawn by modifying the data about it.
Just move the [aPath stroke]
out of pathFrom:to:
and put it after the applyTransform:
.