/TLStoryCameraFramework/TLStoryCameraFramework/Class/Views/TLStoryEditContainerView.swift

https://github.com/timelessg/TLStoryCamera · Swift · 234 lines · 183 code · 44 blank · 7 comment · 8 complexity · 1d22ab29e0357141a89513f927a6e8c5 MD5 · raw file

  1. //
  2. // TLStoryEditContainerView.swift
  3. // TLStoryCamera
  4. //
  5. // Created by GarryGuo on 2017/6/1.
  6. // Copyright © 2017年 GarryGuo. All rights reserved.
  7. //
  8. import UIKit
  9. protocol TLStoryEditContainerViewDelegate: NSObjectProtocol{
  10. func storyEditContainerEndDrawing()
  11. func storyEditContainerSticker(editing:Bool)
  12. func storyEditContainerTextStickerBeEditing(sticker:TLStoryTextSticker)
  13. func storyEditContainerSwipeUp()
  14. func storyEditContainerTap()
  15. func storyEditSwpieFilter(direction:UISwipeGestureRecognizerDirection)
  16. }
  17. class TLStoryEditContainerView: UIView {
  18. public weak var delegate:TLStoryEditContainerViewDelegate?
  19. fileprivate var stickersView:TLStoryStickersView?
  20. fileprivate var doodleView:TLStoryDoodleView?
  21. fileprivate var colorPicker:TLStoryColorPickerView?
  22. fileprivate var confrimBtn:TLButton = {
  23. let btn = TLButton.init(type: UIButtonType.custom)
  24. btn.setTitle("确定", for: .normal)
  25. btn.showsTouchWhenHighlighted = true
  26. btn.titleLabel?.font = UIFont.boldSystemFont(ofSize: 15)
  27. return btn
  28. }()
  29. fileprivate var undoBtn:TLButton = {
  30. let btn = TLButton.init(type: UIButtonType.custom)
  31. btn.showsTouchWhenHighlighted = true
  32. btn.setImage(UIImage.tl_imageWithNamed(named: "story_publish_icon_drawing_cancel"), for: .normal)
  33. return btn
  34. }()
  35. fileprivate var isDrawing:Bool = false
  36. fileprivate var tapGesture:UITapGestureRecognizer?
  37. fileprivate var swipeUpGesture:UISwipeGestureRecognizer?
  38. fileprivate var swipeLeftGesture:UISwipeGestureRecognizer?
  39. fileprivate var swipeRightGesture:UISwipeGestureRecognizer?
  40. override init(frame: CGRect) {
  41. super.init(frame: frame)
  42. doodleView = TLStoryDoodleView.init(frame: self.bounds)
  43. doodleView?.delegate = self
  44. self.addSubview(doodleView!)
  45. stickersView = TLStoryStickersView.init(frame: self.bounds)
  46. stickersView?.delegate = self
  47. self.addSubview(stickersView!)
  48. confrimBtn.addTarget(self, action: #selector(confrimAction), for: .touchUpInside)
  49. self.addSubview(confrimBtn)
  50. confrimBtn.isHidden = true
  51. confrimBtn.bounds = CGRect.init(x: 0, y: 0, width: 55, height: 55)
  52. confrimBtn.center = CGPoint.init(x: self.width - confrimBtn.width / 2, y:confrimBtn.height / 2)
  53. undoBtn.addTarget(self, action: #selector(undoAction), for: .touchUpInside)
  54. self.addSubview(undoBtn)
  55. undoBtn.isHidden = true
  56. undoBtn.bounds = CGRect.init(x: 0, y: 0, width: 55, height: 55)
  57. undoBtn.center = CGPoint.init(x: self.undoBtn.width / 2, y: confrimBtn.centerY)
  58. colorPicker = TLStoryColorPickerView.init(frame: CGRect.init(x: 0, y: self.height - 60, width: self.width, height: 60))
  59. colorPicker?.delegate = self
  60. colorPicker!.isHidden = true
  61. self.addSubview(colorPicker!)
  62. tapGesture = UITapGestureRecognizer.init(target: self, action: #selector(tapAction))
  63. tapGesture?.delegate = self
  64. self.addGestureRecognizer(tapGesture!)
  65. swipeUpGesture = UISwipeGestureRecognizer.init(target: self, action: #selector(swipeUpAction))
  66. swipeUpGesture?.direction = .up
  67. swipeUpGesture?.delegate = self
  68. self.addGestureRecognizer(swipeUpGesture!)
  69. swipeLeftGesture = UISwipeGestureRecognizer.init(target: self, action: #selector(switchFilter));
  70. swipeLeftGesture!.direction = .left
  71. swipeLeftGesture?.delegate = self
  72. self.addGestureRecognizer(swipeLeftGesture!)
  73. swipeRightGesture = UISwipeGestureRecognizer.init(target: self, action: #selector(switchFilter));
  74. swipeRightGesture!.direction = .right
  75. swipeRightGesture?.delegate = self
  76. self.addGestureRecognizer(swipeRightGesture!)
  77. }
  78. @objc fileprivate func tapAction() {
  79. self.delegate?.storyEditContainerTap()
  80. }
  81. @objc fileprivate func swipeUpAction() {
  82. self.delegate?.storyEditContainerSwipeUp()
  83. }
  84. @objc fileprivate func switchFilter(sender:UISwipeGestureRecognizer) {
  85. self.delegate?.storyEditSwpieFilter(direction: sender.direction)
  86. }
  87. @objc fileprivate func undoAction() {
  88. doodleView?.undo()
  89. }
  90. @objc fileprivate func confrimAction() {
  91. self.doodleIcons(true)
  92. isDrawing = false
  93. self.delegate?.storyEditContainerEndDrawing()
  94. self.doodleView?.isUserInteractionEnabled = false
  95. stickersView!.isUserInteractionEnabled = true
  96. }
  97. fileprivate func doodleIcons(_ hidden:Bool) {
  98. if hidden {
  99. UIView.animate(withDuration: 0.3, animations: {
  100. self.confrimBtn.alpha = 0
  101. self.undoBtn.alpha = 0
  102. self.colorPicker?.alpha = 0
  103. }, completion: { (x) in
  104. if x {
  105. self.confrimBtn.isHidden = true
  106. self.undoBtn.isHidden = true
  107. self.colorPicker?.isHidden = true
  108. }
  109. })
  110. }else {
  111. self.confrimBtn.isHidden = false
  112. self.undoBtn.isHidden = false
  113. self.colorPicker?.isHidden = false
  114. UIView.animate(withDuration: 0.3, animations: {
  115. self.confrimBtn.alpha = 1
  116. self.undoBtn.alpha = 1
  117. self.colorPicker?.alpha = 1
  118. })
  119. }
  120. }
  121. public func add(textSticker:TLStoryTextSticker) {
  122. self.stickersView?.addSub(textSticker: textSticker)
  123. }
  124. public func add(img:UIImage) {
  125. self.stickersView?.addSub(image: img)
  126. }
  127. public func getScreenshot() -> UIImage {
  128. let doodleImg = self.doodleView!.screenshot()
  129. let stickersImg = self.stickersView!.screenshot()
  130. return doodleImg.imageMontage(img: stickersImg,bgColor: nil, size: UIScreen.main.bounds.size)
  131. }
  132. public func benginDrawing() {
  133. self.doodleIcons(false)
  134. isDrawing = true
  135. stickersView!.isUserInteractionEnabled = false
  136. self.doodleView?.isUserInteractionEnabled = true
  137. }
  138. public func reset() {
  139. self.doodleView?.erase()
  140. self.stickersView?.reset()
  141. self.colorPicker?.reset()
  142. }
  143. override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
  144. guard let v = super.hitTest(point, with: event) else {
  145. return nil
  146. }
  147. if v.isKind(of: TLStoryDoodleView.self) || v.isKind(of: TLStoryStickersView.self) {
  148. if isDrawing {
  149. return self.doodleView
  150. }else {
  151. return self.stickersView
  152. }
  153. }else {
  154. return v
  155. }
  156. }
  157. required init?(coder aDecoder: NSCoder) {
  158. fatalError("init(coder:) has not been implemented")
  159. }
  160. }
  161. extension TLStoryEditContainerView: UIGestureRecognizerDelegate {
  162. override func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
  163. if isDrawing {
  164. return false
  165. }
  166. return true
  167. }
  168. }
  169. extension TLStoryEditContainerView: TLStoryStickersViewDelegate {
  170. internal func storyTextStickersBeEditing(sticker: TLStoryTextSticker) {
  171. self.delegate?.storyEditContainerTextStickerBeEditing(sticker: sticker)
  172. }
  173. internal func storyStickers(editing: Bool) {
  174. self.delegate?.storyEditContainerSticker(editing: editing)
  175. }
  176. }
  177. extension TLStoryEditContainerView: TLStoryDoodleViewDelegate {
  178. internal func storyDoodleView(drawing:Bool) {
  179. self.doodleIcons(drawing)
  180. }
  181. }
  182. extension TLStoryEditContainerView: TLStoryColorPickerViewDelegate {
  183. func storyColorPickerDidChange(color: TLStoryColor) {
  184. doodleView?.lineColor = color.backgroundColor
  185. }
  186. internal func storyColorPickerDidChange(percent: CGFloat) {
  187. let lineWidth = (TLStoryConfiguration.maxDrawLineWeight - TLStoryConfiguration.minDrawLineWeight) * percent + TLStoryConfiguration.minDrawLineWeight
  188. doodleView?.lineWidth = lineWidth
  189. }
  190. }