RDSHomeTopView.m 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. //
  2. // RDSHomeTopView.m
  3. // Temperature
  4. //
  5. // Created by RD on 2022/10/25.
  6. //
  7. #import "RDSHomeTopView.h"
  8. #import "NSDate+currentDate.h"
  9. #import <SDWebImage/SDWebImage.h>
  10. @interface RDSHomeTopView ()
  11. @property (weak, nonatomic) IBOutlet UIButton *addBtn;
  12. @property (weak, nonatomic) IBOutlet UIButton *messageBtn;
  13. @property (weak, nonatomic) IBOutlet UILabel *titleLab;
  14. @property (weak, nonatomic) IBOutlet UIImageView *topArrowImageView;
  15. @property (weak, nonatomic) IBOutlet UIView *firstView;
  16. @property (weak, nonatomic) IBOutlet UIView *secondView;
  17. @property (weak, nonatomic) IBOutlet UIView *thirdView;
  18. @property (weak, nonatomic) IBOutlet NSLayoutConstraint *firstViewWidth;
  19. @property (weak, nonatomic) IBOutlet UILabel *tempLab;// 温度
  20. @property (weak, nonatomic) IBOutlet UILabel *cityLab;// 城市
  21. @property (weak, nonatomic) IBOutlet UILabel *rhLab;// 湿度
  22. @property (weak, nonatomic) IBOutlet UIView *styleView;
  23. @property (weak, nonatomic) IBOutlet NSLayoutConstraint *styleHeight;
  24. @property (weak, nonatomic) UIScrollView *styleScrollView;
  25. @end
  26. @implementation RDSHomeTopView
  27. - (void)awakeFromNib {
  28. [super awakeFromNib];
  29. // Initialization code
  30. [self creatStyleScrollView];
  31. self.redView.hidden = YES;
  32. self.titleLab.userInteractionEnabled = YES;
  33. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(titleTapAction:)];
  34. [self.titleLab addGestureRecognizer:tap];
  35. self.topArrowImageView.userInteractionEnabled = YES;
  36. UITapGestureRecognizer *imgTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(titleTapAction:)];
  37. [self.topArrowImageView addGestureRecognizer:imgTap];
  38. }
  39. -(void)titleTapAction:(UITapGestureRecognizer *)tap{
  40. if (self.headTitleClick) {
  41. self.headTitleClick(self.titleLab);
  42. }
  43. }
  44. - (void)setTitle:(NSString *)title{
  45. _title = title;
  46. _titleLab.text = title;
  47. }
  48. - (void)setModel:(WeatherModel *)model{
  49. _model = model;
  50. if (model.name == nil) {
  51. _cityLab.text = @"点击获取位置";
  52. _cityLab.textColor = [UIColor colorWithHexString:@"#267AFF"];
  53. _firstViewWidth.constant = 112;
  54. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(firstTapAction)];
  55. [_firstView addGestureRecognizer:tap];
  56. self.secondView.hidden = YES;
  57. self.thirdView.hidden = YES;
  58. }else{
  59. _cityLab.text = model.name;
  60. _firstViewWidth.constant = _cityLab.text.length * 10 + 55;
  61. _cityLab.textColor = [UIColor colorWithHexString:@"#333333"];
  62. _tempLab.text = [NSString stringWithFormat:@"%@°C", model.temperature];
  63. _rhLab.text = [NSString stringWithFormat:@"%@%%", model.humidity];
  64. self.secondView.hidden = NO;
  65. self.thirdView.hidden = NO;
  66. }
  67. }
  68. -(void)firstTapAction
  69. {
  70. if (self.locationClick) {
  71. self.locationClick();
  72. }
  73. }
  74. - (void)setSystemParameter:(RDSSystemParameterModel *)systemParameter {
  75. _systemParameter = systemParameter;
  76. }
  77. - (IBAction)clickAddBtnAction:(id)sender {
  78. if (self.addBtnClick) {
  79. self.addBtnClick(sender);
  80. }
  81. }
  82. - (IBAction)clickMessageBtnAction:(id)sender {
  83. if (self.messageClick) {
  84. self.messageClick(sender);
  85. }
  86. }
  87. -(void)creatStyleScrollView{
  88. UIScrollView *styleScrollView = [[UIScrollView alloc]init];
  89. styleScrollView.showsVerticalScrollIndicator = NO;
  90. styleScrollView.showsHorizontalScrollIndicator = NO;
  91. [self.styleView addSubview:styleScrollView];
  92. self.styleScrollView = styleScrollView;
  93. [styleScrollView mas_makeConstraints:^(MASConstraintMaker *make) {
  94. make.left.equalTo(@0);
  95. make.right.equalTo(@0);
  96. make.bottom.equalTo(@0);
  97. make.top.equalTo(@0);
  98. }];
  99. [self addItemView];
  100. }
  101. -(void)setScenesArray:(NSArray *)scenesArray
  102. {
  103. _scenesArray = scenesArray;
  104. [self addItemView];
  105. }
  106. -(void)addItemView{
  107. CGFloat itemWidth = 130;
  108. CGFloat itemSpace = 15;
  109. self.styleScrollView.contentSize = CGSizeMake(self.scenesArray.count * (itemWidth + itemSpace), 45);
  110. [self.styleScrollView removeAllSubviews];
  111. for (int i = 0; i < self.scenesArray.count; i++) {
  112. YXHomeSceneModel *model = self.scenesArray[i];
  113. NSString *text = model.name;
  114. YXItemView *itemView = [[YXItemView alloc]init];
  115. itemView.frame = CGRectMake(i*(itemWidth + itemSpace), 0, itemWidth, self.styleScrollView.frame.size.height);
  116. [itemView.picImgView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@", TheApiHelper.baseUrlHeadImg, model.icon]]];
  117. itemView.textLabel.text = text;
  118. itemView.layer.cornerRadius = 5;
  119. if ([model.status integerValue] == 1) {
  120. itemView.colorView.backgroundColor = [UIColor colorWithHexString:@"#48E441"];
  121. }else{
  122. itemView.colorView.backgroundColor = [UIColor colorWithHexString:@"#CBD0DB"];
  123. }
  124. [self.styleScrollView addSubview:itemView];
  125. itemView.tag = i;
  126. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(itemTapAction:)];
  127. [itemView addGestureRecognizer:tap];
  128. }
  129. }
  130. -(void)itemTapAction:(UITapGestureRecognizer *)tap{
  131. NSInteger tag = tap.view.tag;
  132. NSArray *views = self.styleScrollView.subviews;
  133. for (int i = 0; i < views.count; i++) {
  134. YXItemView *itemView = views[i];
  135. if (i == tag) {
  136. itemView.colorView.backgroundColor = [UIColor colorWithHexString:@"#48E441"];
  137. }else{
  138. itemView.colorView.backgroundColor = [UIColor colorWithHexString:@"#CBD0DB"];
  139. }
  140. }
  141. if (self.itemClick) {
  142. self.itemClick(tag);
  143. }
  144. }
  145. @end