2

I'm stumped here and need some assistance with this. For testing purposes I have a UIView that I draw into via drawRect (code below). I'm able to draw a circle with an specific angle and such but it's filled and closed. I need it to be open and stroked. I'm currently using UIBezierPath to do this but is there better way to do this? I can draw the open ended triangle like I want in UIBezierPath but I can't specify the angle that I need it to be (code below also). Any assistance with this would be much appreciated. Thanks.

The picture below is how I would like it to look when I draw it with a specific angle.

enter image description here

#import "AngleView.h"
#define   DEGREES_TO_RADIANS(degrees)  ((M_PI * degrees)/ 180)
@implementation AngleView

-(void)drawRect:(CGRect)rect {

UIBezierPath *aPath = [UIBezierPath bezierPathWithArcCenter:CGPointZero
                                                     radius:50
                                                 startAngle:0
                                                   endAngle:DEGREES_TO_RADIANS(45)
                                                  clockwise:NO];

[aPath fill];
}

@end

and here is how I can draw it without a specific angle.

UIBezierPath* bezierPath = [UIBezierPath bezierPath];
[bezierPath moveToPoint: CGPointMake(86.5, 33.5)];
[bezierPath addLineToPoint: CGPointMake(60.5, 62.5)];
[bezierPath addLineToPoint: CGPointMake(87.5, 62.5)];
[[UIColor blackColor] setStroke];
bezierPath.lineWidth = 1;
[bezierPath stroke];
rmaddy
  • 314,917
  • 42
  • 532
  • 579
0SX
  • 1,252
  • 6
  • 24
  • 47
  • 1
    Have you considered just drawing two separate lines? – Nathan S. Jul 24 '14 at 22:22
  • Yes, that's what I thought about doing from the very beginning but I figured it was the wrong way to go about it and discarded the idea but I guess that's how I should do it huh? However, wouldn't that make the corner of the angle look no connected and not sharp? Thanks. – 0SX Jul 24 '14 at 23:19

1 Answers1

1

Instead of trying draw one single line you can draw two separate originating from same point and you can give angle to one of them.

- (void)drawRect:(CGRect)rect {
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSaveGState(context);
    CGContextTranslateCTM(context, 0, 119.5);  //here you can set originating point of line
    CGContextRotateCTM(context, -45 * M_PI / 180);  //Change Angle here -45
    UIBezierPath* bezierPath = UIBezierPath.bezierPath;
    [bezierPath moveToPoint: CGPointMake(0, 0)];
    [bezierPath addCurveToPoint: CGPointMake(39, 0) controlPoint1: CGPointMake(39, 0) controlPoint2: CGPointMake(39, 0)];
    [UIColor.blackColor setStroke];
    bezierPath.lineWidth = 1;
    [bezierPath stroke];
    CGContextRestoreGState(context);

    //Second UIBezierPath with 0 angle
    context = UIGraphicsGetCurrentContext();
    UIBezierPath* bezier2Path = UIBezierPath.bezierPath;
    [bezier2Path moveToPoint: CGPointMake(0, 119.5)];
    [bezier2Path addCurveToPoint: CGPointMake(38.5, 119.5) controlPoint1: CGPointMake(38.5, 119.5) controlPoint2: CGPointMake(38.5, 119.5)];
    [UIColor.blackColor setStroke];
    bezier2Path.lineWidth = 1;
    [bezier2Path stroke];
    CGContextRestoreGState(context);
}
modusCell
  • 13,151
  • 9
  • 53
  • 80
  • So I just need to draw the base line correct because I don't see it in your code. Thanks so much! – 0SX Jul 24 '14 at 23:08
  • Yes the code is just draws the -45 angled line, you can repeat same code to draw second one with 0 angle. – modusCell Jul 24 '14 at 23:10
  • Thanks mohacs! I'm always learning something new. I'm sure your code will help others with similar questions. – 0SX Jul 24 '14 at 23:37