CurveRefreshHeader.swift 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. //
  2. // CurveRefreshHeader.swift
  3. // PullToRefreshKit
  4. //
  5. // Created by zhongyuan on 2016/12/8.
  6. // Copyright © 2016年 luoyang. All rights reserved.
  7. //
  8. import GTMRefresh
  9. import UIKit
  10. class CurveRefreshHeader: GTMRefreshHeader, SubGTMRefreshHeaderProtocol {
  11. let pullingIndicator = UIImageView()
  12. // let bgColor = UIColor(red: 77.0/255.0, green: 184.0/255.0, blue: 255.0/255.0, alpha: 0.65)
  13. let totalHeight = UIScreen.main.bounds.size.height
  14. let maskLayer = CAShapeLayer()
  15. let spinner = UIActivityIndicatorView(style: .white)
  16. let backgroundLayer = CALayer()
  17. override init(frame: CGRect) {
  18. super.init(frame: frame)
  19. pullingIndicator.image = UIImage(named: "arrow_downWhite")
  20. self.contentView.layer.addSublayer(backgroundLayer)
  21. backgroundLayer.backgroundColor = UIColor(red: 77.0/255.0, green: 184.0/255.0, blue: 255.0/255.0, alpha: 0.65).cgColor
  22. backgroundLayer.mask = maskLayer
  23. self.contentView.addSubview(pullingIndicator)
  24. self.contentView.addSubview(spinner)
  25. }
  26. override func layoutSubviews() {
  27. super.layoutSubviews()
  28. let center = CGPoint(x: self.contentView.bounds.width/2, y: self.contentView.bounds.height - 30)
  29. spinner.center = center
  30. pullingIndicator.frame = CGRect(x: 0, y: 0, width: 25, height: 25)
  31. pullingIndicator.center = center
  32. backgroundLayer.bounds = self.contentView.bounds
  33. backgroundLayer.position = CGPoint(x: self.contentView.bounds.width/2, y: self.contentView.bounds.height/2)
  34. maskLayer.bounds = self.contentView.bounds
  35. maskLayer.anchorPoint = CGPoint(x: 0.0, y: 0.0)
  36. }
  37. required init?(coder aDecoder: NSCoder) {
  38. fatalError("init(coder:) has not been implemented")
  39. }
  40. override func refreshingHoldHeight() -> CGFloat {
  41. return 60
  42. }
  43. /// 即将触发刷新的高度(特殊的控件需要重写该方法,返回不同的数值)
  44. ///
  45. /// - Returns: 触发刷新的高度
  46. open override func willRefresHeight() -> CGFloat {
  47. return 60
  48. }
  49. func toNormalState() {
  50. UIView.animate(withDuration: 0.4, animations: {
  51. self.pullingIndicator.transform = CGAffineTransform.identity
  52. })
  53. }
  54. func toRefreshingState() {
  55. spinner.startAnimating()
  56. let controPoint = CGPoint(x: self.bounds.width/2, y: self.bounds.height - 60.0)
  57. self.maskLayer.path = createLayerWithY(60, controlPoint: controPoint).cgPath
  58. }
  59. func toPullingState() {
  60. UIView.animate(withDuration: 0.4, animations: {
  61. self.pullingIndicator.transform = CGAffineTransform(rotationAngle: CGFloat(-Double.pi+0.000001))
  62. })
  63. }
  64. func changePullingPercent(percent: CGFloat) {
  65. let heightScrolled = 60 * percent
  66. let adjustHeight = heightScrolled < 60 ? heightScrolled : 60
  67. // dPrint(adjustHeight)
  68. let controlPoint = CGPoint(x: self.bounds.width/2, y: self.bounds.height + adjustHeight)
  69. let bezierPath = createLayerWithY(adjustHeight,controlPoint: controlPoint)
  70. self.maskLayer.path = bezierPath.cgPath
  71. }
  72. func willBeginEndRefershing(isSuccess: Bool) {
  73. spinner.stopAnimating()
  74. pullingIndicator.transform = CGAffineTransform.identity
  75. // imageView.image = UIImage(named: "success")
  76. }
  77. func willCompleteEndRefershing() {
  78. pullingIndicator.image = UIImage(named: "arrow_downWhite")
  79. }
  80. func contentHeight()->CGFloat{
  81. return 500
  82. }
  83. // MARK: Private
  84. func createLayerWithY(_ y:CGFloat,controlPoint:CGPoint)->UIBezierPath{
  85. let bezierPath = UIBezierPath()
  86. bezierPath.move(to: CGPoint(x: 0, y: 0))
  87. bezierPath.addLine(to: CGPoint(x: self.bounds.width, y: 0))
  88. bezierPath.addLine(to: CGPoint(x: self.bounds.width, y: self.bounds.height - y))
  89. bezierPath.addQuadCurve(to: CGPoint(x: 0, y: self.bounds.height - y), controlPoint: controlPoint)
  90. bezierPath.addLine(to: CGPoint(x: 0, y: 0))
  91. return bezierPath
  92. }
  93. }