我在UICollectionViewCell
中有一个自定义的“滑块”视图。当我滚动滚动以在屏幕上显示另一个单元时,它经常崩溃并显示以下错误:
***由于未捕获的异常“ CALayerInvalidGeometry”而终止应用程序,原因:“ CALayer位置包含NaN:[nan 8.75]”
我的代码的相关部分是此函数,该函数在单元格的init上调用:
func moveSlider(to time: CMTime) {
let totalSeconds: Double
if let duration = delegate?.videoDuration {
totalSeconds = CMTimeGetSeconds(duration)
} else {
totalSeconds = .greatestFiniteMagnitude
}
adjustStartTime(to: time)
slider.value = CMTimeGetSeconds(time)/totalSeconds
}
更新slider.value
,然后在我的Slider类(这是UIControl
的子类)中调用这段代码:
private func updateLayerFrames() {
CATransaction.begin()
CATransaction.setDisableActions(true)
let trackHeight: CGFloat = 3
let trackFrame = CGRect(x: 0, y: bounds.midY - trackHeight/2, width: bounds.width, height: trackHeight)
trackLayer.frame = trackFrame
trackLayer.setNeedsDisplay()
let thumbCenter = CGFloat(position(for: value))
thumbLayer.frame = CGRect(
x: thumbCenter - (thumbWidth + SliderThumbLayer.extraHighlightedWidth) / 2.0,
y: 0.0,
width: thumbWidth + SliderThumbLayer.extraHighlightedWidth/2,
height: thumbWidth + SliderThumbLayer.extraHighlightedWidth/2
)
thumbLayer.setNeedsDisplay()
CATransaction.commit()
}
func position(for value: Double) -> Double {
return Double(bounds.width - thumbWidth) * (value - minimumValue) /
(maximumValue - minimumValue) + Double(thumbWidth / 2.0)
}
该updateLayerFrames
代码中的某处崩溃。我该如何解决?