【发布时间】:2011-03-18 08:39:18
【问题描述】:
是否可以将 CALayer 剪辑到任意路径?我知道我可以剪辑到超级层的边界,但在这种情况下,我需要更加规范。
TIA,亚当
【问题讨论】:
标签: core-animation quartz-graphics calayer
是否可以将 CALayer 剪辑到任意路径?我知道我可以剪辑到超级层的边界,但在这种情况下,我需要更加规范。
TIA,亚当
【问题讨论】:
标签: core-animation quartz-graphics calayer
使用CAShapeLayer 作为要剪辑的图层的蒙版。 CAShapeLayer 有一个采用 CGPathRef 的路径属性。
例如:
let mask = CAShapeLayer()
mask.path = path // Where path is some CGPath
layer.mask = mask // Where layer is your CALayer
// This also works for other CAShapeLayers
【讨论】:
是的,您可以覆盖自定义层的 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 作为蒙版。
【讨论】: