LBXScanLineAnimation.swift 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. //
  2. // LBXScanLineAnimation.swift
  3. // swiftScan
  4. //
  5. // Created by lbxia on 15/12/9.
  6. // Copyright © 2015年 xialibing. All rights reserved.
  7. //
  8. import UIKit
  9. class LBXScanLineAnimation: UIImageView {
  10. var isAnimationing = false
  11. var animationRect = CGRect.zero
  12. func startAnimatingWithRect(animationRect: CGRect, parentView: UIView, image: UIImage?) {
  13. self.image = image
  14. self.animationRect = animationRect
  15. parentView.addSubview(self)
  16. isHidden = false
  17. isAnimationing = true
  18. if image != nil {
  19. stepAnimation()
  20. }
  21. }
  22. @objc func stepAnimation() {
  23. guard isAnimationing else {
  24. return
  25. }
  26. var frame = animationRect
  27. let hImg = image!.size.height * animationRect.size.width / image!.size.width
  28. frame.origin.y -= hImg
  29. frame.size.height = hImg
  30. self.frame = frame
  31. alpha = 0.0
  32. UIView.animate(withDuration: 1.4, animations: {
  33. self.alpha = 1.0
  34. var frame = self.animationRect
  35. let hImg = self.image!.size.height * self.animationRect.size.width / self.image!.size.width
  36. frame.origin.y += (frame.size.height - hImg)
  37. frame.size.height = hImg
  38. self.frame = frame
  39. }, completion: { _ in
  40. self.perform(#selector(LBXScanLineAnimation.stepAnimation), with: nil, afterDelay: 0.3)
  41. })
  42. }
  43. func stopStepAnimating() {
  44. isHidden = true
  45. isAnimationing = false
  46. }
  47. public static func instance() -> LBXScanLineAnimation {
  48. return LBXScanLineAnimation()
  49. }
  50. deinit {
  51. stopStepAnimating()
  52. }
  53. }