Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Need a tip with UIBezierPath. Triangle Shape like Instagram Sign Up View

I'm trying to create a bezier path like the instagram triangle in the picture below, however I must be doing something wrong. The Bezier path does not show!

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.

}

-(void)viewDidLayoutSubviews{
[super viewDidLayoutSubviews];

[self drawTriangle];

}

- (IBAction)closeButton:(UIButton *)sender {
[self dismissViewControllerAnimated:YES completion:nil];
}

- (void)drawTriangle{

UIBezierPath* trianglePath = [UIBezierPath bezierPath];
[trianglePath moveToPoint:CGPointMake(self.signUpButton.center.x, self.signUpButton.frame.origin.y + 30)];
[trianglePath addLineToPoint:CGPointMake(self.signUpButton.center.x - 10, self.imageView.frame.size.height)];
[trianglePath addLineToPoint:CGPointMake(self.signUpButton.center.x + 10, self.imageView.frame.size.height)];

UIColor *fillColor = [UIColor whiteColor];
[fillColor setFill];
UIColor *strokeColor = [UIColor whiteColor];
[strokeColor setStroke];

 [trianglePath fill];
[trianglePath stroke];

[trianglePath closePath];
}

enter image description here

like image 954
SleepsOnNewspapers Avatar asked Jun 14 '15 01:06

SleepsOnNewspapers


1 Answers

Xcode 10 • Swift 4.2

func drawTriangle(size: CGFloat, x: CGFloat, y: CGFloat, up:Bool) {

    let triangleLayer = CAShapeLayer()
    let trianglePath = UIBezierPath()
    trianglePath.move(to: .zero)
    trianglePath.addLine(to: CGPoint(x: -size, y: up ? size : -size))
    trianglePath.addLine(to: CGPoint(x: size, y: up ? size : -size))
    trianglePath.close()
    triangleLayer.path = trianglePath.cgPath
    triangleLayer.fillColor = UIColor.white.cgColor
    triangleLayer.anchorPoint = .zero
    triangleLayer.position = CGPoint(x: x, y: y)
    triangleLayer.name = "triangle"
    view.layer.addSublayer(triangleLayer)
}

drawTriangle(size: 12, x: view.frame.midX/2, y: view.frame.midY, up: true)
drawTriangle(size: 12, x: view.frame.midX, y: view.frame.midY, up: false)
like image 152
Leo Dabus Avatar answered Oct 06 '22 00:10

Leo Dabus