O2LoadingView.swift 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. //
  2. // O2LoadingView.swift
  3. // O2Platform
  4. //
  5. // Created by 程剑 on 2017/7/6.
  6. // Copyright © 2017年 zoneland. All rights reserved.
  7. //
  8. import UIKit
  9. class O2LoadingView: UIView {
  10. fileprivate var displayLink: CADisplayLink?
  11. fileprivate var shapeLayer: CAShapeLayer?
  12. fileprivate var treeImageView: UIImageView?
  13. fileprivate var viewRect: CGRect!
  14. var offsetY: CGFloat = 15
  15. var moveSpeed: CGFloat = 0.5
  16. var statusF: CGFloat!
  17. override init(frame: CGRect) {
  18. super.init(frame: frame)
  19. self.viewRect = frame
  20. self.backgroundColor = UIColor(red: 251/255, green: 71/255, blue: 71/255, alpha: 1)
  21. configUI(frame)
  22. }
  23. required init?(coder aDecoder: NSCoder) {
  24. fatalError("init(coder:) has not been implemented")
  25. }
  26. func configUI(_ rect: CGRect) {
  27. self.treeImageView = UIImageView(image: UIImage(named: "oloading0"))
  28. self.treeImageView?.frame = CGRect(x: 0, y: 0, width: 100, height: 100)
  29. self.addSubview(self.treeImageView!)
  30. self.shapeLayer = CAShapeLayer()
  31. let shapeColor = UIColor(red: 251/255, green: 71/255, blue: 71/255, alpha: 0.5)
  32. self.shapeLayer?.fillColor = shapeColor.cgColor
  33. self.layer.addSublayer(self.shapeLayer!)
  34. self.displayLink = CADisplayLink(target: self, selector: #selector(self.getCurrentShape))
  35. self.displayLink?.add(to: .current, forMode: RunLoop.Mode.common)
  36. }
  37. @objc func getCurrentShape() {
  38. if (self.offsetY >= self.viewRect.size.height - 15) {
  39. self.statusF = 1;
  40. }else if (self.offsetY <= 15){
  41. self.statusF = 0;
  42. }
  43. if (self.statusF == 1) {
  44. self.offsetY -= self.moveSpeed;
  45. }else{
  46. self.offsetY += self.moveSpeed;
  47. }
  48. self.setCurrentStatusShapePath()
  49. }
  50. func setCurrentStatusShapePath() {
  51. let path = UIBezierPath()
  52. path.move(to: CGPoint(x: 0, y: self.offsetY))
  53. for i in 0..<Int(self.viewRect.size.width) {
  54. path.addLine(to: CGPoint(x: CGFloat(i), y: self.offsetY))
  55. }
  56. path.addLine(to: CGPoint(x: self.viewRect.size.width, y: 0))
  57. path.addLine(to: CGPoint(x: 0, y: 0))
  58. path.close()
  59. self.shapeLayer?.path = path.cgPath
  60. }
  61. func finishWave() {
  62. if let displayLink = self.displayLink {
  63. displayLink.invalidate()
  64. self.displayLink = nil
  65. }
  66. }
  67. }