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

非 Y 不嫁゛ 提交于 2019-12-21 02:55:14

问题


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


回答1:


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)



回答2:


Here is some sample code that I have drawn a triangle for UIButton in my project and working great.

UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn setBackgroundColor:[UIColor redColor]];
btn.frame = CGRectMake(100, 100, 80, 53);
[self.view addSubview:btn];

UIBezierPath* bezierPath = UIBezierPath.bezierPath;
[bezierPath moveToPoint:CGPointMake(40, 43)];
[bezierPath addLineToPoint:CGPointMake(25, 53)];
[bezierPath addLineToPoint:CGPointMake(55, 53)];
[bezierPath closePath];

CAShapeLayer *shapeLayer = [CAShapeLayer layer];
shapeLayer.frame = btn.bounds;
shapeLayer.path = bezierPath.CGPath;
shapeLayer.fillColor = [UIColor whiteColor].CGColor;

[btn.layer addSublayer:shapeLayer];

and if you want to remove triangle of previous button when you click on next button then use following code snippet:

NSArray *arr = btn.layer.sublayers;

for (id class in arr) {

    if ([class isKindOfClass:[CAShapeLayer class]] ) {
        [class removeFromSuperlayer];
    }
}


来源:https://stackoverflow.com/questions/30825252/need-a-tip-with-uibezierpath-triangle-shape-like-instagram-sign-up-view

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!