2017-06-02 77 views
0

我想在我的UIView的底部邊緣添加鯊魚牙齒/三角形/尖刺圖案。事情是這樣的:UIView bottom egde pattern

enter image description here

現在,這可能只代碼?如何?

+0

當然 - UIBezierPath作爲掩模(很多很多的容易找到的例子在那裏)。 – DonMag

+0

你可以鏈接一些嗎?或舉一個例子作爲答案?我會接受,如果它的作品:) – Peter

+0

使用paintcode來執行這些任務! –

回答

3

這應該讓你對你的方式:

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    UIBezierPath *pth = [UIBezierPath bezierPath]; 

    CGFloat w = 240.0; 
    CGFloat h = 200.0; 

    CGPoint p = CGPointZero; 

    // start at top-left 
    [pth moveToPoint:p]; 

    // line to top-right 
    p.x = w; 
    [pth addLineToPoint:p]; 

    // line to bottom-right 
    p.y = h; 
    [pth addLineToPoint:p]; 

    // line to 40 left, 40 up 
    p.x -= 40; 
    p.y -= 40; 
    [pth addLineToPoint:p]; 

    // line to 40 left, 40 down (back to bottom) 
    p.x -= 40; 
    p.y += 40; 
    [pth addLineToPoint:p]; 

    // line to 40 left, 40 up 
    p.x -= 40; 
    p.y -= 40; 
    [pth addLineToPoint:p]; 

    // line to 40 left, 40 down (back to bottom) 
    p.x -= 40; 
    p.y += 40; 
    [pth addLineToPoint:p]; 

    // line to 40 left, 40 up 
    p.x -= 40; 
    p.y -= 40; 
    [pth addLineToPoint:p]; 

    // line to 40 left, 40 down (back to bottom) 
    p.x -= 40; 
    p.y += 40; 
    [pth addLineToPoint:p]; 

    // line to starting point - top-left 
    [pth closePath]; 

    // 240 x 200 rectangle at 40,40 
    CGRect r = CGRectMake(40, 40, w, h); 

    // create a UIView 
    UIView *v = [[UIView alloc] initWithFrame:r]; 
    v.backgroundColor = [UIColor redColor]; 

    // create a CAShapeLayer 
    CAShapeLayer *maskShape = [CAShapeLayer layer]; 

    // add the path to the CAShapeLayer 
    maskShape.path = pth.CGPath; 

    // set the view's layer mask to the CAShapeLayer 
    v.layer.mask = maskShape; 

    // add the masked subview to the view 
    [self.view addSubview:v]; 

}