2010-07-20 92 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作为掩码。