2010-07-20 116 views

回答

14

使用CAShapeLayer的面具,你要裁剪的層。 CAShapeLayer有一個帶有CGPathRef的路徑屬性。

1

是的,你可以覆蓋你自定義圖層的drawInContext。

func addPathAndClipIfNeeded(ctx:CGContext) { 
    if (self.path != nil) { 
     CGContextAddPath(ctx,self.path); 
     if (self.stroke) { 
      CGContextSetLineWidth(ctx, self.lineWidth); 
      CGContextReplacePathWithStrokedPath(ctx); 
     } 
     CGContextClip(ctx); 
    } 
} 
override public func drawInContext(ctx: CGContext) { 
    super.drawInContext(ctx) 
    addPathAndClipIfNeeded(ctx) 
} 

或者您可以創建一個CAShapeLayer作爲掩碼。