GiftGroupView.m 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. //
  2. // GiftGroupView.m
  3. // BuguLive
  4. //
  5. // Created by 范东 on 2019/1/28.
  6. // Copyright © 2019 xfg. All rights reserved.
  7. //
  8. #import "GiftGroupView.h"
  9. #define kGiftGroupViewButtonBaseTag 100
  10. @interface GiftGroupView()
  11. @property (nonatomic, strong) UIScrollView *scrollView;
  12. @property (nonatomic, strong) NSArray *titleArray;
  13. @property (nonatomic, copy) clickGiftGroupBtnBlock clickGiftGroupBtnBlock;
  14. @end
  15. @implementation GiftGroupView
  16. - (instancetype)initWithFrame:(CGRect)frame TitleArray:(NSArray *)titleArray{
  17. if (self = [super initWithFrame:frame]) {
  18. self.backgroundColor = kClearColor;
  19. // [RGB(21, 6, 36) colorWithAlphaComponent:0.1];
  20. self.titleArray = titleArray;
  21. [self initSubview];
  22. }
  23. return self;
  24. }
  25. - (void)initSubview{
  26. [self addSubview:self.scrollView];
  27. // CGSize titleSize = [ASLocalizedString(@"测试")textSizeIn:CGSizeMake(MAXFLOAT, MAXFLOAT) font:[UIFont systemFontOfSize:15]];
  28. int buttonWidth = kScreenW - 80;
  29. for (NSInteger i = 0; i < self.titleArray.count; i++) {
  30. //礼物分组
  31. UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(i * buttonWidth / self.titleArray.count, 0, buttonWidth / self.titleArray.count, kRealValue(53))];
  32. button.tag = kGiftGroupViewButtonBaseTag + i;
  33. [button setTitle:self.titleArray[i] forState:UIControlStateNormal];
  34. [button setTitleColor:RGBA(255, 255, 255, 0.8) forState:UIControlStateNormal];
  35. [button setTitleColor:[UIColor colorWithHexString:@"#C28CF8"] forState:UIControlStateSelected];
  36. button.titleLabel.font = [UIFont systemFontOfSize:14];
  37. [button addTarget:self action:@selector(onClick:) forControlEvents:UIControlEventTouchUpInside];
  38. [self.scrollView addSubview:button];
  39. }
  40. for (NSInteger i =1 ; i < self.titleArray.count; i++) {
  41. UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(i*(buttonWidth) - 0.5, 0, 0.5, self.height)];
  42. lineView.backgroundColor = [kWhiteColor colorWithAlphaComponent:0.1];
  43. lineView.hidden = YES;
  44. [self.scrollView addSubview:lineView];
  45. }
  46. self.scrollView.contentSize = CGSizeMake(self.titleArray.count * self.width / 2, 0);
  47. UIButton *selectedBtn = (UIButton *)[self viewWithTag:kGiftGroupViewButtonBaseTag];
  48. selectedBtn.selected = YES;
  49. }
  50. - (void)onClick:(UIButton *)sender{
  51. for (UIView *subView in self.scrollView.subviews) {
  52. if ([subView isMemberOfClass:[UIButton class]]) {
  53. UIButton *button = (UIButton *)subView;
  54. button.selected = (button == sender);
  55. }
  56. }
  57. if (self.clickGiftGroupBtnBlock) {
  58. self.clickGiftGroupBtnBlock(sender.tag - kGiftGroupViewButtonBaseTag);
  59. }
  60. }
  61. - (UIScrollView *)scrollView{
  62. if (!_scrollView) {
  63. _scrollView = [[UIScrollView alloc]initWithFrame:self.bounds];
  64. _scrollView.showsVerticalScrollIndicator = NO;
  65. _scrollView.showsHorizontalScrollIndicator = NO;
  66. }
  67. return _scrollView;
  68. }
  69. -(void)resetIndexs:(NSInteger)index{
  70. UIButton *sender = [self.scrollView viewWithTag:kGiftGroupViewButtonBaseTag + index];
  71. for (UIView *subView in self.scrollView.subviews) {
  72. if ([subView isMemberOfClass:[UIButton class]]) {
  73. UIButton *button = (UIButton *)subView;
  74. button.selected = (button == sender);
  75. }
  76. }
  77. }
  78. //- (void)setIndex:(NSInteger)index{
  79. //
  80. //}
  81. - (void)setClickGiftGroupBtnBlock:(clickGiftGroupBtnBlock)clickGiftGroupBtnBlock{
  82. _clickGiftGroupBtnBlock = clickGiftGroupBtnBlock;
  83. }
  84. /*
  85. // Only override drawRect: if you perform custom drawing.
  86. // An empty implementation adversely affects performance during animation.
  87. - (void)drawRect:(CGRect)rect {
  88. // Drawing code
  89. }
  90. */
  91. @end