SPSBChooseCityNavigationView.m 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. //
  2. // SPSBChooseCityNavigationView.m
  3. // 我的社保
  4. //
  5. // Created by shanp on 2021/5/20.
  6. //
  7. #import "SPSBChooseCityNavigationView.h"
  8. #import "SPSBUIGeneralHeader.h"
  9. #define spsb_chooseCityNavigationHeight ((jxh_screenHeight() - 180) > 400 ? 400 : (jxh_screenHeight() - 180))
  10. #define spsb_chooseCityNavigationViewLabelTag 3000
  11. #define spsb_chooseCityNavigationLabelTag 10000
  12. @interface SPSBChooseCityNavigationView () {
  13. NSInteger _count;
  14. CGFloat _navigationHeight;
  15. }
  16. @end
  17. @implementation SPSBChooseCityNavigationView
  18. - (instancetype)initWithArray:(NSArray *)array {
  19. self = [super init];
  20. if (!self) return nil;
  21. _count = array.count;
  22. _navigationHeight = 15 * _count;
  23. _navigationHeight = _navigationHeight < (jxh_screenHeight() - 180) ? _navigationHeight : jxh_screenHeight() - 180;
  24. self.backgroundColor = [UIColor whiteColor];
  25. UILabel *tempLabel = nil;
  26. for (NSInteger i = 0; i < _count; i ++) {
  27. UILabel *label = [UILabel convenienceWithFont:spsb_semiboldFont(11) text:[array objectAtIndex:i] textColor:spsb_3296FB_color() textAlignment:NSTextAlignmentCenter];
  28. label.tag = spsb_chooseCityNavigationViewLabelTag + i;
  29. [self addSubview:label];
  30. [label makeConstraints:^(JXHConstraintMaker *make) {
  31. if (tempLabel) {
  32. make.top.equalTo(tempLabel.bottom);
  33. } else {
  34. make.top.equalTo((jxh_screenHeight() - self->_navigationHeight - 64) / 2);
  35. }
  36. make.leading.equalTo(0);
  37. make.trailing.equalTo(0);
  38. make.height.equalTo(self->_navigationHeight / self->_count);
  39. }];
  40. tempLabel = label;
  41. }
  42. return self;
  43. }
  44. - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
  45. UITouch *touch = [touches anyObject];
  46. CGPoint touchPoint = [touch locationInView:self];
  47. if (touchPoint.y > (jxh_screenHeight() - _navigationHeight - 64) / 2
  48. && touchPoint.y < (jxh_screenHeight() - _navigationHeight - 64) / 2 + _navigationHeight) {
  49. NSInteger temp = (touchPoint.y - (jxh_screenHeight() - _navigationHeight - 64) / 2) / (_navigationHeight / _count);
  50. if (temp > _count - 1) {
  51. self.spsb_section = _count - 1;
  52. } else {
  53. self.spsb_section = temp;
  54. }
  55. [self addNavigationLabel];
  56. }
  57. }
  58. - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
  59. UITouch *touch = [touches anyObject];
  60. CGPoint touchPoint = [touch locationInView:self];
  61. if (touchPoint.y <= (jxh_screenHeight() - _navigationHeight - 64) / 2) {
  62. self.spsb_section = 0;
  63. } else if (touchPoint.y >= (jxh_screenHeight() - _navigationHeight - 64) / 2 + _navigationHeight) {
  64. self.spsb_section = _count - 1;
  65. } else {
  66. NSInteger temp = (touchPoint.y - (jxh_screenHeight() - _navigationHeight - 64) / 2) / (_navigationHeight / _count);
  67. if (temp > _count - 1) {
  68. self.spsb_section = _count - 1;
  69. } else {
  70. self.spsb_section = temp;
  71. }
  72. }
  73. UILabel *navigationLabel = (UILabel *)[self viewWithTag:spsb_chooseCityNavigationLabelTag];
  74. if (navigationLabel) {
  75. navigationLabel.text = ((UILabel *)[self viewWithTag:spsb_chooseCityNavigationViewLabelTag + self.spsb_section]).text;
  76. } else {
  77. [self addNavigationLabel];
  78. }
  79. }
  80. - (void)touchesEnded:(NSSet *)touches
  81. withEvent:(UIEvent *)event {
  82. UILabel *navigationLabel = (UILabel *)[self viewWithTag:spsb_chooseCityNavigationLabelTag];
  83. [navigationLabel removeFromSuperview];
  84. }
  85. //添加导航标示
  86. - (void)addNavigationLabel {
  87. UILabel *navigationLabel = [UILabel convenienceWithFont:spsb_font(30) text:((UILabel *)[self viewWithTag:spsb_chooseCityNavigationViewLabelTag + self.spsb_section]).text textColor:spsb_FFFFFF_color(1.f) textAlignment:NSTextAlignmentCenter];
  88. navigationLabel.tag = spsb_chooseCityNavigationLabelTag;
  89. [navigationLabel setLayerCornerRadius:5 clipToBounds:true];
  90. navigationLabel.backgroundColor = spsb_000000_color(0.8);
  91. [self addSubview:navigationLabel];
  92. [navigationLabel makeConstraints:^(JXHConstraintMaker *make) {
  93. make.centerX.equalTo(navigationLabel.superview.superview.centerX);
  94. make.centerY.equalTo(navigationLabel.superview.superview.centerY).offset(-64 / 2);
  95. make.size.equalTo(CGSizeMake(80, 80));
  96. }];
  97. }
  98. @end
  99. #undef spsb_chooseCityNavigationHeight
  100. #undef spsb_chooseCityNavigationViewLabelTag
  101. #undef spsb_chooseCityNavigationLabelTag