【问题标题】:Clipping a CALayer to arbitrary path将 CALayer 裁剪到任意路径
【发布时间】:2011-03-18 08:39:18
【问题描述】:

是否可以将 CALayer 剪辑到任意路径?我知道我可以剪辑到超级层的边界,但在这种情况下,我需要更加规范。

TIA,亚当

【问题讨论】:

    标签: core-animation quartz-graphics calayer


    【解决方案1】:

    使用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
    

    【讨论】:

      【解决方案2】:

      是的,您可以覆盖自定义层的 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 作为蒙版。

      【讨论】:

        猜你喜欢
        • 2019-10-21
        • 1970-01-01
        • 2011-04-25
        • 1970-01-01
        • 2016-08-01
        • 2013-10-06
        • 2014-12-08
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多