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
//
//  SearcHotSearchViewCell.m
//  BuWanVideo2.0
//
//  Created by Aeline on 2021/5/30.
//  Copyright © 2021 com.yeshi.buwansheque.ios. All rights reserved.
//
 
#import "SearchHotSearchCell.h"
 
@interface SearchHotSearchCell ()
 
@property (nonatomic, nullable, strong) UIScrollView *scrollView;
 
@end
 
@implementation SearchHotSearchCell
 
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setupViewConfig];
    }
    return self;
}
 
- (void)setupViewConfig {
    [self addSubview:self.scrollView];
    self.scrollView.sd_layout.leftSpaceToView(self, 0).topSpaceToView(self, 0).rightSpaceToView(self, 0).bottomSpaceToView(self, 0);
    
}
 
- (void)setData:(NSDictionary *)data {
    _data = data;
    if (data) {
        if ([[self.scrollView subviews] count] > 1) {
            return;
        }
        
        NSUInteger count = [data allKeys].count;
        
        for (int i = 0; i < count; i++) {
            NSString *title = [data allKeys][i];
            
            UIView *viewBG = [[UIView alloc] initWithFrame:CGRectMake(10 + 10 * i + 284 * i, 0, 284, 198)];
            [self.scrollView addSubview:viewBG];
            viewBG.layer.masksToBounds = YES;
            viewBG.layer.cornerRadius = 6;
            viewBG.layer.borderWidth = 0.5;
            viewBG.layer.borderColor = [UIColor colorWithRed:255/255.0 green:236/255.0 blue:197/255.0 alpha:1.0].CGColor;
            
        
            UIView *viewTop = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 284, 32)];
            [viewBG addSubview:viewTop];
            CAGradientLayer *layer = [CAGradientLayer layer];
            layer.startPoint = CGPointMake(0, 0);//(0,0)表示从左上角开始变化。默认值是(0.5,0.0)表示从x轴为中间,y为顶端的开始变化
            layer.endPoint = CGPointMake(1, 1);//(1,1)表示到右下角变化结束。默认值是(0.5,1.0)  表示从x轴为中间,y为低端的结束变化
            layer.colors = [NSArray arrayWithObjects:UICOLOR_FROM_RGB(0xFFF8E5, 0.8).CGColor, UIColorFromRGBValue(0xFFFFFF).CGColor, nil];
            layer.locations = @[@0.0f, @1.0f];//渐变颜色的区间分布,locations的数组长度和color一致,这个值一般不用管它,默认是nil,会平均分布
            layer.frame = viewTop.layer.bounds;
            [viewTop.layer insertSublayer:layer atIndex:0];
            
            UILabel *labelTitle = [[UILabel alloc] initWithFrame:CGRectMake(18, 18, 200, 14)];
            labelTitle.text = title;
            labelTitle.textAlignment = NSTextAlignmentLeft;
            labelTitle.textColor = [UIColor blackColor];
            labelTitle.font = [UIFont boldSystemFontOfSize:14];
            [viewBG addSubview:labelTitle];
            
            NSUInteger nCount = [_data[title] count];
            
            for (int j = 0; j < nCount; j++) {
                NSString *name = _data[title][j];
                
                UILabel *labelNumber = [[UILabel alloc] init];
                labelNumber.text = [NSString stringWithFormat:@"%d", j + 1];
                labelNumber.textAlignment = NSTextAlignmentCenter;
                labelNumber.textColor = [UIColor whiteColor];
                labelNumber.font = [UIFont boldSystemFontOfSize:11];
                [viewBG addSubview:labelNumber];
                if (j == 0) {
                    labelNumber.backgroundColor = UICOLOR_FROM_RGB(0xFE3E3C, 1);
 
                } else if (j == 1) {
                    labelNumber.backgroundColor = UICOLOR_FROM_RGB(0xFE853C, 1);
                    
                } else if (j == 2) {
                    labelNumber.backgroundColor = UICOLOR_FROM_RGB(0xFEC03C, 1);
                    
                } else {
                        labelNumber.backgroundColor = UICOLOR_FROM_RGB(0xC2C2C2, 1);
                }
                
                UILabel *labelName = [[UILabel alloc] init];
                labelName.text = name;
                labelName.textAlignment = NSTextAlignmentLeft;
                labelName.textColor = [UIColor blackColor];
                labelName.font = [UIFont boldSystemFontOfSize:12];
                [viewBG addSubview:labelName];
                
                UIButton *buttonTap = [UIButton buttonWithType:UIButtonTypeCustom];
                buttonTap.titleLabel.text = name;
                [viewBG addSubview:buttonTap];
             
                if (j > 4) {
                    labelNumber.sd_layout.leftSpaceToView(viewBG, 152).topSpaceToView(viewBG, 49 + 17 * (j - 5) + 11 * (j - 5)).widthIs(17).heightIs(17);
                    labelName.sd_layout.leftSpaceToView(labelNumber, 13).centerYEqualToView(labelNumber).widthIs(90).heightIs(17);
                    
                    buttonTap.sd_layout.leftSpaceToView(viewBG, 152).topSpaceToView(viewBG, 49 + 17 * (j - 5) + 11 * (j - 5)).widthIs(120).heightIs(17);
                    
                } else {
                    labelNumber.sd_layout.leftSpaceToView(viewBG, 16).topSpaceToView(viewBG, 49 + 17 * j + 11 * j).widthIs(17).heightIs(17);
                    labelName.sd_layout.leftSpaceToView(labelNumber, 13).centerYEqualToView(labelNumber).widthIs(100).heightIs(17);
                    
                    buttonTap.sd_layout.leftSpaceToView(viewBG, 16).topSpaceToView(viewBG, 49 + 17 * j + 11 * j).widthIs(130).heightIs(17);
                }
                labelNumber.sd_cornerRadius = @4;
                
                [buttonTap addTarget:self action:@selector(touchTitle:) forControlEvents:UIControlEventTouchUpInside];
            }
        }
        
        self.scrollView.contentSize = CGSizeMake(10 + 10 * (count-1) + 284 * (count) + 10, 198);
    }
}
 
- (void)touchTitle:(UIButton *)button {
    NSString *title = button.titleLabel.text;
    if (_delegate && [_delegate respondsToSelector:@selector(hotSearchEvent:)]) {
        [_delegate hotSearchEvent:title];
    }
}
 
- (UIScrollView *)scrollView {
    if (!_scrollView) {
        _scrollView = [[UIScrollView alloc] init];
        _scrollView.showsHorizontalScrollIndicator = NO;
    }
    return _scrollView;
}
@end