我想要达到的目标是在雷达上绘制一个红色方形周边e screen made of 2 perpendicular lines t o将4个象限和10个绿色同心圆作为UIBezierPath放在自己的CAShapeLayer上然后在它们上面叠加一个红色的方形周边。
我所得到的是周边层是"覆盖"绿色线的底层。显示红色方形边界和底层圆圈的唯一方法是定义perimeter.opacity = 0.5。这样,红色周边和绿色线条都可见,但副作用是两个松散的不透明度,同时我希望它们的路径完全不透明和它们的背景(每个层的部分不包含路径)要透明。 请注意,我已将每个路径的背景颜色设置为UIColor.clear.cgColor,但这并非如此。似乎有任何影响。 对于这么简单的事情应该有一个简单的解决方案!
!!!!! UPDATE !!!!!
您可以轻松地重现复制和粘贴到游乐场的问题。确保打开助理编辑器,您将看到图纸
import UIKit
import XCPlayground
import PlaygroundSupport
var plottingPanel = UIView(frame: CGRect(x: 100.0, y: 0.0, width: 375.0, height: 375.0))
let plottingPanelHeight = plottingPanel.bounds.size.height
let plottingPanelWidth = plottingPanel.bounds.size.width
let originX = plottingPanel.bounds.size.width/2
let originY = plottingPanel.bounds.size.height/2
let originPoint = CGPoint(x: originX, y: originY)
let west = CGPoint(x: 0,y: originY)
let east = CGPoint(x: plottingPanelWidth,y: originY)
let north = CGPoint(x :originX,y: 0)
let sud = CGPoint(x: originX,y: plottingPanelHeight)
let radius = plottingPanelWidth/2
//Draw Circles
let circles = 10
for i in 0..<circles{
let circle1Layer = CAShapeLayer()
let circleRadius = (radius/CGFloat(circles)) * CGFloat(circles - i)
circle1Layer.path = UIBezierPath(arcCenter: originPoint, radius: circleRadius , startAngle: CGFloat(0), endAngle:CGFloat(M_PI * 2), clockwise: true ).cgPath
circle1Layer.strokeColor = UIColor.green.cgColor
circle1Layer.backgroundColor = UIColor.clear.cgColor
circle1Layer.lineWidth = 1
plottingPanel.layer.addSublayer(circle1Layer)
}
PlaygroundPage.current.liveView = plottingPanel
PlaygroundPage.current.needsIndefiniteExecution = true
// Draw Quadrants
let bezierPathQuadrants = UIBezierPath()
bezierPathQuadrants.move(to: west)
bezierPathQuadrants.addLine(to: east)
bezierPathQuadrants.move(to: north)
bezierPathQuadrants.addLine(to: sud)
let cross = CAShapeLayer()
cross.path = bezierPathQuadrants.cgPath
cross.strokeColor = UIColor.green.cgColor
cross.lineWidth = 1
cross.backgroundColor = UIColor.clear.cgColor
cross.isOpaque = true
plottingPanel.layer.addSublayer(cross)
// Draw Perimeter
var perimeterRectSideMts = CGFloat(400)
func ptsFromCm(_ cm: Float) -> CGFloat{
let maxRadius = CGFloat(1000) // cm
return (plottingPanelHeight / (2 * maxRadius) ) * CGFloat(cm) // 6,25
}
let sideMts = Float(perimeterRectSideMts/2)
let sidePts = ptsFromCm(sideMts)
let pointA = CGPoint(x: (originPoint.x - sidePts) , y: (originPoint.y - sidePts) )
let pointB = CGPoint(x: originPoint.x + sidePts, y: originPoint.y - sidePts)
let pointC = CGPoint(x: originPoint.x - sidePts, y: originPoint.y + sidePts)
let pointD = CGPoint(x: originPoint.x + sidePts, y: originPoint.y + sidePts)
let bezierPath = UIBezierPath()
bezierPath.move(to: pointA)
bezierPath.addLine(to: pointB)
bezierPath.addLine(to: pointD)
bezierPath.addLine(to: pointC)
bezierPath.close()
let perimeter = CAShapeLayer()
perimeter.path = bezierPath.cgPath
perimeter.strokeColor = UIColor.red.cgColor
perimeter.lineWidth = 2
perimeter.backgroundColor = UIColor.clear.cgColor
perimeter.isOpaque = false
perimeter.opacity = 0.5
// Tried this with no luck
var maskLayer = CAShapeLayer()
maskLayer.frame = perimeter.bounds;
maskLayer.path = perimeter.path
perimeter.mask = maskLayer;
plottingPanel.layer.addSublayer(perimeter)