我正在编写 Swift 4.2 iOS应用。
我正在使用以下代码绘制路径。当推入VC时它可以完美工作,但是如果我导航回该VC (在navigationController中弹出) path.stroke()崩溃。
override public func draw(_ rect: CGRect) {
drawGauge()
}
public override init(frame: CGRect) {
super.init(frame: frame)
drawGauge()
}
required public init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
drawGauge()
}
override public func layoutSubviews() {
super.layoutSubviews()
drawGauge()
}
func drawGauge() {
layer.sublayers = []
drawSmartArc()
drawNeedle()
drawNeedleCircle()
}
func drawSmartArc() {
var angles = getAllAngles()
let arcColors = colorCodes.components(separatedBy: ",")
let center = CGPoint(x: bounds.width / 2, y: bounds.height / 2)
var arcs = [ArcModel(startAngle: angles[0],
endAngle: angles.last!,
strokeColor: _shadowColor,
arcCap: CGLineCap.round,
center:CGPoint(x: bounds.width / 2, y: (bounds.height / 2)+5))]
for index in 0..<arcColors.count {
let arc = ArcModel(startAngle: angles[index], endAngle: angles[index+1],
strokeColor: UIColor(hex: arcColors[index]),
arcCap: CGLineCap.butt,
center: center)
arcs.append(arc)
}
arcs.rearrange(from: arcs.count-1, to: 2)
arcs[1].arcCap = self.capStyle
arcs[2].arcCap = self.capStyle
for i in 0..<arcs.count {
createArcWith(startAngle: arcs[i].startAngle, endAngle: arcs[i].endAngle, arcCap: arcs[i].arcCap, strokeColor: arcs[i].strokeColor, center: arcs[i].center)
}
if blinkAnimate {
blink()
}
}
func createArcWith(startAngle: CGFloat, endAngle: CGFloat, arcCap: CGLineCap, strokeColor: UIColor, center:CGPoint) {
// 1
let center = center
let radius: CGFloat = max(bounds.width, bounds.height)/2 - self.frame.width/10
let lineWidth: CGFloat = self.frame.width/5
// 2
let path = UIBezierPath(arcCenter: center,
radius: radius,
startAngle: startAngle,
endAngle: endAngle,
clockwise: true)
// 3
path.lineWidth = lineWidth
path.lineCapStyle = arcCap
strokeColor.setStroke()
do {
try path.stroke() //CRASHING WHEN NAVIGATING BACK TO THIS VC
} catch let error {
Log.b(error.localizedDescription)
}
}
线程1:EXC_BAD_ACCESS(代码= 1,地址= 0x6b00000038)
我尝试使用try,但显示:
在“ try”表达式中没有对throwing函数的调用。 “ catch”块不可用,因为“ do”块中没有抛出任何错误
答案 0 :(得分:0)
您不能仅在应用程序执行的某个时刻绘制路径。图纸上下文需要设置为特定的上下文。通常,您会在视图的draw(_:)
方法中为您设置上下文,从而绘制命令。失败的话,您需要在绘制之前自行设置图形上下文(非常与众不同。)
如何/在何处调用createArcWith()函数?