2017-06-21 55 views
2

我使用BMPlayer庫,並希望實現自定義的控制,爲此,我有下面的類都確認以下協議斯威夫特 - 調用自定義的委託方法

@objc public protocol BMPlayerControlViewDelegate: class { 
    func controlView(controlView: BMPlayerControlView, didChooseDefition index: Int) 
    func controlView(controlView: BMPlayerControlView, didPressButton button: UIButton) 
    func controlView(controlView: BMPlayerControlView, slider: UISlider, onSliderEvent event: UIControlEvents) 
    @objc optional func controlView(controlView: BMPlayerControlView, didChangeVideoPlaybackRate rate: Float) 
} 

open class BMPlayerControlView: UIView { 
    open weak var delegate: BMPlayerControlViewDelegate? 
    open weak var player: BMPlayer? 

    // Removed rest of the code for clarity 

    open func onButtonPressed(_ button: UIButton) { 
     autoFadeOutControlViewWithAnimation() 
     if let type = ButtonType(rawValue: button.tag) { 
      switch type { 
      case .play, .replay: 
       if playerLastState == .playedToTheEnd { 
        hidePlayToTheEndView() 
       } 
      default: 
       break 
      } 
     } 
     delegate?.controlView(controlView: self, didPressButton: button) 
    } 
} 

我擴展BMPlayerControlView類擴展控制視圖使用下面的代碼。

class BMPlayerCustomControlStyle3: BMPlayerControlView { 

} 

class BMPlayerStyle3: BMPlayer { 

    class override func storyBoardCustomControl() -> BMPlayerControlView? { 
     return BMPlayerCustomControlStyle3() 
    } 
} 

我的問題是,我該如何調用didPressButton委託方法?我不希望覆蓋onButtonPressed,我嘗試以下

extension BMPlayerCustomControlStyle3:BMPlayerControlViewDelegate { 

    func controlView(controlView: BMPlayerControlView, didChooseDefition index: Int) { 

    } 

    func controlView(controlView: BMPlayerControlView, didPressButton button: UIButton) { 
     print("Did Press Button Invoked") 
    } 

    func controlView(controlView: BMPlayerControlView, slider: UISlider, onSliderEvent event: UIControlEvents) { 

    } 
} 

而這似乎並沒有工作,我缺少什麼嗎?

謝謝。

+0

嘗試把'super.controlView(controlView:controlView,didPressButton按鈕)'在你重寫的方法。讓我知道如果這不起作用。 –

回答

1

如果你希望你的BMPlayerControlView子類充當委託對象,你需要設置delegate屬性以及(和符合BMPlayerControlViewDelegate協議,你已經這樣做)。

一種方式這樣做是覆蓋delegate超屬性在子類:

class BMPlayerCustomControlStyle3: BMPlayerControlView { 

    override open weak var delegate: BMPlayerControlViewDelegate? { 
     get { return self } 
     set { /* fatalError("Delegate for internal use only!") */ } 
    } 
} 

當然,在內部使用像這樣的委託時,你不會它允許由BMPlayerControlView客戶使用的。上面的覆蓋set確保如果試圖這樣做,你得到一個錯誤。

+0

@IbrahimAzharArmar *您還需要*保持這一'擴展BMPlayerCustomControlStyle3:BMPlayerControlViewDelegate..'左右爲好;) –

+0

你是對的保羅,我做了小姐說,當我加入它給了我另一個錯誤,說'致命錯誤:委派僅供內部使用!' –

+0

@IbrahimAzharArmar註釋掉'/ * fatalError ... * /'聲明現在... –