UIBezierPath and applytransform

I am trying to implement a custom UIView, which is basically a pie menu (something like a cake divided into slices).

To do this, I try to draw a circle and a series of lines from the center, like rays in the wheel of a diagram.

I have successfully drawn a circle, and now I would like to draw lines dividing the circle in slices.

This is what I have so far:

-(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; } 

The problem is that applyTransform does nothing on the path. The first road sign is drawn correctly and the following rotations are not affected by the rotation. Basically, I see only one way. Check out the screenshot here http://img837.imageshack.us/img837/9757/iossimulatorscreenshotf.png

Thank you for your help!

+4
source share
1 answer

You draw a path (with stroke ) before converting it. A path is just a mathematical representation. This is not a β€œon screen” line. You cannot move what you have already drawn by changing the data about it.

Just move [aPath stroke] from pathFrom:to: and place it after applyTransform:

+4
source

Source: https://habr.com/ru/post/1396855/


All Articles