Ei kuvausta

FKSegmentControl.m 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. //
  2. // FKSegmentControl.m
  3. // FirstLink
  4. //
  5. // Created by jack on 16/5/10.
  6. // Copyright © 2016年 FirstLink. All rights reserved.
  7. //
  8. #import "FKSegmentControl.h"
  9. @interface FKSegmentItemView : UIControl
  10. @property (nonatomic, strong) UILabel *titleLabel;
  11. @end
  12. @implementation FKSegmentItemView
  13. - (instancetype)initWithFrame:(CGRect)frame{
  14. if (self = [super initWithFrame:frame]) {
  15. [self addAllSubviews];
  16. }
  17. return self;
  18. }
  19. - (void)addAllSubviews{
  20. [self addSubview:self.titleLabel];
  21. [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  22. make.center.equalTo(self);
  23. }];
  24. }
  25. - (void)setSelected:(BOOL)selected{
  26. [super setSelected:selected];
  27. if (selected) {
  28. self.backgroundColor = UIColorFromRGB(0xccccccc);
  29. // self.titleLabel.textColor = UIColorFromRGB(0x4a4a4a);
  30. }else{
  31. self.backgroundColor = UIColorFromRGB(0xffffff);
  32. // self.titleLabel.textColor = UIColorFromRGB(0x9b9b9b);
  33. }
  34. }
  35. - (UILabel *)titleLabel{
  36. if (_titleLabel == nil) {
  37. _titleLabel = [[UILabel alloc]init];
  38. _titleLabel.font = [UIFont systemFontOfSize:14];
  39. _titleLabel.textColor = UIColorFromRGB(0x4a4a4a);
  40. }
  41. return _titleLabel;
  42. }
  43. @end
  44. ////////////////////////////////////////////////////////////////////////////////////////////////////////////
  45. @interface FKSegmentControl ()
  46. @property (nonatomic, strong) NSMutableArray *itemViewArrayM;
  47. @end
  48. @implementation FKSegmentControl
  49. - (instancetype)initWithIndexTitles:(NSArray *)titleArray{
  50. if (self = [super init]) {
  51. self.titleArray = titleArray;
  52. self.layer.cornerRadius = 3.0f;
  53. self.layer.borderColor = UIColorFromRGB(0xccccccc).CGColor;
  54. self.layer.borderWidth = 0.5;
  55. self.clipsToBounds = YES;
  56. [self buildSubviewWithTitleArray:self.titleArray];
  57. }
  58. return self;
  59. }
  60. - (void)buildSubviewWithTitleArray:(NSArray *)titleArray{
  61. if (titleArray.count == 0) return;
  62. for (UIView *view in self.subviews) {
  63. if ([view isKindOfClass:[FKSegmentItemView class]]){
  64. [view removeFromSuperview];
  65. }
  66. }
  67. for (FKSegmentItemView *itemView in self.itemViewArrayM) {
  68. [itemView setSelected:NO];
  69. }
  70. while (self.itemViewArrayM.count < titleArray.count) {
  71. FKSegmentItemView *itemView = [[FKSegmentItemView alloc]init];
  72. [itemView addTarget:self
  73. action:@selector(clickItemView:)
  74. forControlEvents:UIControlEventTouchUpInside];
  75. [self.itemViewArrayM addObject:itemView];
  76. }
  77. UIView *uperView = nil;
  78. for (NSInteger i = 0; i < titleArray.count; i++) {
  79. FKSegmentItemView *itemView = self.itemViewArrayM[i];
  80. itemView.titleLabel.text = titleArray[i];
  81. itemView.selected = NO;
  82. if (i == self.defaultSelectIndex) itemView.selected = YES;
  83. [self addSubview:itemView];
  84. [itemView mas_makeConstraints:^(MASConstraintMaker *make) {
  85. if (i == 0){
  86. make.left.equalTo(self);
  87. }else if (i == titleArray.count - 1){
  88. make.left.equalTo(uperView.mas_right);
  89. make.right.equalTo(self);
  90. }else{
  91. make.left.equalTo(uperView.mas_right);
  92. make.right.equalTo(self);
  93. }
  94. if (uperView){
  95. make.width.equalTo(uperView);
  96. }
  97. make.top.bottom.equalTo(self);
  98. }];
  99. uperView = itemView;
  100. }
  101. }
  102. - (void)clickItemView:(FKSegmentItemView *)sender{
  103. if (sender.selected) return;
  104. [self resetAllSelectedState];
  105. sender.selected = YES;
  106. NSUInteger index = [self.itemViewArrayM indexOfObject:sender];
  107. if (self.changeIndex){
  108. self.changeIndex(index);
  109. }
  110. }
  111. - (void)resetAllSelectedState{
  112. for (FKSegmentItemView *itemView in self.subviews) {
  113. if ([itemView isKindOfClass:[FKSegmentItemView class]]){
  114. itemView.selected = NO;
  115. }
  116. }
  117. }
  118. - (NSMutableArray *)itemViewArrayM{
  119. if (_itemViewArrayM == nil) {
  120. _itemViewArrayM = [[NSMutableArray alloc]init];
  121. }
  122. return _itemViewArrayM;
  123. }
  124. @end