2012-06-01 23 views
7

Bir katmanı diğeriyle kırpmaya çalışıyorum, ancak (B) bir maske oluşturmaya ve katman (A) B biçiminde kırpılmış bir katman A olsun, kırpma tabaka B. Ben kırpılmış tabakanın a alabilirim nasılBir dış ağ yolunu almak için bir CAShapeLayer kırpma

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

ile kırpma yarattığı şekil a ile bir katman ve bir 'delik' alınır?

cevap

20

numaralı telefonu tutmak istediğiniz alanı kapsayan bir maske oluşturmanız gerekir. Bu, çift sayılı bir dolgu kuralı kullanılarak yapılabilir ve her iki dikdörtgen ile bir şekil katmanı için bir yol oluşturur. Şekli bu şekilde oluşturabilirsiniz (iki dikdörtgenin iki kareniz olacağı yer). Sonra bunu, sonuçta olduğunuz sonucu elde etmek için maske olarak ayarlarsınız.

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 çözeltisi:

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) 
    } 
} 
İlgili konular