Skip to content

Instantly share code, notes, and snippets.

@chrisbrandow
Created February 17, 2015 20:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chrisbrandow/589a030b1dd56d243e31 to your computer and use it in GitHub Desktop.
Save chrisbrandow/589a030b1dd56d243e31 to your computer and use it in GitHub Desktop.
- (UIBezierPath *)pathForView:(UIView *)button withVertices:(CGFloat)vertices initialPointAngle:(CGFloat)initialAngle cornerRadius:(CGFloat)pCornerRadius lineWidth:(CGFloat)lineWidth{
    
    CGFloat angle = 2*M_PI/vertices;
    
    CGPoint buttonCenter = CGPointMake(button.frame.size.width/2, button.frame.size.height/2);
 
    CGFloat totalRadius = button.frame.size.width/2;
    CGFloat innerRadius = totalRadius - pCornerRadius -lineWidth;
    
    UIBezierPath* bezierPath = UIBezierPath.bezierPath;
 
    CGFloat totalAngle = 0*angle + initialAngle;
    CGPoint vertex = CGPointMake(buttonCenter.x + innerRadius*cos(totalAngle), buttonCenter.y + innerRadius*sin(totalAngle));
    CGFloat shiftX = pCornerRadius*cos(totalAngle - angle/2);
    CGFloat shiftY = pCornerRadius*sin(totalAngle - angle/2);
    CGPoint shiftedVertex = vertex;
    shiftedVertex.x += shiftX;
    shiftedVertex.y += shiftY;
 
    [bezierPath moveToPoint:shiftedVertex];
    [bezierPath addArcWithCenter:vertex
                          radius:pCornerRadius
                      startAngle:totalAngle - angle/2
                        endAngle:totalAngle + angle/2
                       clockwise:YES];
    
    for (NSInteger i = 1; i < vertices; i++) {
        totalAngle = (double)i*angle + initialAngle;
        shiftX = pCornerRadius*cos(totalAngle - angle/2);
        shiftY = pCornerRadius*sin(totalAngle - angle/2);
        
        vertex = CGPointMake(buttonCenter.x + innerRadius*cos(totalAngle), buttonCenter.y + innerRadius*sin(totalAngle));
        
        shiftedVertex = vertex;
        shiftedVertex.x += shiftX;
        shiftedVertex.y += shiftY;
        
        [bezierPath addLineToPoint:shiftedVertex];
        [bezierPath addArcWithCenter:vertex
                              radius:pCornerRadius
                          startAngle:totalAngle - angle/2
                            endAngle:totalAngle + angle/2
                           clockwise:YES];
    }
    
    [bezierPath closePath];
    bezierPath.lineWidth = lineWidth;
 
    return bezierPath;
    
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment