No Description

FKProSegmentView.m 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. //
  2. // FKProSegmentView.m
  3. // FirstLink
  4. //
  5. // Created by jack on 16/8/13.
  6. // Copyright © 2016年 FirstLink. All rights reserved.
  7. //
  8. #import "FKProSegmentView.h"
  9. @interface FKProSegmentView ()
  10. @property (nonatomic, strong) UILabel *leftLabel;
  11. @property (nonatomic, strong) UILabel *rightLabel;
  12. @property (nonatomic, strong) UIView *horLine;
  13. @property (nonatomic, copy) void(^indexChange)(NSInteger index);
  14. @property (nonatomic, strong) MASConstraint *constToCenterX;
  15. @end
  16. @implementation FKProSegmentView
  17. - (instancetype)initWithIndexChange:(void(^)(NSInteger index))indexChange{
  18. if (self = [super init]) {
  19. [self addAllSubviews];
  20. self.indexChange = indexChange;
  21. }
  22. return self;
  23. }
  24. - (void)addAllSubviews{
  25. self.backgroundColor = [UIColor whiteColor];
  26. UIButton *leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  27. UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  28. leftBtn.tag = 0;
  29. rightBtn.tag = 1;
  30. [leftBtn addTarget:self
  31. action:@selector(changeIndex:)
  32. forControlEvents:UIControlEventTouchUpInside];
  33. [rightBtn addTarget:self
  34. action:@selector(changeIndex:)
  35. forControlEvents:UIControlEventTouchUpInside];
  36. [self addSubview:self.leftLabel];
  37. [self addSubview:self.rightLabel];
  38. [self addSubview:self.horLine];
  39. [self addSubview:leftBtn];
  40. [self addSubview:rightBtn];
  41. [self.leftLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  42. make.right.equalTo(self.mas_centerX).offset(- 50);
  43. make.centerY.equalTo(self);
  44. }];
  45. [self.rightLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  46. make.left.equalTo(self.mas_centerX).offset(50);
  47. make.centerY.equalTo(self);
  48. }];
  49. [self.horLine mas_makeConstraints:^(MASConstraintMaker *make) {
  50. self.constToCenterX = make.centerX.equalTo(self.leftLabel);
  51. make.bottom.equalTo(self).offset(- 5);
  52. make.size.mas_equalTo(CGSizeMake(60, 2));
  53. }];
  54. [leftBtn mas_makeConstraints:^(MASConstraintMaker *make) {
  55. make.left.top.bottom.equalTo(self);
  56. make.right.equalTo(self.mas_centerX);
  57. }];
  58. [rightBtn mas_makeConstraints:^(MASConstraintMaker *make) {
  59. make.right.top.bottom.equalTo(self);
  60. make.left.equalTo(self.mas_centerX);
  61. }];
  62. }
  63. - (void)changeIndex:(UIButton *)sender{
  64. if (sender.tag == self.horLine.tag) return;
  65. self.leftLabel.textColor = UIColorFromRGB(0x666666);
  66. self.rightLabel.textColor = UIColorFromRGB(0x666666);
  67. UILabel *targetLabel = self.leftLabel;
  68. if (sender.tag == 1) targetLabel = self.rightLabel;
  69. targetLabel.textColor = UIColorFromRGB(0x333333);
  70. [self.horLine mas_remakeConstraints:^(MASConstraintMaker *make) {
  71. make.centerX.equalTo(targetLabel);
  72. make.bottom.equalTo(self).offset(- 5);
  73. make.size.mas_equalTo(CGSizeMake(60, 2));
  74. }];
  75. // self.constToCenterX.equalTo(targetLabel);
  76. self.horLine.tag = sender.tag;
  77. if (self.indexChange) {
  78. self.indexChange(sender.tag);
  79. }
  80. }
  81. - (NSInteger)currentIndex{
  82. return self.horLine.tag;
  83. }
  84. - (UILabel *)leftLabel{
  85. if (_leftLabel == nil) {
  86. _leftLabel = [[UILabel alloc]init];
  87. _leftLabel.textColor = UIColorFromRGB(0x333333);
  88. _leftLabel.font = [UIFont systemFontOfSize:14];
  89. _leftLabel.text = @"商品详情";
  90. }
  91. return _leftLabel;
  92. }
  93. - (UILabel *)rightLabel{
  94. if (_rightLabel == nil) {
  95. _rightLabel = [[UILabel alloc]init];
  96. _rightLabel.textColor = UIColorFromRGB(0x666666);
  97. _rightLabel.font = [UIFont systemFontOfSize:14];
  98. _rightLabel.text = @"温馨提示";
  99. }
  100. return _rightLabel;
  101. }
  102. - (UIView *)horLine{
  103. if (_horLine == nil) {
  104. _horLine = [[UIView alloc]init];
  105. _horLine.backgroundColor = UIColorFromRGB(0xff6362);
  106. _horLine.tag = 0;
  107. }
  108. return _horLine;
  109. }
  110. @end