暂无描述

QRCodeMaskView.swift 7.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. //
  2. // QRCodeMaskView.swift
  3. // PaiAi-Guide
  4. //
  5. // Created by FFIB on 2017/1/20.
  6. // Copyright © 2017年 FFIB. All rights reserved.
  7. //
  8. import UIKit
  9. let qrcodeAnimation = "com.QR.gradient"
  10. class QRCodeMaskView: UIView {
  11. var configuration = QRCodeConfiguration() {
  12. didSet {
  13. layoutIfNeeded()
  14. }
  15. }
  16. fileprivate var cornerLineWidth: CGFloat {
  17. return configuration.cornerLineWidth
  18. }
  19. fileprivate var cornerLineLength: CGFloat {
  20. return configuration.cornerLineLength
  21. }
  22. fileprivate var cornerLineColor: UIColor {
  23. return configuration.cornerLineColor
  24. }
  25. fileprivate var gradientColor: UIColor {
  26. return configuration.gradientColor
  27. }
  28. fileprivate var cornerLineSpace: CGFloat = 1.0
  29. fileprivate var cornerLineDistance: CGFloat = 1.0
  30. fileprivate let animation = { () -> CABasicAnimation in
  31. let animation = CABasicAnimation(keyPath: "position.y")
  32. animation.repeatCount = 100000
  33. animation.isRemovedOnCompletion = false
  34. animation.duration = 2
  35. animation.fillMode = CAMediaTimingFillMode.forwards
  36. return animation
  37. }()
  38. fileprivate let gradientLayer = CAGradientLayer()
  39. func stopAnimation() {
  40. let pauseTime = gradientLayer.convertTime(CACurrentMediaTime(), to: nil)
  41. gradientLayer.speed = 0.0
  42. gradientLayer.timeOffset = pauseTime
  43. }
  44. func startAnimation() {
  45. let pauseTime = gradientLayer.timeOffset
  46. gradientLayer.speed = 1.0
  47. gradientLayer.timeOffset = 0.0
  48. gradientLayer.beginTime = 0.0
  49. let timeSincePause = gradientLayer.convertTime(CACurrentMediaTime(), to: nil) - pauseTime
  50. gradientLayer.beginTime = timeSincePause
  51. }
  52. override init(frame: CGRect) {
  53. super.init(frame: frame)
  54. }
  55. required init?(coder aDecoder: NSCoder) {
  56. super.init(coder: aDecoder)
  57. }
  58. func setScanAnimation(clearRect: CGRect) {
  59. let view = UIView(frame: clearRect)
  60. view.clipsToBounds = true
  61. addSubview(view)
  62. gradientLayer.frame = CGRect(x: 0,
  63. y: -48,
  64. width: clearRect.width,
  65. height: 48)
  66. gradientLayer.colors = [UIColor(red: 150 / 255.0,
  67. green: 150 / 255.0,
  68. blue: 150 / 255.0,
  69. alpha: 0.0).cgColor,
  70. gradientColor.cgColor]
  71. view.layer.addSublayer(gradientLayer)
  72. animation.toValue = clearRect.height
  73. gradientLayer.add(animation, forKey: qrcodeAnimation)
  74. stopAnimation()
  75. }
  76. override func draw(_ rect: CGRect) { //swiftlint:disable:this function_body_length
  77. let screenSize = CGSize(width: rect.width / 1.5,
  78. height: rect.width / 1.5)
  79. let clearRect = CGRect(x: (bounds.width - screenSize.width) / 2,
  80. y: (bounds.height - screenSize.height) / 2,
  81. width: screenSize.width,
  82. height: screenSize.height)
  83. setScanAnimation(clearRect: clearRect)
  84. //set masking
  85. let context = UIGraphicsGetCurrentContext()
  86. context?.setFillColor(UIColor(red: 40 / 255.0,
  87. green: 40 / 255.0,
  88. blue: 40 / 255.0,
  89. alpha: 0.2).cgColor)
  90. context?.fill(frame)
  91. context?.clear(clearRect)
  92. context?.setLineWidth(cornerLineWidth)
  93. context?.setStrokeColor(cornerLineColor.cgColor)
  94. //the upper left corner
  95. let pointUpperLeftA = [CGPoint(x: clearRect.origin.x - cornerLineDistance,
  96. y: clearRect.origin.y - cornerLineDistance),
  97. CGPoint(x: clearRect.origin.x - cornerLineDistance,
  98. y: clearRect.origin.y + cornerLineLength - cornerLineDistance)]
  99. let pointUpperLeftB = [CGPoint(x: clearRect.origin.x - cornerLineDistance - cornerLineSpace,
  100. y: clearRect.origin.y - cornerLineDistance),
  101. CGPoint(x: clearRect.origin.x + cornerLineLength - cornerLineDistance + cornerLineSpace,
  102. y: clearRect.origin.y - cornerLineDistance)]
  103. context?.addLines(between: pointUpperLeftA)
  104. context?.addLines(between: pointUpperLeftB)
  105. //the lower left corner
  106. let pointLowerLeftA = [CGPoint(x: clearRect.origin.x - cornerLineDistance,
  107. y: clearRect.origin.y + screenSize.height + cornerLineDistance),
  108. CGPoint(x: clearRect.origin.x - cornerLineDistance,
  109. y: clearRect.origin.y + screenSize.height - cornerLineLength + cornerLineDistance)] //swiftlint:disable:this line_length
  110. let pointLowerLeftB = [CGPoint(x: clearRect.origin.x - cornerLineDistance - cornerLineSpace,
  111. y: clearRect.origin.y + screenSize.height + cornerLineDistance),
  112. CGPoint(x: clearRect.origin.x + cornerLineLength - cornerLineDistance + cornerLineSpace,
  113. y: clearRect.origin.y + screenSize.height + cornerLineDistance)]
  114. context?.addLines(between: pointLowerLeftA)
  115. context?.addLines(between: pointLowerLeftB)
  116. //the upper right corner
  117. let pointUpperRightA = [CGPoint(x: clearRect.origin.x + screenSize.width + cornerLineDistance + cornerLineSpace,
  118. y: clearRect.origin.y - cornerLineDistance),
  119. CGPoint(x: clearRect.origin.x + screenSize.width - cornerLineLength + cornerLineDistance - cornerLineSpace, y: clearRect.origin.y - cornerLineDistance)] //swiftlint:disable:this line_length
  120. let pointUpperRightB = [CGPoint(x: clearRect.origin.x + screenSize.width + cornerLineDistance,
  121. y: clearRect.origin.y - cornerLineDistance),
  122. CGPoint(x: clearRect.origin.x + screenSize.width + cornerLineDistance,
  123. y: clearRect.origin.y + cornerLineLength - cornerLineDistance)]
  124. context?.addLines(between: pointUpperRightA)
  125. context?.addLines(between: pointUpperRightB)
  126. //the lower right corrner
  127. let pointLowerRightA = [CGPoint(x: clearRect.origin.x + screenSize.width + cornerLineDistance,
  128. y: clearRect.origin.y + screenSize.height + cornerLineDistance),
  129. CGPoint(x: clearRect.origin.x + screenSize.width + cornerLineDistance,
  130. y: clearRect.origin.y + screenSize.height - cornerLineLength + cornerLineDistance)] //swiftlint:disable:this line_length
  131. let pointLowerRightB = [CGPoint(x: clearRect.origin.x + screenSize.width + cornerLineDistance + cornerLineSpace,
  132. y: clearRect.origin.y + screenSize.height + cornerLineDistance),
  133. CGPoint(x: clearRect.origin.x + screenSize.width - cornerLineLength + cornerLineDistance - cornerLineDistance, y: clearRect.origin.y + screenSize.height + cornerLineDistance)] //swiftlint:disable:this line_length
  134. context?.addLines(between: pointLowerRightA)
  135. context?.addLines(between: pointLowerRightB)
  136. context?.strokePath()
  137. }
  138. }