ShopMallClassView.m 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. //
  2. // ShopMallClassView.m
  3. // YBLive
  4. //
  5. // Created by ybRRR on 2020/8/19.
  6. // Copyright © 2020 cat. All rights reserved.
  7. //
  8. #import "ShopMallClassView.h"
  9. @implementation ShopMallClassView
  10. -(instancetype)initWithFrame:(CGRect)frame andData:(NSArray *)dataArr{
  11. self = [super initWithFrame:frame];
  12. if (self) {
  13. listArr = dataArr;
  14. self.backgroundColor = UIColor.whiteColor;
  15. backScroll = [[UIScrollView alloc]initWithFrame:CGRectMake(10, 0, self.width-20, self.height)];
  16. backScroll.showsHorizontalScrollIndicator = NO;
  17. backScroll.backgroundColor = UIColor.whiteColor;
  18. backScroll.delegate = self;
  19. backScroll.layer.cornerRadius = 5;
  20. backScroll.layer.masksToBounds = YES;
  21. [self addSubview:backScroll];
  22. [self addBottomSlider];
  23. if (dataArr.count > 12) {
  24. backScroll.contentSize = CGSizeMake(((_window_width-20)/5.5)*(dataArr.count)/2+20, self.height);
  25. }else{
  26. backScroll.contentSize = CGSizeMake(((_window_width-20)/5.5)*(dataArr.count)+20, self.height);
  27. }
  28. if (dataArr.count > 12) {
  29. for (int i = 0 ; i <dataArr.count; i ++) {
  30. NSString *gc_iconStr =[dataArr[i]valueForKey:@"gc_icon"];
  31. UIButton *btn = [UIButton buttonWithType:0];
  32. btn.frame = CGRectMake(i/2*((_window_width-20)/5.5), i%2*55 +i%2*10, (_window_width-20)/5.5, 55);
  33. [btn addTarget:self action:@selector(classBtnClick:) forControlEvents:UIControlEventTouchUpInside];
  34. btn.tag = 10000+i;
  35. [backScroll addSubview:btn];
  36. UIImageView *img = [[UIImageView alloc]init];
  37. [img sd_setImageWithURL:[NSURL URLWithString:gc_iconStr]];
  38. [btn addSubview:img];
  39. [img mas_makeConstraints:^(MASConstraintMaker *make) {
  40. make.center.equalTo(btn);
  41. make.height.width.mas_equalTo(35);
  42. }];
  43. [btn addSubview:img];
  44. UILabel *titleLb = [[UILabel alloc]init];
  45. titleLb.font = [UIFont systemFontOfSize:11];
  46. titleLb.textColor = UIColor.blackColor;
  47. titleLb.textAlignment = NSTextAlignmentCenter;
  48. titleLb.text = minstr([dataArr[i]valueForKey:@"gc_name"]);
  49. [btn addSubview:titleLb];
  50. [titleLb mas_makeConstraints:^(MASConstraintMaker *make) {
  51. make.centerX.equalTo(img);
  52. make.top.equalTo(img.mas_bottom).offset(5);
  53. make.width.equalTo(btn.mas_width).offset(-8);
  54. }];
  55. }
  56. }else{
  57. for (int i = 0; i < dataArr.count; i ++) {
  58. NSString *gc_iconStr =[dataArr[i]valueForKey:@"gc_icon"];
  59. UIButton *btn = [UIButton buttonWithType:0];
  60. btn.frame = CGRectMake(i * ((_window_width-20)/5.5), 0, ((_window_width-20)/5.5), 55);
  61. [btn addTarget:self action:@selector(classBtnClick:) forControlEvents:UIControlEventTouchUpInside];
  62. btn.tag = 10000+i;
  63. [backScroll addSubview:btn];
  64. UIImageView *img = [[UIImageView alloc]init];
  65. [img sd_setImageWithURL:[NSURL URLWithString:gc_iconStr]];
  66. [btn addSubview:img];
  67. [img mas_makeConstraints:^(MASConstraintMaker *make) {
  68. make.center.equalTo(btn);
  69. make.height.width.mas_equalTo(35);
  70. }];
  71. [btn addSubview:img];
  72. UILabel *titleLb = [[UILabel alloc]init];
  73. titleLb.font = [UIFont systemFontOfSize:11];
  74. titleLb.textColor = [UIColor blackColor];
  75. titleLb.textAlignment = NSTextAlignmentCenter;
  76. titleLb.text = minstr([dataArr[i]valueForKey:@"gc_name"]);
  77. [btn addSubview:titleLb];
  78. [titleLb mas_makeConstraints:^(MASConstraintMaker *make) {
  79. make.centerX.equalTo(img);
  80. make.top.equalTo(img.mas_bottom).offset(2);
  81. }];
  82. }
  83. }
  84. }
  85. return self;
  86. }
  87. -(void)addBottomSlider{
  88. slideBackView=[[UIView alloc] initWithFrame:CGRectMake(_window_width/2-20, backScroll.bottom-6, 40, 4)];
  89. [self addSubview:slideBackView];
  90. slideBackView.backgroundColor = [UIColor colorWithRed:0.91 green:0.91 blue:0.91 alpha:1.00];
  91. slideBackView.layer.cornerRadius = 2;
  92. sliderView = [[UIView alloc] init];
  93. [slideBackView addSubview:sliderView];
  94. sliderView.frame=CGRectMake(0, 0, 15 ,4);
  95. sliderView.backgroundColor =Pink_Cor;
  96. sliderView.layer.cornerRadius = 2;
  97. }
  98. -(void)scrollViewDidScroll:(UIScrollView *)scrollView{
  99. [UIView animateWithDuration:0.2 animations:^{
  100. CGPoint offset = scrollView.contentOffset;
  101. // scrollView的当前位移/scrollView的总位移=滑块的当前位移/滑块的总位移
  102. //offset/(scrollView.contentSize.width-scrollView.frame.size.width)=滑块的位移/(slideBackView.frame.size.width-sliderView.frame.size.width)
  103. // 滑块距离屏幕左边的距离加上滑块的当前位移,即为滑块当前的x
  104. CGRect frame=sliderView.frame;
  105. frame.origin.x=1 +offset.x*(slideBackView.frame.size.width-sliderView.frame.size.width)/(scrollView.contentSize.width-scrollView.frame.size.width);
  106. sliderView.frame = frame;
  107. }];
  108. }
  109. -(void)classBtnClick:(UIButton *)sender{
  110. NSInteger index = sender.tag -10000;
  111. NSDictionary *dataDic = listArr[index];
  112. if ([self.delegate respondsToSelector:@selector(selectClassData:)]) {
  113. [self.delegate selectClassData:dataDic];
  114. }
  115. }
  116. @end