2012-06-01 9 views
7

Estoy tratando de recortar una capa con otra, pero, en lugar de crear una máscara (B) y recortar la capa (A) obteniendo una capa recortada A con la forma de B, quiero para obtener una capa con forma de a y un 'agujero' creado por el cultivo con la capa B.Recortar un CAShapeLayer recuperar la ruta externa

*************    ***Layer A*** 
* Layer A *    ************* 
* ***** *    **** ***** 
* * B * *  ->  **** ***** Layer A without shape B 
* ***** *    **** ***** 
*   *    ************* 
*************    ************* 

¿Cómo puedo obtener la capa recortada a?

Respuesta

20

Tiene que crear una máscara que cubra el área que desea mantener. Esto se puede hacer utilizando una regla de relleno par e impar y crear una ruta para una capa de forma con ambos rectángulos. Puede crear la forma de esta manera (donde los dos rectángulos serían sus dos marcos). Luego configura esto como la máscara para obtener el resultado que busca.

CAShapeLayer *maskWithHole = [CAShapeLayer layer]; 

// Both frames are defined in the same coordinate system 
CGRect biggerRect = CGRectMake(30, 50, 120, 200); 
CGRect smallerRect = CGRectMake(80, 100, 50, 80); 

UIBezierPath *maskPath = [UIBezierPath bezierPath]; 
[maskPath moveToPoint:CGPointMake(CGRectGetMinX(biggerRect), CGRectGetMinY(biggerRect))]; 
[maskPath addLineToPoint:CGPointMake(CGRectGetMinX(biggerRect), CGRectGetMaxY(biggerRect))]; 
[maskPath addLineToPoint:CGPointMake(CGRectGetMaxX(biggerRect), CGRectGetMaxY(biggerRect))]; 
[maskPath addLineToPoint:CGPointMake(CGRectGetMaxX(biggerRect), CGRectGetMinY(biggerRect))]; 
[maskPath addLineToPoint:CGPointMake(CGRectGetMinX(biggerRect), CGRectGetMinY(biggerRect))]; 

[maskPath moveToPoint:CGPointMake(CGRectGetMinX(smallerRect), CGRectGetMinY(smallerRect))]; 
[maskPath addLineToPoint:CGPointMake(CGRectGetMinX(smallerRect), CGRectGetMaxY(smallerRect))]; 
[maskPath addLineToPoint:CGPointMake(CGRectGetMaxX(smallerRect), CGRectGetMaxY(smallerRect))]; 
[maskPath addLineToPoint:CGPointMake(CGRectGetMaxX(smallerRect), CGRectGetMinY(smallerRect))]; 
[maskPath addLineToPoint:CGPointMake(CGRectGetMinX(smallerRect), CGRectGetMinY(smallerRect))]; 

[maskWithHole setPath:[maskPath CGPath]]; 
[maskWithHole setFillRule:kCAFillRuleEvenOdd]; 
[maskWithHole setFillColor:[[UIColor orangeColor] CGColor]]; 
0

Swift 3,0 solución:

class MakeTransparentHoleOnOverlayView: UIView { 

    @IBOutlet weak var transparentHoleView: UIView! 

    // MARK: - Drawing 

    override func draw(_ rect: CGRect) { 
     super.draw(rect) 

     if self.transparentHoleView != nil { 
      // Ensures to use the current background color to set the filling color 
      self.backgroundColor?.setFill() 
      UIRectFill(rect) 

      let layer = CAShapeLayer() 
      let path = CGMutablePath() 

      // Make hole in view's overlay 
      // NOTE: Here, instead of using the transparentHoleView UIView we could use a specific CFRect location instead... 
      path.addRect(transparentHoleView.frame) 
      path.addRect(bounds) 

      layer.path = path 
      layer.fillRule = kCAFillRuleEvenOdd 
      self.layer.mask = layer 
     } 
    } 

    override func layoutSubviews() { 
     super.layoutSubviews() 
    } 

    // MARK: - Initialization 

    required init?(coder aDecoder: NSCoder) { 
     super.init(coder: aDecoder) 
    } 

    override init(frame: CGRect) { 
     super.init(frame: frame) 
    } 
} 
Cuestiones relacionadas