O2UISignatureView.swift 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. //
  2. // O2UISignatureView.swift
  3. // O2Platform
  4. //
  5. // Created by FancyLou on 2018/8/28.
  6. // Copyright © 2018 zoneland. All rights reserved.
  7. //
  8. import UIKit
  9. import CocoaLumberjack
  10. class O2UISignatureView: UIView {
  11. //MARK: - arguments
  12. var control: Int!
  13. var beizerPath: UIBezierPath!
  14. var points:[CGPoint] = [CGPoint](repeating: CGPoint(), count: 5)
  15. required init?(coder aDecoder: NSCoder) {
  16. super.init(coder: aDecoder)
  17. self.initArguments()
  18. }
  19. override init(frame: CGRect) {
  20. super.init(frame: frame)
  21. self.initArguments()
  22. }
  23. private func initArguments() {
  24. self.backgroundColor = UIColor.white //背景颜色
  25. self.isMultipleTouchEnabled = false
  26. self.beizerPath = UIBezierPath()
  27. self.beizerPath.lineWidth = 2
  28. }
  29. override func draw(_ rect: CGRect) {
  30. let color = UIColor.black
  31. color.setStroke()
  32. self.beizerPath.stroke()
  33. }
  34. override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
  35. self.control = 0
  36. if let point = touches.first?.location(in: self) {
  37. points[0] = point
  38. let startPoint = points[0]
  39. let endPoint = CGPoint(x: startPoint.x + 1.5, y: startPoint.y + 2)
  40. self.beizerPath.move(to: startPoint)
  41. self.beizerPath.addLine(to: endPoint)
  42. }
  43. }
  44. override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
  45. if let point = touches.first?.location(in: self) {
  46. self.control = self.control + 1
  47. points[self.control] = point
  48. if self.control == 4 {
  49. points[3] = CGPoint(x: (points[2].x + points[4].x)/2.0, y: (points[2].y + points[4].y)/2.0)
  50. //设置画笔起始点
  51. self.beizerPath.move(to: points[0])
  52. //endPoint终点 controlPoint1、controlPoint2控制点
  53. self.beizerPath.addCurve(to: points[3], controlPoint1: points[1], controlPoint2: points[2])
  54. //setNeedsDisplay会自动调用drawRect方法,这样可以拿到UIGraphicsGetCurrentContext,就可以画画了
  55. self.setNeedsDisplay()
  56. points[0] = points[3]
  57. points[1] = points[4]
  58. self.control = 1
  59. }
  60. }
  61. }
  62. // 设置笔粗细
  63. func setLineSize(lineSize: CGFloat) {
  64. DDLogDebug("line size : \(lineSize)")
  65. self.beizerPath.lineWidth = lineSize
  66. }
  67. // 清除内容
  68. func clearSignature() {
  69. self.beizerPath.removeAllPoints()
  70. self.setNeedsDisplay()
  71. DDLogDebug("is empty: \(self.beizerPath.isEmpty)")
  72. }
  73. // 生成签名图片
  74. func getSignatureImage() -> UIImage? {
  75. if self.beizerPath.isEmpty {
  76. return nil
  77. }
  78. UIGraphicsBeginImageContextWithOptions(self.bounds.size, false, UIScreen.main.scale)
  79. self.layer.render(in: UIGraphicsGetCurrentContext()!)
  80. let image = UIGraphicsGetImageFromCurrentImageContext()
  81. UIGraphicsEndImageContext()
  82. if image != nil {
  83. let documentsURL = FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask)[0]
  84. let fileURL = documentsURL.appendingPathComponent("signature.png")
  85. do {
  86. try image?.pngData()?.write(to: fileURL, options: .atomic)
  87. }catch {
  88. DDLogError("写入本地异常,\(error.localizedDescription)")
  89. }
  90. return image!
  91. }else {
  92. return nil
  93. }
  94. }
  95. }