LBXScanNetAnimation.swift 1.9 KB

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