| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336 |
- //
- // MOScrollMenuView.m
- // powerone
- //
- // Created by SuperC on 2019/8/8.
- // Copyright © 2019 onecloud.ltd. All rights reserved.
- //
- #define TitleFont 12.0
- #define ImageViewWidthAndHeight 18.0
- #define ImageViewX 10.0
- #import "MOScrollMenuView.h"
- #import "UIView+Extension.h"
- #import "MOCountryList.h"
- #import "MOThemeManager.h"
- @implementation MOCountryBtn
- - (void)layoutSubviews{
- [super layoutSubviews];
-
- if(self.imageView.image != nil)
- {
- self.imageView.width = ImageViewWidthAndHeight;
- self.imageView.height = ImageViewWidthAndHeight;
- self.imageView.x = ImageViewX;
- self.imageView.centerY = self.centerY;
-
- UIFont *customFont = [UIFont fontWithName:kNormalContentFontStr size:12.0];
- if(!customFont){
- customFont = [MOTextTools MODisplayFontWithSize:12.0 bold:YES itatic:YES weight:UIFontWeightMedium];
- }
-
- CGFloat width = [MOTools getWidthWithString:self.titleLabel.text font:customFont] + 2.0;
- self.titleLabel.frame = CGRectMake(CGRectGetMaxX(self.imageView.frame) + 2, 0, width, (TitleFont + 2.0));
- self.titleLabel.centerY = self.imageView.centerY;
- self.titleLabel.textAlignment = NSTextAlignmentCenter;
- }
- }
- @end
- @implementation MOScrollMenuView
- {
- /** 用于记录最后创建的控件 */
- UIView *_lastView;
- /** 按钮下面的横线 */
- UIView *_lineView;
- }
- #pragma mark - 重写构造方法
- /** 重写构造方法 */
- - (instancetype)initWithFrame:(CGRect)frame
- {
- if (self = [super initWithFrame:frame])
- {
- self.showsHorizontalScrollIndicator = NO;
- self.contentInset = UIEdgeInsetsMake(0, 0, 0, 10);
- _currentButtonIndex = 0; // 默认当前选择的按钮是第一个
- }
- return self;
- }
- - (void)layoutSubviews {
- [super layoutSubviews];
-
- if (!self.layer.mask) {
- CAGradientLayer *maskLayer = [CAGradientLayer layer];
-
- // 设置水平渐变
- maskLayer.startPoint = CGPointMake(0.0, 0.5);
- maskLayer.endPoint = CGPointMake(1.0, 0.5);
-
- maskLayer.locations = @[@0.0, @0.1, @0.9, @1.0];
- maskLayer.bounds = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
- maskLayer.anchorPoint = CGPointZero;
-
- self.layer.mask = maskLayer;
- }
-
- [self updateGradientMask];
- }
- - (void)updateGradientMask {
- CGColorRef outerColor = [UIColor colorWithWhite:1.0 alpha:0.0].CGColor;
- CGColorRef innerColor = [UIColor colorWithWhite:1.0 alpha:1.0].CGColor;
-
- if (self.contentOffset.x <= 0) {
- // 最左边
- ((CAGradientLayer *)self.layer.mask).colors = @[
- (__bridge id)innerColor,
- (__bridge id)innerColor,
- (__bridge id)innerColor,
- (__bridge id)outerColor
- ];
- } else if (self.contentOffset.x + self.frame.size.width >= self.contentSize.width) {
- // 最右边
- ((CAGradientLayer *)self.layer.mask).colors = @[
- (__bridge id)outerColor,
- (__bridge id)innerColor,
- (__bridge id)innerColor,
- (__bridge id)innerColor
- ];
- } else {
- // 中间
- ((CAGradientLayer *)self.layer.mask).colors = @[
- (__bridge id)outerColor,
- (__bridge id)innerColor,
- (__bridge id)innerColor,
- (__bridge id)outerColor
- ];
- }
-
- [CATransaction begin];
- [CATransaction setDisableActions:YES];
- self.layer.mask.position = CGPointMake(self.contentOffset.x, 0); // 水平方向偏移
- [CATransaction commit];
- }
- - (void)setContentOffset:(CGPoint)contentOffset {
- [super setContentOffset:contentOffset];
- [self updateGradientMask];
- }
- - (void)reloadData {
- [self setTitleArray:_titleArray];
- WEAKSELF
- dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
- [weakSelf setCurrentButtonIndex:weakSelf.currentButtonIndex];
- });
- }
- #pragma mark - 赋值标题数组
- /** 赋值标题数组 */
- - (void)setTitleArray:(NSArray *)titleArray
- {
- _titleArray = titleArray;
-
- // 先将所有子控件移除
- for (UIView *subView in self.subviews)
- {
- [subView removeFromSuperview];
- }
-
- // 将lastView置空
- _lastView = nil;
-
- // 遍历标题数组
- [_titleArray enumerateObjectsUsingBlock:^(MOCountryList * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop)
- {
- __block MOCountryBtn *menuButton = [MOCountryBtn buttonWithType:UIButtonTypeCustom];
- [self addSubview:menuButton];
- if (_lastView)
- {
- menuButton.frame = CGRectMake(_lastView.maxX + 10, 0, 100, self.height);
- }
- else
- {
- menuButton.frame = CGRectMake(0, 0, 100, self.height);
- }
-
- menuButton.layer.masksToBounds = YES;
- menuButton.layer.cornerRadius = self.height / 2.0;
- menuButton.tag = 100 + idx;
-
- if(obj.icon.length > 0){
- [menuButton sd_setImageWithURL:[NSURL URLWithString:obj.icon] forState:UIControlStateNormal];
- [menuButton sd_setImageWithURL:[NSURL URLWithString:obj.icon] forState:UIControlStateSelected];
- }
- else{
- [menuButton sd_setImageWithURL:[NSURL URLWithString:@""] forState:UIControlStateNormal];
- [menuButton sd_setImageWithURL:[NSURL URLWithString:@""] forState:UIControlStateSelected];
- }
- menuButton.imageView.contentMode = UIViewContentModeScaleAspectFit;
-
- UIFont *customFont = [UIFont fontWithName:kNormalContentFontStr size:12.0];
- if(!customFont){
- customFont = [MOTextTools MODisplayFontWithSize:12.0 bold:YES itatic:YES weight:UIFontWeightMedium];
- }
-
- [menuButton.titleLabel setFont:customFont];
- [menuButton setTitleColor:[MOTools colorWithHexString:@"#26252A" alpha:1.0] forState:UIControlStateNormal];
- if ([MOThemeManager shareManager].canUseTheme && [MOThemeManager shareManager].themeModel.countryColor.length > 0) {
- [menuButton setTitleColor:[MOTools colorWithHexString:[MOThemeManager shareManager].themeModel.countryColor] forState:UIControlStateNormal];
- }
- [menuButton setTitleColor:[MOTools colorWithHexString:@"#26252A" alpha:1.0] forState:UIControlStateSelected];
- if ([MOThemeManager shareManager].canUseTheme && [MOThemeManager shareManager].themeModel.countryColorOn.length > 0) {
- [menuButton setTitleColor:[MOTools colorWithHexString:[MOThemeManager shareManager].themeModel.countryColorOn] forState:UIControlStateSelected];
- }
- [menuButton setTitle:obj.name forState:UIControlStateNormal];
- [menuButton setTitle:obj.name forState:UIControlStateSelected];
- menuButton.backgroundColor = [MOTools colorWithHexString:@"#FFFFFF" alpha:1.0];
- if ([MOThemeManager shareManager].canUseTheme && [MOThemeManager shareManager].themeModel.countryBgColor.length > 0) {
- menuButton.backgroundColor = [MOTools colorWithHexString:[MOThemeManager shareManager].themeModel.countryBgColor];
- }
- [menuButton addTarget:self action:@selector(menuButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
-
- // 宽度自适应
- [menuButton sizeToFit];
- if(obj.icon.length > 0){
- CGFloat labebTextWidth = [MOTools getWidthWithString:obj.name font:customFont] + 2.0;
- CGFloat labelWidth = ImageViewX * 2.0 + ImageViewWidthAndHeight + labebTextWidth;
- menuButton.width = labelWidth;
- }
- else{
- menuButton.width = menuButton.width + 20.0;
- }
-
- menuButton.height = self.height;
-
- // 这句不能少,不然初始化时button的label的宽度为0
- [menuButton layoutIfNeeded];
-
- // 默认第一个按钮时选中状态
- if (idx == 0)
- {
- menuButton.selected = YES;
- menuButton.backgroundColor = [MOTools colorWithHexString:@"#16E0FE" alpha:1.0];
- if ([MOThemeManager shareManager].canUseTheme && [MOThemeManager shareManager].themeModel.countryBgColorOn.length > 0) {
- menuButton.backgroundColor = [MOTools colorWithHexString:[MOThemeManager shareManager].themeModel.countryBgColorOn];
- }
- }
-
- _lastView = menuButton;
- }];
-
- self.contentSize = CGSizeMake(CGRectGetMaxX(_lastView.frame), CGRectGetHeight(self.frame));
- }
- #pragma mark - 菜单按钮点击
- /** 菜单按钮点击 */
- - (void)menuButtonClicked:(MOCountryBtn *)sender
- {
- // 改变按钮的选中状态
- // for (MOCountryBtn *button in self.subviews)
- // {
- // if ([button isKindOfClass:[MOCountryBtn class]])
- // {
- // button.selected = NO;
- // button.backgroundColor = [MOTools colorWithHexString:@"#F5F1F6" alpha:1.0];
- // button.layer.borderColor = [MOTools colorWithHexString:@"#F5F1F6" alpha:1.0].CGColor;
- // }
- // }
- // sender.selected = YES;
- // sender.backgroundColor = [MOTools colorWithHexString:@"#F0DAFD" alpha:1.0];
- // sender.layer.borderColor = [MOTools colorWithHexString:@"#E72DD3" alpha:1.0].CGColor;
- //
- // // 将所点击的button移到中间
- // if (_lastView.maxX > self.width)
- // {
- // if (sender.x >= self.width / 2 && sender.centerX <= self.contentSize.width - self.width/2)
- // {
- // [UIView animateWithDuration:0.3 animations:^
- // {
- // self.contentOffset = CGPointMake(sender.centerX - self.width / 2, 0);
- // }];
- // }
- // else if (sender.frame.origin.x < self.width / 2)
- // {
- // [UIView animateWithDuration:0.3 animations:^
- // {
- // self.contentOffset = CGPointMake(0, 0);
- // }];
- // }
- // else
- // {
- // [UIView animateWithDuration:0.3 animations:^
- // {
- // self.contentOffset = CGPointMake(self.contentSize.width - self.width, 0);
- // }];
- // }
- // }
-
- self.currentButtonIndex = sender.tag - 100;
-
- if (self.clickBtnBlock)
- {
- self.clickBtnBlock(sender.tag - 100);
- }
- }
- #pragma mark - 赋值当前选择的按钮
- /** 赋值当前选择的按钮 */
- - (void)setCurrentButtonIndex:(NSInteger)currentButtonIndex
- {
- _currentButtonIndex = currentButtonIndex;
-
- // 改变按钮的选中状态
- MOCountryBtn *currentButton = [self viewWithTag:(100 + currentButtonIndex)];
- for (MOCountryBtn *button in self.subviews)
- {
- if ([button isKindOfClass:[MOCountryBtn class]])
- {
- button.selected = NO;
- button.backgroundColor = [MOTools colorWithHexString:@"#FFFFFF" alpha:1.0];
- if ([MOThemeManager shareManager].canUseTheme && [MOThemeManager shareManager].themeModel.countryBgColor.length > 0) {
- button.backgroundColor = [MOTools colorWithHexString:[MOThemeManager shareManager].themeModel.countryBgColor];
- }
- }
- }
- currentButton.selected = YES;
- currentButton.backgroundColor = [MOTools colorWithHexString:@"#16E0FE" alpha:1.0];
- if ([MOThemeManager shareManager].canUseTheme && [MOThemeManager shareManager].themeModel.countryBgColorOn.length > 0) {
- currentButton.backgroundColor = [MOTools colorWithHexString:[MOThemeManager shareManager].themeModel.countryBgColorOn];
- }
-
- // 将所点击的button移到中间
- if (_lastView.maxX > self.width)
- {
- if (currentButton.x >= self.width / 2 && currentButton.centerX <= self.contentSize.width - self.width/2)
- {
- [UIView animateWithDuration:0.3 animations:^
- {
- self.contentOffset = CGPointMake(currentButton.centerX - self.width / 2, 0);
- }];
- }
- else if (currentButton.x < self.width / 2)
- {
- [UIView animateWithDuration:0.3 animations:^
- {
- self.contentOffset = CGPointMake(0, 0);
- }];
- }
- else
- {
- [UIView animateWithDuration:0.3 animations:^
- {
- self.contentOffset = CGPointMake(self.contentSize.width - self.width + self.contentInset.right, 0);
- }];
- }
- }
- }
- @end
|