// // WatchHomeHeaderView.m // tongxin // // Created by ecell on 2023/4/25. // Copyright © 2023 xTT. All rights reserved. // #import "WatchHomeHeaderView.h" #import "ZZBadgeButton.h" #import "baseModel.h" #import "Device.h" #import "User.h" #import "DisturbBanViewController.h" #import "ContactsViewController.h" #import "NewStepVC.h" #import "EllipsePageControl.h" #import "ShortMessageListViewController.h" @interface WatchHomeHeaderView () @property (nonatomic ,strong) UIScrollView *bgScrollView; @property (nonatomic ,strong) EllipsePageControl *pageControl; @property (nonatomic ,weak) UIView *bgView; /// 按钮菜单View @property (nonatomic ,weak) UIView *btnView; ///昵称 @property (nonatomic ,weak) UILabel *nameLabel; /// 头像 @property (nonatomic ,weak) UIImageView *iconImg; /// 电量 @property (nonatomic ,weak) UIImageView *kwhImg; ///电量 @property (nonatomic ,weak) UILabel *kwhLabel; @property (nonatomic ,strong) NSMutableArray *btnArr; @end @implementation WatchHomeHeaderView - (instancetype)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { self.backgroundColor = KKClearColor; self.clipsToBounds = YES; [self subHeaderView]; } return self; } - (void)subHeaderView { [self addSubview:self.bgScrollView]; [self addSubview:self.pageControl]; // [self.pageControl mas_makeConstraints:^(MASConstraintMaker *make) { // make.centerX.equalTo(self); // make.top.equalTo(self.bgScrollView.mas_bottom).offset(5); // make.size.mas_equalTo(CGSizeMake(200, 10)); // }]; } //16 = - (UIScrollView *)bgScrollView { if (!_bgScrollView) { _bgScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(10, 15, ScreenWidth-20, 188)]; _bgScrollView.delegate = self; _bgScrollView.clipsToBounds = NO; // 是否按照页(默认以scrollview的宽度或高度为一页) _bgScrollView.pagingEnabled = YES; // 是否显示右侧边界显示的竖向指示条 _bgScrollView.showsVerticalScrollIndicator = NO; // 是否显示底侧边界显示的横向指示条 _bgScrollView.showsHorizontalScrollIndicator = NO; // 是否显示侧边界显示的指示条的颜色样式 _bgScrollView.indicatorStyle = UIScrollViewIndicatorStyleWhite; } return _bgScrollView; } -(EllipsePageControl *)pageControl { if (!_pageControl) { _pageControl = [[EllipsePageControl alloc] init]; _pageControl.backgroundColor = KKClearColor; _pageControl.otherColor = KKGrey153; _pageControl.currentColor = mainColor; } return _pageControl; } - (void)setWatchArr:(NSArray *)watchArr { _watchArr = watchArr; self.pageControl.hidden = watchArr.count > 1 ? NO : YES; self.bgScrollView.contentSize = CGSizeMake(self.bgScrollView.width *watchArr.count, 188); self.pageControl.numberOfPages = watchArr.count; CGFloat ww = watchArr.count*6+watchArr.count*8; [self.pageControl mas_remakeConstraints:^(MASConstraintMaker *make) { make.centerX.equalTo(self); make.top.equalTo(self.bgScrollView.mas_bottom).offset(5); make.size.mas_equalTo(CGSizeMake(ww, 10)); }]; for (UIView *views in self.bgScrollView.subviews) { [views removeFromSuperview]; } [watchArr enumerateObjectsUsingBlock:^(Device *dev, NSUInteger idx, BOOL * _Nonnull stop) { [self subWatchView:CGRectMake(self.bgScrollView.width*idx+5, 0,self.bgScrollView.width-10 , 188) devModel:dev]; }]; } - (void)subWatchView:(CGRect)fromt devModel:(Device *)dev { UIView *bgView = [UICommon ui_view:fromt backgroundColor:KKWhiteColorColor cornerRadius:12 borderWidth:0 borderColor:KKWhiteColorColor]; [self.bgScrollView addSubview:bgView]; UIImageView *iconImg = [UICommon ui_imageView:CGRectZero fileName:[dev.sex isEqualToString:@"girl"] ? @"icon_girl_head_portrait" : @"icon_boy_head_portrait"]; if (dev.avator && ![dev.avator containsString:@"/default.jpg"]) [iconImg sd_setImageWithURL:[NSURL URLWithString:dev.avator] placeholderImage:ImageName_(@"")]; iconImg.layer.cornerRadius = 12; iconImg.layer.masksToBounds = YES; self.iconImg = iconImg; [bgView addSubview:iconImg]; [iconImg mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(bgView).offset(15); make.left.equalTo(bgView).offset(15); make.size.mas_equalTo(CGSizeMake(64, 64)); }]; /// 昵称 UILabel *nameLabel = [UICommon ui_label:CGRectZero lines:0 align:NSTextAlignmentCenter font:FontBold_(20) textColor:KKTextBlackColor text:dev.name Radius:0]; self.nameLabel = nameLabel; [bgView addSubview:nameLabel]; [nameLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(iconImg.mas_right).offset(10); make.bottom.equalTo(iconImg.mas_centerY).inset(3); }]; UILabel *msgLabel = [UICommon ui_label:CGRectZero lines:0 align:NSTextAlignmentLeft font:Font_(14) textColor:KKGrey163 text:@"点击查看设备信息" Radius:0]; //self.msgLabel = msgLabel; [bgView addSubview:msgLabel]; [msgLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(nameLabel); make.right.equalTo(bgView.mas_right).inset(15); make.top.equalTo(iconImg.mas_centerY).offset(3); }]; /// 右箭头 UIImageView *rightImg = [UICommon ui_imageView:CGRectZero fileName:@"icon_enter_gray"]; [bgView addSubview:rightImg]; [rightImg mas_makeConstraints:^(MASConstraintMaker *make) { make.centerY.equalTo(iconImg); make.right.equalTo(bgView.mas_right).inset(15); make.size.mas_equalTo(rightImg.image.size); }]; /// 电量信息View UIView *kwhView = [[UIView alloc] init]; kwhView.backgroundColor = dev.battery.intValue <= 25 ? RGBA(255, 94, 16, .2) : RGBA(16, 199, 114, .2); kwhView.layer.cornerRadius = 10; kwhView.layer.masksToBounds = YES; [bgView addSubview:kwhView]; [kwhView mas_makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(nameLabel.mas_right).offset(5); make.centerY.equalTo(nameLabel); make.size.mas_equalTo(CGSizeMake(60, 20)); }]; NSString *imageName = @"icon_home_electricity_50"; if(dev.battery.intValue == 0) imageName = @"icon_home_electricity_0"; else if (dev.battery.intValue > 0 && dev.battery.intValue < 50) imageName = @"icon_home_electricity_25"; else if (dev.battery.intValue >= 50 && dev.battery.intValue < 75) imageName = @"icon_home_electricity_50"; else if (dev.battery.intValue >= 75 && dev.battery.intValue < 100) imageName = @"icon_home_electricity_75"; else if (dev.battery.intValue == 100) imageName = @"icon_home_electricity_100"; /// 电量 UIImageView *kwhImg = [UICommon ui_imageView:CGRectZero fileName:imageName]; self.kwhImg = kwhImg; [kwhView addSubview:kwhImg]; [kwhImg mas_makeConstraints:^(MASConstraintMaker *make) { make.centerY.equalTo(kwhView); make.left.equalTo(kwhView).offset(6); make.size.mas_equalTo(CGSizeMake(Adapted(16), Adapted(16))); }]; /// 电量 UILabel *kwhLabel = [UICommon ui_label:CGRectZero lines:0 align:NSTextAlignmentCenter font:Font_(10) textColor:dev.battery.intValue <= 25 ? RGB(255, 94, 16) : RGB(16, 199, 114) text:[NSString stringWithFormat:@"%@%%",dev.battery] Radius:0]; self.kwhLabel = kwhLabel; [kwhView addSubview:kwhLabel]; [kwhLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.centerY.equalTo(kwhView); make.left.equalTo(kwhImg.mas_right).offset(5); }]; UIButton *msgbtn = [UICommon ui_buttonSimple:CGRectZero font:Font_(0) normalColor:KKWhiteColorColor normalText:@"" click:^(id x) { UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil]; UIViewController *vc = [sb instantiateViewControllerWithIdentifier:@"EditDeviceViewController"]; [vc setValue:@(NO) forKey:@"isAdd"]; [[UICommon currentVC].navigationController pushViewController:vc animated:YES]; }]; [bgView addSubview:msgbtn]; [msgbtn mas_makeConstraints:^(MASConstraintMaker *make) { make.left.right.equalTo(bgView); make.top.equalTo(iconImg.mas_top); make.bottom.equalTo(iconImg.mas_bottom); }]; UIView *btnView = [UICommon ui_view:CGRectZero backgroundColor:KKClearColor cornerRadius:0 borderWidth:0 borderColor:KKWhiteColorColor]; self.btnView = btnView; [bgView addSubview:btnView]; [btnView mas_makeConstraints:^(MASConstraintMaker *make) { make.left.right.equalTo(bgView); make.top.equalTo(iconImg.mas_bottom).offset(28); make.height.mas_equalTo(60); }]; [self addMenuBtn:dev]; } //- (void)addMenuBtn:(NSArray *)titleArr ImageArr:(NSArray *)imageArr - (void)addMenuBtn:(Device *)dev { for (UIButton *views in self.btnView.subviews) { [views removeFromSuperview]; } self.btnArr = [NSMutableArray new]; NSMutableArray *titleArray = [NSMutableArray new]; NSMutableArray *imageArray = [NSMutableArray new]; if (dev.support_whiteList && dev.support_whiteList.intValue !=0) [titleArray addObject:@"管理员"]; else [titleArray addObject:@"电话本"]; [imageArray addObject:@"icon_phonebook"]; [titleArray addObject:@"手表短信"]; [imageArray addObject:@"icon_phonemessage"]; if(dev.support_step && dev.support_step.intValue == 1){ [titleArray addObject:@"计步"]; [imageArray addObject:@"icon_Pedometer"]; } if (dev.support_disturb && dev.support_disturb.intValue == 1) { [titleArray addObject:@"上课禁用"]; [imageArray addObject:@"icon_disabled"]; } // if(dev.support_video && dev.support_video.intValue != 0) // { // if (titleArray.count < 4) // { // [titleArray addObject:@"视频通话"]; // [imageArray addObject:@"icon_home_video_call"]; // } // } CGFloat masx = (ScreenWidth-30)/titleArray.count; for (int i = 0; i < titleArray.count; i++) { NSString *title = [titleArray objectAtIndex:i]; NSString *image = [imageArray objectAtIndex:i]; ZZBadgeButton *button = [self createButtonWithTitle:title image:image]; [button addTarget:self action:@selector(buttonClickAction:) forControlEvents:UIControlEventTouchUpInside]; button.tag = i; button.frame = CGRectMake(i*masx, 0, masx, masx); [self.btnView addSubview:button]; [self.btnArr addObject:button]; } } - (void)buttonClickAction:(UIButton *)btn { NSLog(@"%@",btn.currentTitle); NSString *title = btn.currentTitle; if([title isEqualToString:@"电话本"] || [title isEqualToString:@"管理员"]) { [UICommon PusXibViewController:[ContactsViewController new] :@"ContactsViewController"]; } else if([title isEqualToString:@"手表短信"]) { ShortMessageListViewController *vc = [[ShortMessageListViewController alloc] init]; [[UICommon currentVC].navigationController pushViewController:vc animated:YES]; } else if([title isEqualToString:@"计步"]) { NewStepVC * vc = [NewStepVC new]; vc.hidesBottomBarWhenPushed = YES; [self.getViewController.navigationController pushViewController:vc animated:YES]; } else if([title isEqualToString:@"上课禁用"]) { [UICommon PusXibViewController:[DisturbBanViewController new] :@"DisturbBanViewController"]; } // else if([title isEqualToString:@"视频通话"]) // { // [UICommon PusXibViewController:@"VideoCallViewController"]; // } } - (ZZBadgeButton *)createButtonWithTitle:(NSString *)title image:(NSString *)image { ZZBadgeButton *customButton = [[ZZBadgeButton alloc] init]; customButton.titleLabel.font = FontADA_(12); [customButton setTitle:title forState:UIControlStateNormal]; [customButton setTitleColor:KKBlack20 forState:UIControlStateNormal]; [customButton setImage:[UIImage imageNamed:image] forState:UIControlStateNormal]; return customButton; } //UIScrollView内视图移动停止移动时调用 - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView { CGFloat pageWidth = self.bgScrollView.width; NSInteger currentPage = floor((self.bgScrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1; NSLog(@"滚动到了第 %ld 页",currentPage); self.scrollViewTouch(currentPage); self.pageControl.currentPage = currentPage; } /// 滚动到指定页 - (void)scrollViewDidScrolls:(NSInteger)index { CGFloat imageW = self.bgScrollView.width; // 指定页面 CGPoint position = CGPointMake(index*imageW, 0); self.pageControl.currentPage = index; [self.bgScrollView setContentOffset:position animated:NO]; } /* // Only override drawRect: if you perform custom drawing. // An empty implementation adversely affects performance during animation. - (void)drawRect:(CGRect)rect { // Drawing code } */ @end