LBXScanNetAnimation.swift 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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.zero
  12. public static func instance() -> LBXScanNetAnimation {
  13. return LBXScanNetAnimation()
  14. }
  15. func startAnimatingWithRect(animationRect: CGRect, parentView: UIView, image: UIImage?) {
  16. self.image = image
  17. self.animationRect = animationRect
  18. parentView.addSubview(self)
  19. isHidden = false
  20. isAnimationing = true
  21. if image != nil {
  22. stepAnimation()
  23. }
  24. }
  25. @objc func stepAnimation() {
  26. guard isAnimationing else {
  27. return
  28. }
  29. var frame = animationRect
  30. let hImg = image!.size.height * animationRect.size.width / image!.size.width
  31. frame.origin.y -= hImg
  32. frame.size.height = hImg
  33. self.frame = frame
  34. alpha = 0.0
  35. UIView.animate(withDuration: 1.2, animations: {
  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: { _ in
  43. self.perform(#selector(LBXScanNetAnimation.stepAnimation), with: nil, afterDelay: 0.3)
  44. })
  45. }
  46. func stopStepAnimating() {
  47. isHidden = true
  48. isAnimationing = false
  49. }
  50. }