LBXScanLineAnimation.swift 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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 = CGRect.zero
  12. func startAnimatingWithRect(animationRect: CGRect, parentView: UIView, image: UIImage?)
  13. {
  14. self.image = image
  15. self.animationRect = animationRect
  16. parentView.addSubview(self)
  17. self.isHidden = false;
  18. isAnimationing = true;
  19. if image != nil
  20. {
  21. stepAnimation()
  22. }
  23. }
  24. @objc func stepAnimation()
  25. {
  26. if (!isAnimationing) {
  27. return;
  28. }
  29. var frame:CGRect = animationRect;
  30. let hImg = self.image!.size.height * animationRect.size.width / self.image!.size.width;
  31. frame.origin.y -= hImg;
  32. frame.size.height = hImg;
  33. self.frame = frame;
  34. self.alpha = 0.0;
  35. UIView.animate(withDuration: 1.4, animations: { () -> Void in
  36. self.alpha = 1.0;
  37. var frame = self.animationRect;
  38. let hImg = self.image!.size.height * self.animationRect.size.width / self.image!.size.width;
  39. frame.origin.y += (frame.size.height - hImg);
  40. frame.size.height = hImg;
  41. self.frame = frame;
  42. }, completion:{ (value: Bool) -> Void in
  43. self.perform(#selector(LBXScanLineAnimation.stepAnimation), with: nil, afterDelay: 0.3)
  44. })
  45. }
  46. func stopStepAnimating()
  47. {
  48. self.isHidden = true;
  49. isAnimationing = false;
  50. }
  51. static public func instance()->LBXScanLineAnimation
  52. {
  53. return LBXScanLineAnimation()
  54. }
  55. deinit
  56. {
  57. // print("LBXScanLineAnimation deinit")
  58. stopStepAnimating()
  59. }
  60. }