KSMediaPickerCameraToolBar.swift 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. //
  2. // KSMediaPickerCameraToolBar.swift
  3. //
  4. //
  5. // Created by kinsun on 2019/3/11.
  6. //
  7. import UIKit
  8. extension KSMediaPickerCameraToolBar {
  9. open class button: UIControl {
  10. public enum style: UInt {
  11. case lightContent
  12. case darkContent
  13. }
  14. public enum status {
  15. case status1
  16. case status2
  17. case status3
  18. }
  19. required public init?(coder aDecoder: NSCoder) {
  20. fatalError("init(coder:) has not been implemented")
  21. }
  22. private let _imageView = {() -> UIImageView in
  23. let imageView = UIImageView()
  24. imageView.contentMode = .scaleAspectFit
  25. return imageView
  26. }()
  27. override public init(frame: CGRect) {
  28. super.init(frame: frame)
  29. addSubview(_imageView)
  30. }
  31. override open func layoutSubviews() {
  32. super.layoutSubviews()
  33. _imageView.frame = bounds
  34. }
  35. open var status = button.status.status1 {
  36. didSet {
  37. _didSet(status: self.status, style: self.style)
  38. }
  39. }
  40. open var style = button.style.darkContent {
  41. didSet {
  42. _didSet(status: self.status, style: self.style)
  43. }
  44. }
  45. private func _didSet(status: button.status, style: button.style) {
  46. _imageView.image = image(of: status, of: style) ?? image(of: .status1, of: style)
  47. }
  48. private var _lightContentStatusImageArray = [button.status: UIImage]()
  49. private var _darkContentStatusImageArray = [button.status: UIImage]()
  50. open func set(image: UIImage, for status: button.status, of style: button.style) {
  51. switch style {
  52. case .lightContent:
  53. _lightContentStatusImageArray[status] = image
  54. break
  55. case .darkContent:
  56. _darkContentStatusImageArray[status] = image
  57. break
  58. }
  59. if self.status == status, self.style == style {
  60. _imageView.image = image
  61. }
  62. }
  63. open func image(of status: button.status, of style: button.style) -> UIImage? {
  64. let array: [button.status: UIImage]
  65. switch style {
  66. case .lightContent:
  67. array = _lightContentStatusImageArray
  68. break
  69. case .darkContent:
  70. array = _darkContentStatusImageArray
  71. break
  72. }
  73. return array[status]
  74. }
  75. }
  76. }
  77. open class KSMediaPickerCameraToolBar: UIView {
  78. public enum toolBarType: UInt {
  79. case photos
  80. case noFlashlightPhotos
  81. case videos
  82. }
  83. required public init?(coder aDecoder: NSCoder) {
  84. fatalError("init(coder:) has not been implemented")
  85. }
  86. public let closeButton = {() -> KSMediaPickerCameraToolBar.button in
  87. let closeButton = KSMediaPickerCameraToolBar.button()
  88. closeButton.set(image: UIImage(named: "icon_mediaPicker_camera_close")!, for: .status1, of: .lightContent)
  89. closeButton.set(image: UIImage(named: "icon_mediaPicker_camera_close_b")!, for: .status1, of: .darkContent)
  90. return closeButton
  91. }()
  92. public let priviewSizeButton = {() -> KSMediaPickerCameraToolBar.button in
  93. let priviewSizeButton = KSMediaPickerCameraToolBar.button()
  94. priviewSizeButton.set(image: UIImage(named: "icon_mediaPicker_camera_square")!, for: .status1, of: .lightContent)
  95. priviewSizeButton.set(image: UIImage(named: "icon_mediaPicker_camera_square_b")!, for: .status1, of: .darkContent)
  96. priviewSizeButton.set(image: UIImage(named: "icon_mediaPicker_camera_rectangle")!, for: .status2, of: .lightContent)
  97. priviewSizeButton.set(image: UIImage(named: "icon_mediaPicker_camera_rectangle_16_9")!, for: .status3, of: .lightContent)
  98. return priviewSizeButton
  99. }()
  100. public let flashlightButton = {() -> KSMediaPickerCameraToolBar.button in
  101. let flashlightButton = KSMediaPickerCameraToolBar.button()
  102. flashlightButton.set(image: UIImage(named: "icon_mediaPicker_camera_flashlight_auto")!, for: .status1, of: .lightContent)
  103. flashlightButton.set(image: UIImage(named: "icon_mediaPicker_camera_flashlight_auto_b")!, for: .status1, of: .darkContent)
  104. flashlightButton.set(image: UIImage(named: "icon_mediaPicker_camera_flashlight_on")!, for: .status2, of: .lightContent)
  105. flashlightButton.set(image: UIImage(named: "icon_mediaPicker_camera_flashlight_on_b")!, for: .status2, of: .darkContent)
  106. flashlightButton.set(image: UIImage(named: "icon_mediaPicker_camera_flashlight_off")!, for: .status3, of: .lightContent)
  107. flashlightButton.set(image: UIImage(named: "icon_mediaPicker_camera_flashlight_off_b")!, for: .status3, of: .darkContent)
  108. return flashlightButton
  109. }()
  110. public let cameraOrientation = {() -> KSMediaPickerCameraToolBar.button in
  111. let cameraOrientation = KSMediaPickerCameraToolBar.button()
  112. cameraOrientation.set(image: UIImage(named: "icon_mediaPicker_camera_switch")!, for: .status1, of: .lightContent)
  113. cameraOrientation.set(image: UIImage(named: "icon_mediaPicker_camera_switch_b")!, for: .status1, of: .darkContent)
  114. return cameraOrientation
  115. }()
  116. open var didChangedStyleCallback: ((KSMediaPickerCameraToolBar.button.style) -> Void)?
  117. open var style: KSMediaPickerCameraToolBar.button.style {
  118. didSet {
  119. if oldValue != style {
  120. _didSet(style: style)
  121. if didChangedStyleCallback != nil {
  122. didChangedStyleCallback!(style)
  123. }
  124. }
  125. }
  126. }
  127. open var type = KSMediaPickerCameraToolBar.toolBarType.photos {
  128. didSet {
  129. let flashlightIsHidden = type != .photos
  130. var isNeedAnimation = false
  131. if flashlightIsHidden != flashlightButton.isHidden {
  132. flashlightButton.isHidden = flashlightIsHidden
  133. isNeedAnimation = true
  134. }
  135. let priviewSizeIsHidden = type == .videos
  136. if priviewSizeIsHidden != priviewSizeButton.isHidden {
  137. priviewSizeButton.isHidden = priviewSizeIsHidden
  138. isNeedAnimation = true
  139. }
  140. if isNeedAnimation {
  141. UIView.animate(withDuration: 0.2) {[weak self] in
  142. self?.layoutSubviews()
  143. }
  144. }
  145. }
  146. }
  147. public init(frame: CGRect, style: KSMediaPickerCameraToolBar.button.style) {
  148. self.style = style
  149. super.init(frame: frame)
  150. _didSet(style: style)
  151. backgroundColor = .clear
  152. addSubview(closeButton)
  153. addSubview(priviewSizeButton)
  154. addSubview(flashlightButton)
  155. addSubview(cameraOrientation)
  156. }
  157. public convenience init(style: KSMediaPickerCameraToolBar.button.style) {
  158. self.init(frame: .zero, style: style)
  159. }
  160. override open func layoutSubviews() {
  161. super.layoutSubviews()
  162. let windowSize = bounds.size
  163. let windowWidth = windowSize.width
  164. let windowHeight = windowSize.height
  165. let floatZore = CGFloat(0.0)
  166. var buttons = [closeButton, cameraOrientation]
  167. switch type {
  168. case .photos:
  169. buttons.insert(priviewSizeButton, at: 1)
  170. buttons.insert(flashlightButton, at: 2)
  171. break
  172. case .noFlashlightPhotos:
  173. buttons.insert(priviewSizeButton, at: 1)
  174. break
  175. case .videos:
  176. break
  177. }
  178. let count = CGFloat(buttons.count)
  179. var viewX = floatZore
  180. let viewY = floatZore
  181. let viewW = CGFloat(70.0)
  182. let viewH = windowHeight
  183. let margin = (windowWidth-(viewW*count))/(count-1)
  184. for (i, button) in buttons.enumerated() {
  185. viewX = (viewW+margin)*CGFloat(i)
  186. button.frame = CGRect(x: viewX, y: viewY, width: viewW, height: viewH)
  187. }
  188. }
  189. private func _didSet(style: KSMediaPickerCameraToolBar.button.style) {
  190. closeButton.style = style
  191. priviewSizeButton.style = style
  192. priviewSizeButton.style = style
  193. flashlightButton.style = style
  194. flashlightButton.style = style
  195. cameraOrientation.style = style
  196. cameraOrientation.style = style
  197. }
  198. }