财神随手记账

HPSlideSegmentBackgroundView.m 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. //
  2. // HPSlideSegmentBackgroundView.m
  3. // HPSlideSegmentView
  4. //
  5. // Created by 何鹏 on 17/6/13.
  6. // Copyright © 2017年 何鹏. All rights reserved.
  7. //
  8. #import "HPSlideSegmentBackgroundView.h"
  9. #import "HPSlideSegmentLogic.h"
  10. #import "UIView+HPSlideSegmentRect.h"
  11. @interface HPSlideSegmentBackgroundView ()<SlideModuleViewDelegate,HPSlideSegmentViewDelegate>
  12. @property(nonatomic,assign) NSUInteger arrayCount;
  13. @end
  14. @implementation HPSlideSegmentBackgroundView
  15. -(instancetype)initWithFrame:(CGRect)frame
  16. {
  17. if (self=[super initWithFrame:frame]) {
  18. [self updateData];
  19. [self slideSegmentlayout];
  20. [self slideAction];
  21. }
  22. return self;
  23. }
  24. -(void)layoutSubviews{
  25. [super layoutSubviews];
  26. _slideModuleView.frame=CGRectMake(_slideModuleView.x, _slideModuleView.y, self.width, _slideModuleView.height);
  27. CGFloat y=self.slideModuleView.x+self.slideModuleView.height;
  28. CGFloat height=self.height-_slideModuleViewHeight;
  29. _slideSegmenView.frame=CGRectMake(0, y, self.width, height);
  30. }
  31. -(void)awakeFromNib
  32. {
  33. [super awakeFromNib];
  34. [self updateData];
  35. [self slideSegmentlayout];
  36. [self slideAction];
  37. }
  38. -(void)updateData
  39. {
  40. if ([self.dataSource respondsToSelector:@selector(hp_slideListWithCount)]) {
  41. self.arrayCount=[self.dataSource hp_slideListWithCount];
  42. }
  43. }
  44. -(void)updateLayout
  45. {
  46. //更新布局
  47. [self updateData];
  48. [self slideSegmentlayout];
  49. }
  50. -(void)updateLayoutWithIndex:(NSUInteger)index
  51. {
  52. [self.slideSegmenView updateLayout:index];
  53. [self.slideModuleView updateLayoutWithIndex:index];
  54. }
  55. -(void)slideSegmentlayout
  56. {
  57. [self.slideSegmenView updateScrollerViewWidthWidth:self.arrayCount];
  58. self.slideModuleView.showCount=self.arrayCount;
  59. [self addSubview:self.slideModuleView];
  60. [self addSubview:self.slideSegmenView];
  61. }
  62. -(void)slideAction
  63. {
  64. [self.slideModuleView hp_weak:self
  65. actionButton:^(HPSlideSegmentBackgroundView *weak, NSUInteger buttonIndex) {
  66. [weak.slideSegmenView updateLayout:buttonIndex updateDelegate:NO];
  67. }];
  68. }
  69. #pragma mark - HPSlideSegmentViewDataSouce
  70. -(void)hp_slideWithNowIndex:(NSUInteger)nowIndex readyIndex:(NSUInteger)readyIndex movePercent:(CGFloat)movePercent
  71. {
  72. [self.slideModuleView slideWithNowIndex:nowIndex readyIndex:readyIndex movePercent:movePercent];
  73. }
  74. #pragma mark - SlideModuleViewDelegate
  75. -(NSString *)hp_slideContentWithIndex:(NSUInteger)index
  76. {
  77. return self.contents[index];
  78. }
  79. #pragma mark - 懒加载
  80. -(void)setSlideModuleViewHeight:(CGFloat)slideModuleViewHeight
  81. {
  82. if (slideModuleViewHeight<30) {
  83. slideModuleViewHeight=30;
  84. }
  85. _slideModuleViewHeight=slideModuleViewHeight;
  86. _slideModuleView.frame=CGRectMake(0, 0, self.width, slideModuleViewHeight);
  87. CGFloat y=self.slideModuleView.x+self.slideModuleView.height;
  88. _slideSegmenView.frame=CGRectMake(0, y, self.width, self.height-y);
  89. [self updateLayout];
  90. }
  91. -(void)setDataSource:(id<HPSlideSegmentBackgroundDataSource>)dataSource
  92. {
  93. _dataSource=dataSource;
  94. [self updateLayout];
  95. }
  96. -(void)setContents:(NSArray<NSString *> *)contents
  97. {
  98. _contents=contents;
  99. [self updateLayout];
  100. }
  101. -(HPSlideModuleView *)slideModuleView
  102. {
  103. if (_slideModuleView==nil) {
  104. _slideModuleView=[[HPSlideModuleView alloc] initWithFrame:CGRectMake(0, 0, self.width, 30)];
  105. _slideModuleView.delegate=self;
  106. }
  107. return _slideModuleView;
  108. }
  109. -(HPSlideSegmentView *)slideSegmenView
  110. {
  111. CGFloat y=self.slideModuleView.x+self.slideModuleView.height;
  112. if (_slideSegmenView==nil) {
  113. _slideSegmenView=[[HPSlideSegmentView alloc] initWithFrame:CGRectMake(0, y, self.width, self.height)];
  114. _slideSegmenView.delegate=self;
  115. }
  116. return _slideSegmenView;
  117. }
  118. @end