KMAdsWebView.swift 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. import Cocoa
  2. import WebKit
  3. protocol AdsWebViewDelegate: AnyObject {
  4. func kmAdViewClicked(_ adView: KMAdsWebView)
  5. func kmAdViewClose(_ adView: KMAdsWebView)
  6. }
  7. enum KMADViewDirections: Int {
  8. case up
  9. case down
  10. }
  11. // Banner Ads Width,Height
  12. let kAD_View_Width = 728.0
  13. let kAD_View_Height = 90.0
  14. // Banner Ads refresh rate in second, 新广告会自动刷新,所以时间延长
  15. let kAD_Refresh_Rate = 6000.0
  16. class KMAdsWebView: NSView, WKNavigationDelegate, CAAnimationDelegate {
  17. weak var adDelegate: AdsWebViewDelegate?
  18. private var closeButton: NSButton!
  19. var clickButton: NSButton!
  20. private var currentPage: Int = 0
  21. private var completionHandler: ((Int) -> Void)?
  22. var adsImageView: NSImageView!
  23. var adsInfo: KMAdsInfo!
  24. var adPosY: CGFloat = 30.0
  25. override init(frame frameRect: NSRect) {
  26. super.init(frame: frameRect)
  27. wantsLayer = true
  28. self.frame = NSRect(x: 0, y: 0, width: kAD_View_Width, height: kAD_View_Height)
  29. autoresizingMask = [.minXMargin, .maxXMargin]
  30. adsImageView = NSImageView.init(frame: self.bounds)
  31. adsImageView.autoresizingMask = [.width, .height]
  32. adsImageView.imageScaling = .scaleAxesIndependently
  33. addSubview(adsImageView)
  34. adPosY = 30.0
  35. clickButton = NSButton.init(frame: self.bounds)
  36. clickButton.isHidden = false
  37. clickButton.autoresizingMask = [.width, .height]
  38. clickButton.isBordered = false
  39. clickButton.title = ""
  40. clickButton.target = self
  41. clickButton.action = #selector(buttonItemClicked(_:))
  42. addSubview(clickButton)
  43. closeButton = NSButton.init(frame: NSRect(x: frameRect.size.width - 30, y: frameRect.size.height - 30, width: 30, height: 30))
  44. closeButton.isHidden = false
  45. closeButton.imagePosition = .imageOnly
  46. closeButton.imageScaling = .scaleProportionallyUpOrDown
  47. closeButton.autoresizingMask = [.maxXMargin, .minYMargin]
  48. closeButton.image = NSImage(named: "ad_cancel_button00")
  49. closeButton.isBordered = false
  50. closeButton.target = self
  51. closeButton.action = #selector(buttonItemClicked_Close(_:))
  52. addSubview(closeButton)
  53. NotificationCenter.default.addObserver(self, selector: #selector(handleUserHaveClickRateUsNotification(_:)), name: NSNotification.Name("kUserHaveClickRateUsNotification"), object: nil)
  54. NotificationCenter.default.addObserver(self, selector: #selector(recommondInfoUpdateNoti), name: NSNotification.Name("KMRecommondInfoUpdateNoti"), object: nil)
  55. }
  56. required init?(coder: NSCoder) {
  57. fatalError("init(coder:) has not been implemented")
  58. }
  59. deinit {
  60. adDelegate = nil
  61. completionHandler = nil
  62. NotificationCenter.default.removeObserver(self)
  63. }
  64. func restoreDefaultAdPosY() {
  65. adPosY = 30.0
  66. }
  67. func reloadData() {
  68. DispatchQueue.main.async {
  69. self.adsImageView.image = self.adsInfo.adsImage
  70. }
  71. }
  72. func resizeWithOldSuperviewSize(oldSize: NSSize) {
  73. var width = kAD_View_Width
  74. var height = kAD_View_Height
  75. if superview?.frame.size.width ?? 0 < width {
  76. let newWidth = (superview?.frame.size.width ?? 0) - 20
  77. height = height / width * newWidth
  78. width = newWidth
  79. }
  80. self.frame = NSRect(x: (superview?.frame.size.width ?? 0 - width) / 2.0, y: frame.origin.y, width: width, height: height)
  81. }
  82. func beginSheetModalForView(view: NSView, directions: KMADViewDirections, animated: Bool, completionHandler handler: ((Int) -> Void)?) {
  83. self.completionHandler = handler
  84. if adPosY < 0 {
  85. restoreDefaultAdPosY()
  86. }
  87. let tWidth = frame.size.width
  88. let tHeight = frame.size.height
  89. view.addSubview(self)
  90. if animated {
  91. switch directions {
  92. case .up:
  93. frame = NSRect(x: (view.frame.size.width - tWidth) / 2.0, y: view.frame.size.height, width: tWidth, height: tHeight)
  94. animator().frame = NSRect(x: frame.origin.x, y: view.frame.size.height - tHeight - adPosY, width: tWidth, height: tHeight)
  95. case .down:
  96. frame = NSRect(x: (view.frame.size.width - tWidth) / 2.0, y: -tHeight, width: tWidth, height: tHeight)
  97. animator().frame = NSRect(x: frame.origin.x, y: adPosY, width: tWidth, height: tHeight)
  98. default:
  99. frame = NSRect(x: 0, y: 0, width: tWidth, height: tHeight)
  100. }
  101. } else {
  102. switch directions {
  103. case .up:
  104. frame = NSRect(x: (view.frame.size.width - tWidth) / 2.0, y: view.frame.size.height - tHeight - adPosY, width: tWidth, height: tHeight)
  105. case .down:
  106. frame = NSRect(x: (view.frame.size.width - tWidth) / 2.0, y: adPosY, width: tWidth, height: tHeight)
  107. default:
  108. frame = NSRect(x: 0, y: 0, width: tWidth, height: tHeight)
  109. }
  110. }
  111. adsImageView.frame = bounds
  112. closeButton.frame = NSRect(x: self.bounds.size.width - 30, y: self.bounds.size.height - 30, width: 30, height: 30)
  113. currentPage = 0
  114. }
  115. @objc func recommondInfoUpdateNoti() {
  116. DispatchQueue.main.async {
  117. self.reloadData()
  118. }
  119. }
  120. //MARK: - Action
  121. @objc func buttonItemClicked_Open(_ sender: Any) {
  122. guard let newURL = (sender as? NSURL) else {
  123. return
  124. }
  125. NSWorkspace.shared.open(newURL as URL)
  126. adDelegate?.kmAdViewClicked(self)
  127. if let completionHandler = self.completionHandler {
  128. completionHandler(currentPage + 1)
  129. }
  130. removeFromSuperview()
  131. }
  132. @objc func buttonItemClicked(_ sender: Any) {
  133. guard let string = self.adsInfo?.adsURLLink else {
  134. return
  135. }
  136. let newURL = NSURL(string: string)
  137. NSWorkspace.shared.open(newURL! as URL)
  138. if let completionHandler = self.completionHandler {
  139. completionHandler(currentPage + 1)
  140. }
  141. adDelegate?.kmAdViewClicked(self)
  142. }
  143. @objc func buttonItemClicked_Close(_ sender: Any) {
  144. adDelegate?.kmAdViewClose(self)
  145. if let completionHandler = self.completionHandler {
  146. completionHandler(0)
  147. }
  148. removeFromSuperview()
  149. }
  150. //MARK: - CAAnimation
  151. func animationDidStart(_ anim: CAAnimation) {
  152. }
  153. func animationDidStop(_ anim: CAAnimation, finished flag: Bool) {
  154. if !flag {
  155. return
  156. }
  157. closeButton.alphaValue = 1.0
  158. adsImageView.frame = bounds
  159. }
  160. // MARK: - WKNavigationDelegate
  161. @objc func handleUserHaveClickRateUsNotification(_ notification: Notification) {
  162. }
  163. }