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

后端 未结 2 1376
臣服心动
臣服心动 2021-01-01 03:27

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!



        
2条回答
  •  攒了一身酷
    2021-01-01 04:02

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

提交回复
热议问题