为 CGMutablePath 绘制阴影,不是闭合的,只是一条线
Drawing a shadow for a CGMutablePath, not closed, just a line
我有函数 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)
根据文档
Returns a new path equivalent to the results of drawing the path with
a solid stroke.
所以基本上它创建了一个新的 CGPath
这是你提供的路径的笔划轮廓,具有所需的宽度、上限等...我制作了我的 getShadowPath
函数 return 这个新的计算路径:
return trackPath.copy(strokingWithWidth: 6, lineCap: CGLineCap.round, lineJoin: CGLineJoin.round, miterLimit: 10) as! CGMutablePath
然后我将该路径用作图层的 shadowPath。结果现在是预期的结果:
可以在 Objective-c 中找到类似的答案
我有函数 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)
根据文档
Returns a new path equivalent to the results of drawing the path with a solid stroke.
所以基本上它创建了一个新的 CGPath
这是你提供的路径的笔划轮廓,具有所需的宽度、上限等...我制作了我的 getShadowPath
函数 return 这个新的计算路径:
return trackPath.copy(strokingWithWidth: 6, lineCap: CGLineCap.round, lineJoin: CGLineJoin.round, miterLimit: 10) as! CGMutablePath
然后我将该路径用作图层的 shadowPath。结果现在是预期的结果:
可以在 Objective-c 中找到类似的答案