No Description

QRCodeMaskView.swift 7.5KB

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