123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514 |
- //
- // HPSlideLogic.m
- // HPSlideSegmentView
- //
- // Created by 何鹏 on 17/6/14.
- // Copyright © 2017年 何鹏. All rights reserved.
- //
- #import "HPSlideSegmentLogic.h"
- #import "UIView+HPSlideSegmentRect.h"
- @implementation HPSlideSegmentLogic
- +(CGRect)oldButtonPoint:(HPPoint)oldpoint
- slideViewHeight:(CGFloat)slideViewHeight
- isModule:(BOOL)isModule
- content:(NSString *)text
- fontSize:(CGFloat)fontSize
- edgeInsets:(UIEdgeInsets)edgeInsets
- minWidth:(CGFloat)minWidth
- autoType:(AutoSizeType)type;
- {
- CGFloat x=oldpoint.x+oldpoint.width;
- CGFloat y=0;
- CGFloat width=[self widthForText:slideViewHeight content:text fontSize:fontSize];
- CGFloat height=slideViewHeight-(isModule==NO?3:0);
-
- if (oldpoint.width!=0) {
- x=x+edgeInsets.left+edgeInsets.right;
- }
- else
- {
- x=x+edgeInsets.left;
- }
- y=y+edgeInsets.top;
- height=height-edgeInsets.bottom-edgeInsets.top;
-
- switch (type) {
- case ENUM_HP_AUTOSIZE:
- {
-
- }
- break;
- case ENUM_HP_DEFINESIZE:
- {
- width=minWidth;
- }
- break;
- case ENUM_HP_AUTOMINSIZE:
- {
- width=width>minWidth?width:minWidth;
- }
- break;
- default:
- break;
- }
-
- return CGRectMake(x, y, width, height);
- }
- +(id)arrayCount:(NSArray *)arrays index:(NSInteger)index
- {
- if (arrays.count==0) {
- return nil;
- }
- else if (index<0)
- {
- return arrays[0];
- }
-
- NSUInteger indexType=index;
-
- if (indexType>arrays.count-1) {
- return arrays[arrays.count-1];
- }
-
- return arrays[index];
- }
- +(id)isArrayWithNil:(NSArray *)arrays index:(NSInteger)index
- {
- if (arrays.count==0) {
- return nil;
- }
- else if (index>arrays.count-1) {
- return nil;
- }
-
- return arrays[index];
- }
- //单独计算文本的高度
- + (CGFloat)widthForText:(CGFloat )height content:(NSString *)text fontSize:(CGFloat)fontSize
- {
- //设置计算文本时字体的大小,以什么标准来计算
- NSDictionary *attrbute = @{NSFontAttributeName:[UIFont systemFontOfSize:fontSize]};
- return [text boundingRectWithSize:CGSizeMake(1000, height) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:attrbute context:nil].size.width;
- }
- +(CGRect)slideModuleWithView:(CGFloat)slideModuleHeight
- slideViewHeight:(CGFloat)slideViewHeight
- defauleWidth:(CGFloat)width
- buttonWithX:(CGFloat)buttonX
- {
-
- CGFloat y=slideViewHeight-slideModuleHeight;
- CGFloat x=buttonX+((width/2)-width/2);
- if (y<0) {
- return CGRectMake(x, 0, width, slideViewHeight);
- }
-
- return CGRectMake(x, y, width, slideModuleHeight);
-
- }
- +(NSUInteger)arraCount:(NSUInteger)arrayCount index:(NSInteger)index
- {
- if (index<0) {
- return 0;
- }
-
- if (index>arrayCount-1) {
- return arrayCount-1;
- }
-
- return index;
- }
- //---
- //+(CGFloat)scrollViewWidth:(NSUInteger)width dataArrayCount:(NSUInteger)dataArrayCount
- //{
- //
- // if (dataArrayCount==1 || dataArrayCount==0) {
- // return width;
- // }
- // else if(dataArrayCount==2)
- // {
- // return width * 2;
- // }
- //
- // return width * 3;
- //}
- +(CGFloat)scrollViewWidth:(NSUInteger)width dataArrayCount:(NSUInteger)dataArrayCount
- {
-
- return width * dataArrayCount;
- }
- +(CGFloat)scrollViewWidth:(NSUInteger)width
- arrayDataCount:(NSUInteger)dataArrayCount
- currentIndex:(NSUInteger)currentIndex
- {
- if (currentIndex==0) {
- return 0;
- }
- else if (currentIndex==dataArrayCount-1)
- {
- return width*(dataArrayCount-1);
- }
-
- return currentIndex * width;
- }
- +(void)slideSuperView:(CGFloat)slideViewWidth
- scrollView:(UIScrollView *)scrollView
- currentIndex:(NSUInteger)currentIndex
- startOffset:(CGPoint )startOffset
- dataArray:(NSUInteger )arrayDataCount
- changeIndex:(CHANGEINDEXBLOCK)changeBlock
- {
- if (arrayDataCount<3) {
- return;
- }
- if (changeBlock==nil) {
- return;
- }
-
-
- [self currentIndex:currentIndex
- arrayCount:arrayDataCount
- scrollView:scrollView
- slideSuperView:slideViewWidth
- changeIndex:changeBlock];
-
- }
- +(void)scrollViewWithStartPoint:(CGPoint)startOffset
- moveOffset:(CGPoint)moveOffset
- slideModuleWidht:(CGFloat)slideModuleWith
- currentIndex:(NSUInteger )currentIndex
- dataArray:(NSUInteger)arrayCount
- startPointBlock:(ChangeStartPoint)startPointBlock
- {
-
- CGPoint hp_startOffset=startOffset;
- CGPoint hp_endOffset=CGPointMake(0, 0);
-
- CGFloat changeOffset=startOffset.x-moveOffset.x;
-
- if (changeOffset>0 || (changeOffset==0 && moveOffset.x==0)) {
- if (currentIndex==arrayCount-1 && arrayCount>=3) {
-
- hp_startOffset=CGPointMake(2*slideModuleWith, moveOffset.y);
- hp_endOffset=CGPointMake(slideModuleWith, moveOffset.y);
-
-
- }
- else
- {
- hp_startOffset=CGPointMake(slideModuleWith, moveOffset.y);
- hp_endOffset=CGPointMake(0, moveOffset.y);
- }
-
-
- }
- else if (changeOffset<0 || (changeOffset==0 && moveOffset.x==2*slideModuleWith))
- {
- if(currentIndex == 0)
- {
- hp_startOffset=CGPointMake(0, moveOffset.y);
- hp_endOffset=CGPointMake(slideModuleWith, moveOffset.y);
- }
- else
- {
- hp_startOffset=CGPointMake(slideModuleWith, moveOffset.y);
-
- hp_endOffset=CGPointMake(2*slideModuleWith, moveOffset.y);
- }
-
- }
-
- if (startPointBlock!=nil) {
-
- startPointBlock(hp_startOffset,hp_endOffset);
-
- }
-
- }
- +(void)scrollView:(UIScrollView *)scrollView
- currentIndex:(NSUInteger)currentIndex
- startOffset:(CGPoint )startOffset
- dataArray:(NSUInteger )arrayDataCount
- boardBlock:(BoardBlock)boardBlock
- moduleBlock:(ModuleAnimationBlock)moduleAnimationBlock{
-
- CGPoint off=[scrollView contentOffset];
-
- if (off.x > startOffset.x) {
- //right
-
- if (moduleAnimationBlock!=nil) {
-
- CGFloat percent=fabs(off.x-startOffset.x)/scrollView.width;
- NSUInteger exchangeIndex=[self arraCount:arrayDataCount index:currentIndex+1];
-
- if (exchangeIndex==currentIndex) {
- return;
- }
-
- moduleAnimationBlock(currentIndex,exchangeIndex,percent);
- }
-
-
- }else if (off.x < startOffset.x){
- //left
-
- if (moduleAnimationBlock!=nil) {
- CGFloat percent=(startOffset.x-off.x)/scrollView.width;
- NSUInteger exchangeIndex=[self arraCount:arrayDataCount index:currentIndex+1];
- if (exchangeIndex==currentIndex) {
- return;
- }
-
- moduleAnimationBlock(exchangeIndex,currentIndex,percent);
- }
-
- }
-
- if (boardBlock!=nil) {
- boardBlock();
- }
-
- }
- +(void)currentIndex:(NSUInteger)currentIndex
- arrayCount:(NSUInteger)arrayCount
- scrollView:(UIScrollView *)scrollView
- slideSuperView:(CGFloat)slideViewWidth
- changeIndex:(CHANGEINDEXBLOCK)changeBlock
- {
-
- if (changeBlock==nil) {
- return;
- }
-
- if (arrayCount<3) {
-
- [self minIndexJudege:currentIndex
- arrayCount:arrayCount
- scrollView:scrollView
- changeIndex:changeBlock
- slideSuperView:slideViewWidth];
-
- return;
- }
- if (currentIndex==0) {
-
- HPNumber numberLeft=HPNumberMake(currentIndex, nil);
- HPNumber numberCentre=HPNumberMake(currentIndex+1, nil);
- HPNumber numberRight=HPNumberMake(currentIndex+2, nil);
-
-
- changeBlock(numberLeft,numberCentre,numberRight,scrollView.contentOffset);
- return;
- }
- else if (currentIndex==arrayCount-1)
- {
- NSUInteger count=arrayCount-1;
-
-
- HPNumber numberLeft=HPNumberMake(count-2, nil);
- HPNumber numberCentre=HPNumberMake(count-1, nil);
- HPNumber numberRight=HPNumberMake(count, nil);
-
- changeBlock(numberLeft,numberCentre,numberRight,scrollView.contentOffset);
- return;
- }
-
- HPNumber numberLeft=HPNumberMake(currentIndex-1, nil);
- HPNumber numberCentre=HPNumberMake(currentIndex, nil);
- HPNumber numberRight=HPNumberMake(currentIndex+1, nil);
-
- changeBlock(numberLeft,numberCentre,numberRight,scrollView.contentOffset);
- }
- +(void)minIndexJudege:(NSUInteger )currentIndex
- arrayCount:(NSUInteger)arrayCount
- scrollView:(UIScrollView *)scrollView
- changeIndex:(CHANGEINDEXBLOCK)changeBlock
- slideSuperView:(CGFloat)slideViewWidth
- {
-
- if (currentIndex<1) {
- scrollView.contentOffset=CGPointMake(0, 0);
- }
- else
- {
- scrollView.contentOffset=CGPointMake(slideViewWidth, 0);
- }
-
- if (arrayCount==0) {
- return;
- }
- else if (arrayCount==1)
- {
- HPNumber numberLeft=HPNumberMake(0, nil);
- HPNumber numberCentre=HPNumberMake(-1, "Error:arrayCount very max");
- HPNumber numberRight=HPNumberMake(-1, "Error:arrayCount very max");
- changeBlock(numberLeft,numberCentre,numberRight,scrollView.contentOffset);
-
- return;
- }
- else if (arrayCount==2)
- {
- HPNumber numberLeft=HPNumberMake(0, nil);
- HPNumber numberCentre=HPNumberMake(1, nil);
- HPNumber numberRight=HPNumberMake(-1, "Error:arrayCount very max");
- changeBlock(numberLeft,numberCentre,numberRight,scrollView.contentOffset);
- return;
- }
-
- }
- +(void)animationSlideView:(UIView *)slideModule
- slideModuleWidht:(CGFloat)slideModuleWith
- nowPoint:(HPPoint)nowPoint
- readyButton:(HPPoint)readyPoint
- movePercent:(CGFloat)movePercent
- {
- CGFloat nowX=0;
- CGFloat nowWidth=0;
-
- CGFloat readyX=0;
- CGFloat readyWidth=0;
-
- if (slideModuleWith!=0) {
- nowX=nowPoint.x+((nowPoint.width-slideModuleWith)/2);
- nowWidth=slideModuleWith;
-
- readyX=readyPoint.x+((readyPoint.width-slideModuleWith)/2);
- readyWidth=slideModuleWith;
-
- }
- else
- {
- nowX=nowPoint.x;//+((nowPoint.width-slideModuleWith)/2);
- nowWidth=nowPoint.width;
-
- readyX=readyPoint.x;//+((readyPoint.width-slideModuleWith)/2);
- readyWidth=readyPoint.width;
-
- }
-
-
- CGFloat speace=fabs(readyX-nowX);
-
- if (movePercent>=0 && movePercent<=0.5)
- {
-
- movePercent=movePercent/0.5;
-
-
- CGFloat move=0;
- CGFloat width=0;
- CGFloat slideNowX=0;
-
- if (nowX>readyX) {
- move=(nowX-readyX)*movePercent;
- // slideNowX=nowPoint.x-move;
- slideNowX=nowX-move;
- }
- else
- {
- move=((speace-nowWidth)+readyWidth)*movePercent;
- // slideNowX=nowPoint.x;
- slideNowX=nowX;
- }
-
- width=nowWidth+move;
-
- slideModule.frame=CGRectMake(slideNowX, slideModule.y, width, slideModule.height);
-
-
- }
- else if (movePercent >0.5 && movePercent<=1)
- {
-
- CGFloat changeX=0.0;
- CGFloat changeWidth=0.0;
- CGFloat changeSpace=0.0;
-
- if (nowX>readyX) {
-
- movePercent=(1-movePercent)/0.5;
-
- changeX=readyX;
- changeSpace=readyWidth+((nowX-(readyX+readyWidth))+nowWidth)*movePercent;
-
- }
- else
- {
- movePercent=(movePercent-0.5)/0.5;
-
- changeX=nowX+(speace*movePercent);
- changeWidth=fabs(nowX-changeX);
- changeSpace=speace+readyWidth-changeWidth;
- }
-
- slideModule.frame=CGRectMake(changeX, slideModule.y, changeSpace, slideModule.height);
-
-
- }
- }
- +(void)slideModuleAlignCenter:(UIScrollView *)currentScrollerView
- slideModuleWithX:(CGFloat)slideModuleX
- {
-
- CGFloat rightSide=currentScrollerView.contentSize.width-currentScrollerView.bounds.size.width;
- CGFloat centerHalf=currentScrollerView.bounds.size.width/2;
- if (slideModuleX>centerHalf && slideModuleX<rightSide+centerHalf+10) {
- CGFloat centerWidth=slideModuleX-currentScrollerView.contentOffset.x;
-
- CGFloat center=centerWidth-centerHalf;
-
- [UIView animateWithDuration:0.25 animations:^{
- currentScrollerView.contentOffset=CGPointMake(currentScrollerView.contentOffset.x+(center/2), 0);
- }];
-
- }
- else if (slideModuleX<centerHalf)
- {
- [UIView animateWithDuration:0.25 animations:^{
- currentScrollerView.contentOffset=CGPointMake(0, 0);
- }];
-
- }
- else if (slideModuleX>rightSide+centerHalf+10 && currentScrollerView.contentSize.width>currentScrollerView.width)
- {
- [UIView animateWithDuration:0.25 animations:^{
- currentScrollerView.contentOffset=CGPointMake(rightSide, 0);
- }];
- }
-
- }
- @end
|