// // HPSlideView.m // HPSlideSegmentView // // Created by 何鹏 on 17/6/12. // Copyright © 2017年 何鹏. All rights reserved. // #import "HPSlideModuleView.h" #import "HPSlideSegmentLogic.h" #import "UIView+HPSlideSegmentRect.h" #import @interface HPSlideModuleView () @property(nonatomic,strong) UIScrollView *scrollView; @property(nonatomic,strong) UIView *backgroundView; @property(nonatomic,strong) NSMutableArray *arrayButtons; @property(nonatomic,strong) NSMutableArray *arraySpaceLines; @property(nonatomic,weak) id hpWeakObj; @property(nonatomic,strong) HPSLIDEMODULBUTTONBLOCK hpActionBlock; @property(nonatomic,strong) UIButton *oldActionButton; @end @implementation HPSlideModuleView -(instancetype)initWithFrame:(CGRect)frame { if (self=[super initWithFrame:frame]) { self.hiddenModule=NO; [self layoutSlideModule]; } return self; } -(void)layoutSubviews { [super layoutSubviews]; [self updateLayout]; } -(void)updateLayout { self.scrollView.contentSize=CGSizeMake(0, 0); [self layoutSlideModule]; } -(void)layoutSlideModule { self.scrollView.frame=CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height); [self addSubview:_scrollView]; [_scrollView addSubview:self.backgroundView]; [self scrollViewLayoutWithCount:self.showCount cacheArray:self.arrayButtons delegateContent:_delegate layoutView:self moduleHeight:self.bounds.size.height]; self.backgroundColor=[UIColor whiteColor]; self.slideModuleColor=_slideModuleColor; self.slideModuleView=_slideModuleView; [self buttonAction:_arrayButtons]; [self slideModuleLayout]; } -(void)slideModuleLayout { UIButton *defaultButton=nil; if (self.arrayButtons.count!=0) { defaultButton=self.arrayButtons[0]; } self.slideModuleView.backgroundColor=self.slideModuleColor; self.slideModuleView.frame=[HPSlideSegmentLogic slideModuleWithView:self.slideModuleView.bounds.size.height slideViewHeight:self.bounds.size.height defauleWidth:defaultButton.bounds.size.width buttonWithX:defaultButton.x]; [HPSlideSegmentLogic animationSlideView:self.slideModuleView slideModuleWidht:_slideModeuleWidth nowPoint:HPPointMake(defaultButton.x, defaultButton.width) readyButton:HPPointMake(defaultButton.x, defaultButton.width) movePercent:0]; [self.backgroundView insertSubview:_slideModuleView atIndex:0]; } -(void)buttonAction:(NSArray *)arrayButtons { for (int i=0; i0) { if (_scrollView.contentOffset.x+self.width<_scrollView.contentSize.width) { _scrollView.contentOffset=CGPointMake(button.x-self.width/2, 0); } } else{ _scrollView.contentOffset=CGPointMake(0, 0); } } -(void)slideWithNowIndex:(NSUInteger)nowIndex readyIndex:(NSUInteger)readyIndex movePercent:(CGFloat)movePercent { UIButton *nowButton=_arrayButtons[nowIndex]; UIButton *readyButton=_arrayButtons[readyIndex]; [HPSlideSegmentLogic animationSlideView:self.slideModuleView slideModuleWidht:_slideModeuleWidth nowPoint:HPPointMake(nowButton.x, nowButton.width) readyButton:HPPointMake(readyButton.x, readyButton.width) movePercent:movePercent]; [HPSlideSegmentLogic slideModuleAlignCenter:self.scrollView slideModuleWithX:self.slideModuleView.x]; [self selectButton:readyButton]; } -(void)scrollViewLayoutWithCount:(NSUInteger )count cacheArray:(NSMutableArray *)arrayButtons delegateContent:(id)delegate layoutView:(HPSlideModuleView *)moduleView moduleHeight:(CGFloat)height { for (int i=0; i *)arrayButtons getCacheButton:(NSUInteger)index delegateContent:(id)delegate moduleHeight:(CGFloat)height { UIButton *currenButton=[HPSlideSegmentLogic isArrayWithNil:arrayButtons index:index]; UIButton *oldButton=[HPSlideSegmentLogic arrayCount:arrayButtons index:index-1]; if (currenButton==nil) { return [self creatModule:index casheArray:arrayButtons delegateContent:delegate moduleHeight:height]; }else { [self buttonLayoutWithNew:currenButton currentIndex:index oldButton:oldButton]; } return [self buttonLayoutWithOldButton:oldButton newButton:currenButton moduleHeight:height buttonContent:currenButton.titleLabel.text]; } -(UIButton *)creatModule:(NSUInteger)index casheArray:(NSMutableArray *)arrayButtons delegateContent:(id)delegate moduleHeight:(CGFloat)height { UIButton *module=nil; if ([delegate respondsToSelector:@selector(hp_slideWithIndex:)]) { module=[delegate hp_slideWithIndex:index]; } else { module=[[UIButton alloc] init]; } UIButton *oldButton=[HPSlideSegmentLogic arrayCount:arrayButtons index:index-1]; [self buttonLayoutWithNew:module currentIndex:index oldButton:oldButton]; [arrayButtons addObject:module]; return module; } -(UIView *)spaceLineWithIndex:(NSUInteger)index{ if (index == _arrayButtons.count-1) { return nil; } UIView *lineView = nil; if (_arraySpaceLines.count > index) { lineView = _arrayButtons[index]; return lineView; } else{ lineView = [[UIView alloc] init]; lineView.backgroundColor = [UIColor blueColor]; lineView.frame = CGRectMake(0, 3, 1, self.height - 6); [_arraySpaceLines addObject:lineView]; } return lineView; } -(void)buttonLayoutWithNew:(UIButton *)module currentIndex:(NSUInteger)index oldButton:(UIButton *)oldButton { NSString *content=@""; if ([self.delegate respondsToSelector:@selector(hp_slideContentWithIndex:)]) { content=[self.delegate hp_slideContentWithIndex:index]; } [module setTitle:content forState:UIControlStateNormal]; [module setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; module.titleLabel.font=[UIFont systemFontOfSize:14]; module = [self buttonLayoutWithOldButton:oldButton newButton:module moduleHeight:self.bounds.size.height buttonContent:content]; content=nil; } -(UIButton *)buttonLayoutWithOldButton:(UIButton *)oldButton newButton:(UIButton *)module moduleHeight:(CGFloat)height buttonContent:(NSString *)content { module.frame=CGRectMake(0, 0, 0, 0); module.frame=[HPSlideSegmentLogic oldButtonPoint:HPPointMake(oldButton.frame.origin.x, oldButton.bounds.size.width) slideViewHeight:height isModule:self.hiddenModule content:content fontSize:module.titleLabel.font.pointSize edgeInsets:self.edgeInsets minWidth:self.minWidth autoType:self.type]; return module; } -(void)selectButton:(UIButton *)selectButton { for (int i=0; i<_arrayButtons.count; i++) { UIButton *button=_arrayButtons[i]; if (button==selectButton) { if (_backgroundSelectColor!=nil) { button.backgroundColor=_backgroundSelectColor; } button.selected=YES; } else { if (_backgroundDefaultColor!=nil) { button.backgroundColor=_backgroundDefaultColor; } button.selected=NO; } } } #pragma mark - 懒加载 -(void)setBackgroundDefaultColor:(UIColor *)backgroundDefaultColor { _backgroundDefaultColor=backgroundDefaultColor; [self updateLayout]; } -(void)setBackgroundSelectColor:(UIColor *)backgroundSelectColor { _backgroundSelectColor=backgroundSelectColor; [self updateLayout]; } -(void)setDelegate:(id)delegate { _delegate=delegate; for (int i=0; i *)arraySpaceLines{ if (_arraySpaceLines==nil) { _arraySpaceLines = [NSMutableArray array]; } return _arraySpaceLines; } -(NSMutableArray *)arrayButtons { if (_arrayButtons==nil) { _arrayButtons=[NSMutableArray array]; } return _arrayButtons; } @end #pragma mark - HPSlideSegment static void *indexKey = &indexKey; @implementation UIButton (HPSlideSegment) -(void)setIndex:(NSUInteger)index { objc_setAssociatedObject(self, &indexKey, @(index), OBJC_ASSOCIATION_RETAIN_NONATOMIC); } -(NSUInteger)index { return [objc_getAssociatedObject(self, &indexKey) integerValue]; } @end