在委托内调用setNeedsDisplay()不起作用(Swift 3.0)

时间:2016-11-03 04:37:11

标签: ios swift

我有两个UIView类ControllerWaveFormWaveFormView

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)。那么请你解释一下,非常感谢你!

0 个答案:

没有答案