Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/macos/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Macos 为CGMutablePath绘制阴影,不是闭合的,只是一条线_Macos_Calayer_Nsview_Shadow - Fatal编程技术网

Macos 为CGMutablePath绘制阴影,不是闭合的,只是一条线

Macos 为CGMutablePath绘制阴影,不是闭合的,只是一条线,macos,calayer,nsview,shadow,Macos,Calayer,Nsview,Shadow,我有一个函数Track.getShadowPath(),它返回一个CGMutablePath 返回CGMutablePath的函数 static func getShadowPath() -> CGMutablePath{ let trackPath = CGMutablePath() let initPoint = Track.tracks.first?.initPoint let heigthShift = CGPoint(x: 0, y: 100)

我有一个函数
Track.getShadowPath()
,它返回一个
CGMutablePath

返回CGMutablePath的函数

static func getShadowPath() -> CGMutablePath{

    let trackPath = CGMutablePath()

    let initPoint = Track.tracks.first?.initPoint


    let heigthShift = CGPoint(x: 0, y: 100)
    trackPath.move(to: initPoint! - heigthShift)

    var yShift = CGPoint(x: 0, y: 0)

    for track:Track in Track.tracks{

        let q = track.getQuadrant()
        if(q == 2 || q == 5){
            yShift.y = yShift.y + track.endPoint.y - track.initPoint.y
        }
        else {
            trackPath.addLine(to: track.endPoint - heigthShift - yShift)
        }
    }

    return trackPath
}
然后将此路径添加到
CAShapeLayer
,并将该层作为子层添加到我的视图中。我希望这条路有阴影的一面。但是当它渲染时,会产生一个阴影,就好像路径是闭合的一样。我怎样才能达到我的目的

NSView

class DemoView: NSView {
    var trackShape:CAShapeLayer!
    var trackShadowShape:CAShapeLayer!

override init(frame: CGRect){
    super.init(frame: frame)

    trackShape = CAShapeLayer()

    trackShape.strokeColor = NSColor.black.cgColor
    trackShape.lineWidth = 3
    trackShape.lineJoin = kCALineJoinBevel
    trackShape.lineCap = kCALineCapRound
    trackShape.fillColor = nil

    trackShadowShape = CAShapeLayer()

    trackShadowShape.lineWidth = 14
    trackShadowShape.lineJoin = kCALineJoinBevel
    trackShadowShape.lineCap = kCALineCapRound
    trackShadowShape.fillColor = nil
    trackShadowShape.shadowColor = NSColor.blue.cgColor
    trackShadowShape.shadowRadius = 10

    self.layer?.addSublayer(trackShadowShape)
    self.layer?.addSublayer(trackShape)
 }

func redrawTrack(){
    trackShape.path = Track.getCompletePath()
    trackShadowShape.shadowPath = Track.getShadowPath()
    trackShadowShape.shadowRadius = 20
    trackShadowShape.shadowOpacity = 1
    trackShadowShape.fillColor = NSColor.clear.cgColor
    trackShadowShape.lineWidth = 10

 }
}
非预期结果


我用以下方法解决了这个问题:

copy(strokingWithWidth lineWidth: CGFloat, lineCap: CGLineCap, lineJoin: CGLineJoin, miterLimit: CGFloat, transform: CGAffineTransform = default)
根据文件

返回一个新路径,该路径等效于使用 有力的一击

因此,它基本上创建了一个新的
CGPath
,这是您提供的路径的笔划轮廓,具有所需的宽度、上限等。。。我让我的
getShadowPath
函数返回这个新的计算路径:

return trackPath.copy(strokingWithWidth: 6, lineCap: CGLineCap.round, lineJoin: CGLineJoin.round, miterLimit: 10) as! CGMutablePath
然后,我将该路径用作我图层的阴影路径。现在的结果是预期的结果:


在Objective-c中可以找到类似的答案

为什么要使用两个形状层?为什么不将轨迹层配置为绘制阴影呢。不要指定阴影路径,只让核心图形根据其绘制的形状计算阴影路径。
shadowPath
的文档特别说明它将被填充。我需要使用两个形状层,因为对于我的应用程序,有时我需要阴影不跟随轨迹层。另一方面,如果阴影路径总是被填充的,当人们想要一个线状阴影时该怎么办?如果在阴影层中设置
路径
,不设置
阴影路径
,而是将
阴影偏移量
设置为
CGSizeZero
,你会得到什么,而
笔划颜色
阴影颜色
相同吗?这应该画出路径和阴影,路径应该在阴影的中间,和它一样的颜色,所以希望能融入进来。如果在非白色的物体上绘制,则可能不会,因为路径将是不透明的,而阴影将是半透明的。你可以试着摆弄一下
strokeColor
的alpha,它越透明,我认为阴影越亮。它沿路径绘制阴影,但无法消除笔划。如果我将“线宽”设置为1,阴影也会变薄,几乎看不到它。更改线条颜色上的alpha也会更改阴影的alpha。。。