123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538 |
- //
- // 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 <objc/runtime.h>
- @interface HPSlideModuleView ()<UIScrollViewDelegate>
- @property(nonatomic,strong) UIScrollView *scrollView;
- @property(nonatomic,strong) UIView *backgroundView;
- @property(nonatomic,strong) NSMutableArray<UIButton *> *arrayButtons;
- @property(nonatomic,strong) NSMutableArray<UIView *> *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<UIButton *> *)arrayButtons
- {
- for (int i=0; i<arrayButtons.count; i++) {
-
- UIButton *button=arrayButtons[i];
- [button addTarget:self action:@selector(actionButton:) forControlEvents:UIControlEventTouchUpInside];
-
- }
- }
- -(void)hp_weak:(id)weakObj actionButton:(HPSLIDEMODULBUTTONBLOCK)actionBlock
- {
- _hpWeakObj=weakObj;
- _hpActionBlock=actionBlock;
- }
- -(void)updateLayoutWithIndex:(NSUInteger)index
- {
- UIButton *button=[HPSlideSegmentLogic isArrayWithNil:_arrayButtons index:index];
-
- [self actionButton:button];
-
- }
- -(void)actionButton:(UIButton *)button
- {
- if (_hpActionBlock!=nil && button!=_oldActionButton) {
- [self selectButton:button];
- _oldActionButton=button;
-
- [HPSlideSegmentLogic animationSlideView:self.slideModuleView
- slideModuleWidht:_slideModeuleWidth
- nowPoint:HPPointMake(button.x, button.width)
- readyButton:HPPointMake(button.x, button.width)
- movePercent:0];
-
-
- _hpActionBlock(_hpWeakObj,button.index);
-
-
- [HPSlideSegmentLogic slideModuleAlignCenter:self.scrollView
- slideModuleWithX:self.slideModuleView.x];
- }
- }
- -(void)slideBetweenWithButton:(UIButton *)button
- {
- if (button.x-self.width/2>0) {
- 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<UIButton *> *)arrayButtons
- delegateContent:(id)delegate
- layoutView:(HPSlideModuleView *)moduleView
- moduleHeight:(CGFloat)height
- {
- for (int i=0; i<count; i++) {
-
- UIButton *module=[self cacheButton:arrayButtons
- getCacheButton:i
- delegateContent:delegate
- moduleHeight:height];
- // UIView *view = nil;
- // if (_spaceLine == YES) {
- // view = [self spaceLineWithIndex:i];
- // }
- //
- // view.frame = CGRectMake(module.width+module.x + 3, view.y, view.width, view.height);
- if (count-1==i) {
- CGFloat widthAddY=module.width+module.x;
-
- moduleView.scrollView.contentSize =CGSizeMake(widthAddY, 0);
-
- }
-
- if (_backgroundSelectColor!=nil && i==0) {
- module.backgroundColor=_backgroundSelectColor;
- }
- else if(_backgroundDefaultColor!=nil)
- {
- module.backgroundColor=_backgroundDefaultColor;
- }
-
- moduleView.backgroundView.frame=CGRectMake(0, 0, moduleView.scrollView.contentSize.width,moduleView.scrollView.height);
-
- [moduleView.backgroundView addSubview:module];
- // [moduleView.backgroundView addSubview:view];
- module.index=i;
- module=nil;
-
- }
- }
- -(UIButton *)cacheButton:(NSMutableArray<UIButton *> *)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<UIButton *> *)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<SlideModuleViewDelegate>)delegate
- {
-
- _delegate=delegate;
-
- for (int i=0; i<self.arrayButtons.count; i++) {
-
- UIButton *button=_arrayButtons[i];
- [button removeFromSuperview];
-
- }
- [self.arrayButtons removeAllObjects];
-
- [self updateLayout];
- }
- -(void)setHiddenModule:(BOOL)hiddenModule
- {
- _hiddenModule=hiddenModule;
-
- if (hiddenModule==YES) {
-
- [_slideModuleView removeFromSuperview];
- _slideModuleView=nil;
- [self updateLayout];
- }
- }
- -(void)setEdgeInsets:(UIEdgeInsets)edgeInsets
- {
- _edgeInsets=edgeInsets;
-
- [self updateLayout];
- }
- -(void)setMinWidth:(CGFloat)minWidth
- {
- _minWidth=minWidth;
-
- [self updateLayout];
-
- }
- -(void)setType:(AutoSizeType)type
- {
- _type=type;
-
- [self updateLayout];
-
- }
- -(void)setShowCount:(NSUInteger)showCount
- {
- _showCount=showCount;
-
- [self updateLayout];
-
- }
- -(UIScrollView *)scrollView
- {
- if (_scrollView==nil) {
- _scrollView=[[UIScrollView alloc] init];
- _scrollView.backgroundColor=[UIColor clearColor];
- _scrollView.delegate=self;
- _scrollView.showsVerticalScrollIndicator=NO;
- _scrollView.showsHorizontalScrollIndicator=NO;
- }
- return _scrollView;
- }
- -(void)setSlideModuleView:(UIView *)slideModuleView
- {
- if (self.hiddenModule==YES) {
-
- [_slideModuleView removeFromSuperview];
- _slideModuleView=nil;
- _slideModuleView=slideModuleView;
-
- return;
- }
-
- if (slideModuleView==nil) {
-
- _slideModuleView=[[UIView alloc] init];
- self.slideModuleColor=_slideModuleColor;
- _slideModuleView.backgroundColor=self.slideModuleColor;
- _slideModuleView.frame=CGRectMake(0, 0, 10, 3);
-
- }
- else
- {
- self.slideModuleColor=slideModuleView.backgroundColor;
- [self slideModuleLayout];
- }
- }
- -(void)setSlideModuleColor:(UIColor *)slideModuleColor
- {
- if (slideModuleColor==nil ) {
- _slideModuleColor=[UIColor redColor];
- }
- else
- {
- _slideModuleColor=slideModuleColor;
- [self slideModuleLayout];
- }
- }
- -(UIView *)backgroundView
- {
- if (_backgroundView==nil) {
- _backgroundView=[[UIView alloc] init];
- _backgroundView.backgroundColor=[UIColor clearColor];
- }
- return _backgroundView;
- }
- -(NSMutableArray<UIView *> *)arraySpaceLines{
- if (_arraySpaceLines==nil) {
- _arraySpaceLines = [NSMutableArray array];
- }
- return _arraySpaceLines;
- }
- -(NSMutableArray<UIButton *> *)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
|