我有两个UIView类ControllerWaveForm
和WaveFormView
在ControllerWaveForm
我有
class ControllerWaveForm: UIView{
var mWaveformView : WaveFormView?
init(frame: CGRect) {
playButton = UIButton(frame: playButtonRect)// this button for testing
playButton.addTarget(self, action: #selector(self.clickPlay), for: .touchUpInside)
mWaveformView = WaveFormView(frame: rectFrame,deletgate: self)
addSubview(mWaveformView!)
addSubview(playButton)
}
func clickPlay(){
mWaveformView?.setNeedsDisplay()
}
} extension ControllerWaveForm : WaveFormMoveProtocol {
func waveformDraw() {
print("mWaveFormProtocol callback ")
mWaveformView?.setNeedsDisplay()
}
}
这是我的WaveFormView
课程和WaveFormMoveProtocol
协议
protocol WaveFormMoveProtocol {
func waveformDraw()
}
class WaveFormView: UIView {
var mWaveFormProtocol : WaveFormMoveProtocol?
init(frame: CGRect,deletgate : WaveFormMoveProtocol?) {
super.init(frame: frame)
/* I add some CAShapeLayer here
*/
if let delegateProtocol = deletgate {
mWaveFormProtocol = delegateProtocol
}
}
override func draw(_ rect: CGRect) {
print("call draw(_ rect: CGRect) ")
if let waveFormProtocol = mWaveFormProtocol {
print("set callback ")
waveFormProtocol.waveformDraw()
}else{
print("mWaveFormProtocol is nil ")
}
}
}
当我将ControllerWaveForm添加到我的viewController时,控制台将打印:
"call draw(_ rect: CGRect) "
"set callback"
"mWaveFormProtocol callback"
。但如果我点击playButton控制台仍然打印
"call draw(_ rect: CGRect) "
"set callback"
"mWaveFormProtocol callback"
这意味着mWaveformView?.setNeedsDisplay()
无法在委托内部调用draw(_ rect: CGRect)
。那么请你解释一下,非常感谢你!