admin
2023-04-21 0b3a4aaf99ea251bc8e27b96115288f0988fcffe
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
//
//  SearchTitleView.m
//  BuWanVideo2.0
//
//  Created by Aeline on 2020/9/19.
//  Copyright © 2020 com.yeshi.buwansheque.ios. All rights reserved.
//
 
#import "SearchTitleView.h"
 
@interface SearchTitleView ()
 
@property (nonatomic, strong) UIScrollView *scrollView;
 
@property (nonatomic, strong) UIButton *buttonLast;
 
@property (nonatomic, strong) UIView *line;
@end
 
@implementation SearchTitleView
 
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = UIColorFromRGBValue(0x000000);
        [self setupViewConifg];
    }
    return self;
}
 
- (void)setupViewConifg {
    [self addSubview:self.scrollView];
}
 
- (void)setTitleData:(NSArray *)typeList :(BOOL)isSearch {
    self.typeList = typeList;
    if (typeList) {
        //if ([[self.scrollView subviews] count] > 1) return;
        if (!isSearch) return;
        
        for (UIView *view in [self.scrollView subviews]) {
            [view removeFromSuperview];
        }
        
        CGFloat scrWidth = 0;
        for (int i = 0; i < typeList.count; i++) {
            UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
            [button setTitle:typeList[i][@"Name"] forState:UIControlStateNormal];
            [button setTitleColor:UIColorFromRGBValue(0x999999) forState:UIControlStateNormal];
            [button setTitleColor:UIColorFromRGBValue(0xFFFFFF) forState:UIControlStateSelected];
            button.titleLabel.font = [UIFont systemFontOfSize:12];
            button.tag = i;
            [self.scrollView addSubview:button];
            
            if (i == 0) {
                button.selected = YES;
                self.buttonLast = button;
            }
            CGFloat width = 0;
            width = [self sizeWithText:typeList[i][@"Name"] font:[UIFont boldSystemFontOfSize:12] maxSize:CGSizeMake(MAXFLOAT, MAXFLOAT)].width;
  
            if (i == 0) {
                scrWidth = 17;
            }
            
            button.frame = CGRectMake(scrWidth, 5, width, 30);
            
            scrWidth += width + 25;
            
            [button addTarget:self action:@selector(touchIndex:) forControlEvents:UIControlEventTouchUpInside];
            
            if (i == 0) {
                button.transform = CGAffineTransformMakeScale(1.5, 1.5);
            }
            
        }
        [self.scrollView addSubview:self.line];
        UIButton *button = [self.scrollView subviews][1];
        self.line.frame = CGRectMake(11, 38, button.bounds.size.width, 2);
        self.scrollView.contentSize = CGSizeMake(scrWidth, 40);
    }
}
 
- (void)touchIndex:(UIButton *)button {
    
    if (button.tag == self.buttonLast.tag) return;
    self.buttonLast.transform = CGAffineTransformMakeScale(1.0, 1.0);
    self.buttonLast.selected = NO;
    //self.buttonLast.titleLabel.font = [UIFont systemFontOfSize:12];
    
    button.transform = CGAffineTransformMakeScale(1.5, 1.5);
    button.selected = YES;
    // button.titleLabel.font = [UIFont boldSystemFontOfSize:17];
    
    
    //    CGFloat width1 = [self sizeWithText:self.buttonLast.titleLabel.text font:[UIFont systemFontOfSize:12] maxSize:CGSizeMake(MAXFLOAT, MAXFLOAT)].width;
    //
    //
    //    CGFloat width2 = [self sizeWithText:button.titleLabel.text font:[UIFont boldSystemFontOfSize:17] maxSize:CGSizeMake(MAXFLOAT, MAXFLOAT)].width;
    //
    //
    //    self.buttonLast.frame = CGRectMake(self.buttonLast.frame.origin.x, 5, width1, 30);
    //
    //    button.frame = CGRectMake(button.frame.origin.x, 5, width2, 30);
    
    self.buttonLast = button;
    __weak typeof(self) weakSelf = self;
    [UIView animateWithDuration:0.2 animations:^{
        if ([_typeList[button.tag][@"Name"] length] >= 3) {
            weakSelf.line.frame = CGRectMake(button.frame.origin.x, 38, button.bounds.size.width + 20, 2);
            
        } else {
            weakSelf.line.frame = CGRectMake(button.frame.origin.x, 38, button.bounds.size.width + 10, 2);
        }
       
    }];
    
    if (_delegate && [_delegate respondsToSelector:@selector(selectType:)]) {
        [_delegate selectType:[_typeList[button.tag][@"Id"] integerValue]];
    }
}
 
- (UIScrollView *)scrollView {
    if (!_scrollView) {
        _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, KScreenW, 40)];
        _scrollView.showsHorizontalScrollIndicator = NO;
    }
    return _scrollView;
}
 
- (UIView *)line {
    if (!_line) {
        _line = [[UIView alloc] init];
        _line.backgroundColor = UIColorFromRGBValue(0xFFE84E);
    }
    return _line;
}
 
- (CGSize)sizeWithText:(NSString *)text font:(UIFont *)font maxSize:(CGSize)maxSize {
    NSDictionary *attrs = @{NSFontAttributeName:font};
    return [text boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:attrs context:nil].size;
}
 
@end