2015-06-19 18 views
7

Ich habe ein Problem mit Blick Clipping mit CAShapeLayer-UIBezierPath, ich mag den Inhalt Clip aber ich am Ende einen Schlaganfall bekommen (Rahmen) mit dem UIBezierPath, Dies ist mein CodeClip-Maskierung uiview mit CAShapeLayer und UIBezierPath

UIBezierPath *path2Path = [UIBezierPath bezierPath]; 
[path2Path moveToPoint:CGPointMake(206.745, 0)]; 
[path2Path addLineToPoint:CGPointMake(206.745, 97.613)]; 
[path2Path addLineToPoint:CGPointMake(0, 97.613)]; 
[path2Path addLineToPoint:CGPointMake(0, 0)]; 
[path2Path addLineToPoint:CGPointMake(87.28, 0)]; 
[path2Path addCurveToPoint:CGPointMake(103.808, 12.118) controlPoint1:CGPointMake(87.28, 0) controlPoint2:CGPointMake(86.555, 12.118)]; 
[path2Path addCurveToPoint:CGPointMake(119.466, 0) controlPoint1:CGPointMake(121.061, 12.118) controlPoint2:CGPointMake(119.466, 0)]; 
[path2Path addLineToPoint:CGPointMake(206.745, 0)]; 
[path2Path closePath]; 

[path2Path addClip]; 

CAShapeLayer *pathLayer = [CAShapeLayer layer]; 
pathLayer.frame=MYVIEW.bounds; 
pathLayer.path = path2Path.CGPath; 

pathLayer.strokeColor = [[UIColor blackColor] CGColor]; 
pathLayer.fillColor = [[UIColor clearColor] CGColor]; 

pathLayer.fillRule=kCAFillRuleEvenOdd; 
[MYVIEW.layer setMask:pathLayer]; 
[MYVIEW.layer setMasksToBounds:YES]; 

MYVIEW.backgroundColor=[UIColor greenColor]; 

Das Ergebnis dieses Code ist nur eine grüne Strich Linie, sind die Grenzen leer, wie diese http://i.stack.imgur.com/aehdo.png

Allerdings mag ich die Grenzen grün machen, abgeschnitten von diesem Hub

+0

Sie möchten den einzigen Teil des Bildes innerhalb des Bezier-Pfads anzeigen. Recht? – Rajesh

+0

ja genau. @RajeshMaurya – Aproram

+0

Darf ich es wissen? wo schreibst du den code? In Viewcontroller oder erstellte UIView-Unterklasse. – Rajesh

Antwort

13

als rob mayoff sagte Sie können dies problemlos tun, indem Sie die Ebenenmaske Ihrer Ansicht auf einen CAShapeLayer setzen.

UIBezierPath *myClippingPath = ... 
CAShapeLayer *mask   = [CAShapeLayer layer]; 
mask.path     = myClippingPath.CGPath; 
myView.layer.mask   = mask; 

In Swift

let myClippingPath = UIBezierPath(...) 
let mask   = CAShapeLayer() 
mask.path   = myClippingPath.CGPath 
layer.mask   = mask 
2

Danke für Antworten Jungs.

Falls jemand stundenlang auf SO für diese Frage passende Antwort nicht finden kann, wie ich gerade, ich einen Arbeits Kern in Swift 2.2 für die Maskierung/clipping UIView mit CGRect/UIBezierPath zusammengestellt haben tat:

https://gist.github.com/Flar49/7e977e81f1d2827f5fcd5c6c6a3c3d94

extension UIView { 
    func mask(withRect rect: CGRect, inverse: Bool = false) { 
     let path = UIBezierPath(rect: rect) 
     let maskLayer = CAShapeLayer() 

     if inverse { 
      path.appendPath(UIBezierPath(rect: self.bounds)) 
      maskLayer.fillRule = kCAFillRuleEvenOdd 
     } 

     maskLayer.path = path.CGPath 

     self.layer.mask = maskLayer 
    } 

    func mask(withPath path: UIBezierPath, inverse: Bool = false) { 
     let path = path 
     let maskLayer = CAShapeLayer() 

     if inverse { 
      path.appendPath(UIBezierPath(rect: self.bounds)) 
      maskLayer.fillRule = kCAFillRuleEvenOdd 
     } 

     maskLayer.path = path.CGPath 

     self.layer.mask = maskLayer 
    } 
} 

Verbrauch:

let viewSize = targetView.bounds.size 
let rect = CGRect(x: 20, y: 20, width: viewSize.width - 20*2, height: viewSize.height - 20*2) 

// Cuts rectangle inside view, leaving 20pt borders around 
targetView.mask(withRect: rect, inverse: true) 

// Cuts 20pt borders around the view, keeping part inside rect intact 
targetView.mask(withRect: rect) 

Hoffe, dass es jemand einige Zeit in der Zukunft :)

sparen
Verwandte Themen