StudyDetailViewController.swift 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. //
  2. // StudyDetailViewController.swift
  3. // xingchuangke
  4. //
  5. // Created by Virgil on 2019/5/13.
  6. // Copyright © 2019 Virgil. All rights reserved.
  7. //
  8. import UIKit
  9. class StudyDetailViewController: BaseViewController, WKNavigationDelegate {
  10. @IBOutlet weak var btnPlay: UIButton!
  11. @IBOutlet weak var btnGuanZhu: UIButton!
  12. @IBOutlet weak var lblCount: UILabel!
  13. @IBOutlet weak var lblDate: UILabel!
  14. @IBOutlet weak var lblTitle: UILabel!
  15. @IBOutlet weak var webContentView: UIView!
  16. @IBOutlet weak var viewContent: UIView!
  17. @IBOutlet weak var viewVideo: UIView!
  18. @IBOutlet weak var viewTop: UIView!
  19. @IBOutlet weak var scrollView: UIScrollView!
  20. var wkWebView: WKWebView!
  21. override func viewDidLoad() {
  22. super.viewDidLoad()
  23. self.title = "在线学习"
  24. initNavLeftBackButton()
  25. viewVideo.setSizeHeight(height: ScreenWidth / 375 * 213)
  26. viewVideo.setSizeWidth(width: ScreenWidth)
  27. btnGuanZhu.setCornerRadius(size: 6)
  28. viewContent.marginTop(top: 0, view: viewVideo)
  29. viewTop.setSizeHeight(height: viewContent.bottom())
  30. webContentView.marginTop(top: 0, view: viewTop)
  31. scrollView.contentSize.height = webContentView.bottom()
  32. lblTitle.text = getString(key: "title")
  33. let config = WKWebViewConfiguration.init()
  34. wkWebView = WKWebView.init(frame: webContentView.bounds, configuration: config)
  35. wkWebView.navigationDelegate = self
  36. self.webContentView.addSubview(wkWebView)
  37. let count = "\(getIntValue(key: "click_num"))+"
  38. lblCount.attributedText = CommonViewUntils.getAttributedString(str: "\(count)人学过", left: 0, right: count.length() + 1, color: CommonUntils.getUIColorFromRGB(rgbValue: 0xffb400, alpha: 1.0), otherColor: CommonUntils.getUIColorFromRGB(rgbValue: 0x999999, alpha: 1.0))
  39. btnPlay.setCenterForSuperView()
  40. viewVideo.bringSubviewToFront(btnPlay)
  41. lblDate.text = getString(key: "creaetime")
  42. initPlay()
  43. loadData()
  44. }
  45. func initPlay() {
  46. let controlView = ZFPlayerControlView()
  47. controlView.fastViewAnimated = true
  48. controlView.autoHiddenTimeInterval = 5
  49. controlView.autoFadeTimeInterval = 0.5
  50. controlView.prepareShowLoading = true
  51. let playManager = ZFAVPlayerManager()
  52. vcPlayer = ZFPlayerController(playerManager: playManager, containerView: viewVideo)
  53. vcPlayer.controlView = controlView
  54. //后台继续 播放
  55. vcPlayer.pauseWhenAppResignActive = false
  56. vcPlayer.orientationWillChange = {[weak self] (player, isFullScreen) in
  57. self!.setNeedsStatusBarAppearanceUpdate()
  58. }
  59. ///播放完成
  60. vcPlayer.playerDidToEnd = {[weak self] (asset) in
  61. self!.vcPlayer.currentPlayerManager.stop?()
  62. }
  63. vcPlayer.customAudioSession = true
  64. vcPlayer.playerReadyToPlay = { (asse, url) in
  65. do {
  66. try AVAudioSession.sharedInstance().setCategory(.playback, options: .allowBluetooth)
  67. try AVAudioSession.sharedInstance().setActive(true, options: .notifyOthersOnDeactivation)
  68. } catch {}
  69. }
  70. }
  71. var vcPlayer: ZFPlayerController!
  72. func playView(url: String) {
  73. vcPlayer.assetURLs = [URL(string: url)!]
  74. //vcPlayer.playTheIndex(0);
  75. }
  76. func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
  77. var webheight = 0.0
  78. // 获取内容实际高度
  79. self.wkWebView.evaluateJavaScript("document.body.scrollHeight") { [unowned self] (result, _) in
  80. if let tempHeight: Double = result as? Double {
  81. webheight = tempHeight
  82. print("webheight: \(webheight)")
  83. }
  84. DispatchQueue.main.async { [unowned self] in
  85. self.webContentView.setSizeHeight(height: CGFloat(webheight))
  86. self.wkWebView.setSizeHeight(height: CGFloat(webheight))
  87. self.scrollView.contentSize.height = self.webContentView.bottom()
  88. }
  89. }
  90. }
  91. @IBAction func btnMenuClick(_ sender: AnyObject) {
  92. switch (sender as! UIButton).tag {
  93. case 1001: // 播放 暂停
  94. self.vcPlayer.playTheIndex(0)
  95. break
  96. case 1002: // 关注
  97. uploadGZ()
  98. break
  99. case 1003: //
  100. break
  101. case 1004: //
  102. break
  103. case 1005: //
  104. break
  105. case 1006: //
  106. break
  107. default:
  108. break
  109. }
  110. }
  111. // MARK: =============加载数据===============
  112. func loadData() {
  113. let url = RequestURL.onlineDetail
  114. let params = NSMutableDictionary()
  115. params.setValue(getString(key: "id"), forKey: "id")
  116. loadDataInfo(url: url, params: params, tag: 1001)
  117. }
  118. func uploadGZ() {
  119. let url = RequestURL.startStudy
  120. let params = NSMutableDictionary()
  121. params.setValue(getString(key: "id"), forKey: "id")
  122. submitData(url: url, params: params, tag: 1002)
  123. }
  124. override func returnData(tag: Int) {
  125. if tag == 1001 {
  126. let onlineDetail = dicData["onlineDetail"] as! NSDictionary
  127. dicData.setDic(dic: onlineDetail)
  128. playView(url: getString(key: "video_url"))
  129. let isFlow = getIntValue(key: "isFlow")
  130. lblDate.text = getString(key: "creaetime")
  131. wkWebView.loadDataForUrl(url: getString(key: "webUrl"))
  132. if isFlow == 1 {
  133. btnGuanZhu.setTitle("已关注", for: .normal)
  134. btnGuanZhu.backgroundColor = CommonUntils.getUIColorFromRGB(rgbValue: 0xcccccc, alpha: 1.0)
  135. btnGuanZhu.isEnabled = false
  136. } else {
  137. btnGuanZhu.setTitle("关注", for: .normal)
  138. btnGuanZhu.backgroundColor = CommonUntils.getUIColorFromRGB(rgbValue: 0xfcb400, alpha: 1.0)
  139. }
  140. } else if tag == 1002 {
  141. btnGuanZhu.setTitle("已关注", for: .normal)
  142. btnGuanZhu.backgroundColor = CommonUntils.getUIColorFromRGB(rgbValue: 0xcccccc, alpha: 1.0)
  143. }
  144. }
  145. override func returnError(tag: Int, type: String) {
  146. }
  147. override func viewWillAppear(_ animated: Bool) {
  148. super.viewWillAppear(animated)
  149. if self.vcPlayer != nil {
  150. self.vcPlayer.isViewControllerDisappear = false
  151. }
  152. }
  153. override func viewWillDisappear(_ animated: Bool) {
  154. super.viewWillDisappear(animated)
  155. if self.vcPlayer != nil {
  156. self.vcPlayer.isViewControllerDisappear = true
  157. }
  158. }
  159. override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
  160. super.init(nibName: "StudyDetailViewController", bundle: nil)
  161. }
  162. required init?(coder aDecoder: NSCoder) {
  163. fatalError("init(coder:) has not been implemented")
  164. }
  165. }