2017-06-02 9 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]; 

}