财神随手记账

HPSlideSegmentManage.m 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. //
  2. // HPSlideManage.m
  3. // HPSlideSegmentView
  4. //
  5. // Created by 何鹏 on 17/6/14.
  6. // Copyright © 2017年 何鹏. All rights reserved.
  7. //
  8. #import "HPSlideSegmentManage.h"
  9. #import "UIView+HPSlideSegmentRect.h"
  10. @interface HPSlideSegmentManage ()
  11. @property(nonatomic,assign) CGFloat lastPoint;
  12. @end
  13. @implementation HPSlideSegmentManage
  14. static HPSlideSegmentManage *_instance;
  15. + (instancetype)allocWithZone:(struct _NSZone *)zone
  16. {
  17. static dispatch_once_t onceToken;
  18. dispatch_once(&onceToken, ^{
  19. _instance = [super allocWithZone:zone];
  20. });
  21. return _instance;
  22. }
  23. + (instancetype)sharedSlideManage
  24. {
  25. static dispatch_once_t onceToken;
  26. dispatch_once(&onceToken, ^{
  27. _instance = [[self alloc] init];
  28. });
  29. return _instance;
  30. }
  31. - (id)copyWithZone:(NSZone *)zone
  32. {
  33. return _instance;
  34. }
  35. -(void)slideUpSegmentWithMainScrollerView:(UIScrollView *)mainScrollerView
  36. showScrollerView:(UIScrollView *)centreScrollerView
  37. upHeight:(CGFloat)topHeight
  38. delegate:(id<HPSlideSegmentManageDelegate>) delegate;
  39. {
  40. mainScrollerView.bounces=NO;
  41. CGFloat centreY=centreScrollerView.contentOffset.y;
  42. CGFloat mainY=mainScrollerView.contentOffset.y;
  43. CGFloat bottomHeight=mainScrollerView.contentSize.height-mainScrollerView.bounds.size.height;
  44. if (centreScrollerView.contentOffset.y==0 || delegate==nil) {
  45. if ([delegate respondsToSelector:@selector(hp_slideUpSegmentWithMain:)]) {
  46. [delegate hp_slideUpSegmentWithMain:YES];
  47. }
  48. return;
  49. }
  50. if (mainY<bottomHeight) {
  51. mainScrollerView.bounces=YES;
  52. centreScrollerView.contentOffset=CGPointMake(0, 0);
  53. if ([delegate respondsToSelector:@selector(hp_slideUpSegmentWithMain:)]) {
  54. [delegate hp_slideUpSegmentWithMain:YES];
  55. }
  56. }
  57. else
  58. {
  59. if ([delegate respondsToSelector:@selector(hp_slideUpSegmentWithMain:)]) {
  60. [delegate hp_slideUpSegmentWithMain:NO];
  61. }
  62. if (centreY-_lastPoint>20) {
  63. //up
  64. _lastPoint=centreY;
  65. if(centreY>0)
  66. {
  67. mainScrollerView.contentOffset=CGPointMake(0, bottomHeight);
  68. }
  69. else
  70. {
  71. if ([delegate respondsToSelector:@selector(hp_slideUpSegmentWithMain:)]) {
  72. [delegate hp_slideUpSegmentWithMain:YES];
  73. }
  74. }
  75. }
  76. else if (_lastPoint-centreY>20)
  77. {
  78. //down
  79. _lastPoint=centreY;
  80. }
  81. else
  82. {
  83. if (centreY<=0) {
  84. centreScrollerView.contentOffset=CGPointMake(0, 0);
  85. if ([delegate respondsToSelector:@selector(hp_slideUpSegmentWithMain:)]) {
  86. [delegate hp_slideUpSegmentWithMain:YES];
  87. }
  88. }
  89. }
  90. }
  91. }
  92. +(void)slidetLogicSrollerView:(UIScrollView *)mainScrollerView
  93. showScrollerView:(UIScrollView *)centreScrollerView
  94. upHeight:(CGFloat)topHeight
  95. slideUpSegmentBlock:(SlideUpSegmentBlock)upBlock
  96. {
  97. CGFloat offsetHeight=mainScrollerView.contentOffset.y-topHeight;
  98. CGPoint topPoint=CGPointMake(0, 0);
  99. if (offsetHeight<0) {
  100. topPoint=CGPointMake(0, 0);
  101. }
  102. else if (offsetHeight==0)
  103. {
  104. topPoint=CGPointMake(0, 0);
  105. }
  106. else if (offsetHeight>0)
  107. {
  108. CGFloat scrollSizeHeight=mainScrollerView.contentSize.height-mainScrollerView.contentOffset.y;
  109. if (scrollSizeHeight<=mainScrollerView.bounds.size.height)
  110. {
  111. topPoint=CGPointMake(0, offsetHeight);
  112. }
  113. else
  114. {
  115. topPoint=CGPointMake(0, offsetHeight);
  116. }
  117. }
  118. if (upBlock!=nil) {
  119. upBlock(topPoint);
  120. }
  121. }
  122. @end