Q:

Draw Triangle iOS

The code below is drawing me a circle, how can I modify the existing code to draw a triangle instead?
    _colorDotLayer = [CALayer layer];

    CGFloat width = self.bounds.size.width-6;
    _colorDotLayer.bounds = CGRectMake(0, 0, width, width);
    _colorDotLayer.allowsGroupOpacity = YES;
    _colorDotLayer.backgroundColor = self.annotationColor.CGColor;
    _colorDotLayer.cornerRadius = width/2;
    _colorDotLayer.position = CGPointMake(self.bounds.size.width/2, self.bounds.size.height/2);

A:

While there are shown several Core Graphics solution, I want to add a Core Animation based solution.
- (void)viewDidLoad
{
    [super viewDidLoad];
    UIBezierPath* trianglePath = [UIBezierPath bezierPath];
    [trianglePath moveToPoint:CGPointMake(0, view3.frame.size.height-100)];
    [trianglePath addLineToPoint:CGPointMake(view3.frame.size.width/2,100)];
    [trianglePath addLineToPoint:CGPointMake(view3.frame.size.width, view2.frame.size.height)];
    [trianglePath closePath];

    CAShapeLayer *triangleMaskLayer = [CAShapeLayer layer];
    [triangleMaskLayer setPath:trianglePath.CGPath];

    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0,0, size.width, size.height)];

    view.backgroundColor = [UIColor colorWithWhite:.75 alpha:1];
    view.layer.mask = triangleMaskLayer;
    [self.view addSubview:view];
}

code based on my blog post.

A:

@implementation TriangleView {
    CAShapeLayer *_colorDotLayer;
}

- (void)layoutSubviews {
    [super layoutSubviews];

    if (_colorDotLayer == nil) {
        _colorDotLayer = [CAShapeLayer layer];
        _colorDotLayer.fillColor = self.annotationColor.CGColor;
        [self.layer addSublayer:_colorDotLayer];
    }

    CGRect bounds = self.bounds;
    CGFloat radius = (bounds.size.width - 6) / 2;
    CGFloat a = radius * sqrt((CGFloat)3.0) / 2;
    CGFloat b = radius / 2;
    UIBezierPath *path = [UIBezierPath bezierPath];
    [path moveToPoint:CGPointMake(0, -radius)];
    [path addLineToPoint:CGPointMake(a, b)];
    [path addLineToPoint:CGPointMake(-a, b)];
    [path closePath];
    [path applyTransform:CGAffineTransformMakeTranslation(CGRectGetMidX(bounds), CGRectGetMidY(bounds))];
    _colorDotLayer.path = path.CGPath;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    self.annotationColor = [UIColor redColor];
}

@end

Result:

A:

I think is more easy than this solutions:
 UIBezierPath *path = [UIBezierPath new];
[path moveToPoint:(CGPoint){20, 0}];
[path addLineToPoint:(CGPoint){40, 40}];
[path addLineToPoint:(CGPoint){0, 40}];
[path addLineToPoint:(CGPoint){20, 0}];

// Create a CAShapeLayer with this triangular path
// Same size as the original imageView
CAShapeLayer *mask = [CAShapeLayer new];
mask.frame = self.viewTriangleCallout.bounds;
mask.path = path.CGPath;

This is my white triangle:
You can change points or rotate if you want:
UIBezierPath *path = [UIBezierPath new];
[path moveToPoint:(CGPoint){0, 0}];
[path addLineToPoint:(CGPoint){40, 0}];
[path addLineToPoint:(CGPoint){20, 20}];
[path addLineToPoint:(CGPoint){0, 0}];

// Create a CAShapeLayer with this triangular path
// Same size as the original imageView
CAShapeLayer *mask = [CAShapeLayer new];
mask.frame = self.viewTriangleCallout.bounds;
mask.path = path.CGPath;

