财神随手记账

HPSlideSegmentLogic.m 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  1. //
  2. // HPSlideLogic.m
  3. // HPSlideSegmentView
  4. //
  5. // Created by 何鹏 on 17/6/14.
  6. // Copyright © 2017年 何鹏. All rights reserved.
  7. //
  8. #import "HPSlideSegmentLogic.h"
  9. #import "UIView+HPSlideSegmentRect.h"
  10. @implementation HPSlideSegmentLogic
  11. +(CGRect)oldButtonPoint:(HPPoint)oldpoint
  12. slideViewHeight:(CGFloat)slideViewHeight
  13. isModule:(BOOL)isModule
  14. content:(NSString *)text
  15. fontSize:(CGFloat)fontSize
  16. edgeInsets:(UIEdgeInsets)edgeInsets
  17. minWidth:(CGFloat)minWidth
  18. autoType:(AutoSizeType)type;
  19. {
  20. CGFloat x=oldpoint.x+oldpoint.width;
  21. CGFloat y=0;
  22. CGFloat width=[self widthForText:slideViewHeight content:text fontSize:fontSize];
  23. CGFloat height=slideViewHeight-(isModule==NO?3:0);
  24. if (oldpoint.width!=0) {
  25. x=x+edgeInsets.left+edgeInsets.right;
  26. }
  27. else
  28. {
  29. x=x+edgeInsets.left;
  30. }
  31. y=y+edgeInsets.top;
  32. height=height-edgeInsets.bottom-edgeInsets.top;
  33. switch (type) {
  34. case ENUM_HP_AUTOSIZE:
  35. {
  36. }
  37. break;
  38. case ENUM_HP_DEFINESIZE:
  39. {
  40. width=minWidth;
  41. }
  42. break;
  43. case ENUM_HP_AUTOMINSIZE:
  44. {
  45. width=width>minWidth?width:minWidth;
  46. }
  47. break;
  48. default:
  49. break;
  50. }
  51. return CGRectMake(x, y, width, height);
  52. }
  53. +(id)arrayCount:(NSArray *)arrays index:(NSInteger)index
  54. {
  55. if (arrays.count==0) {
  56. return nil;
  57. }
  58. else if (index<0)
  59. {
  60. return arrays[0];
  61. }
  62. NSUInteger indexType=index;
  63. if (indexType>arrays.count-1) {
  64. return arrays[arrays.count-1];
  65. }
  66. return arrays[index];
  67. }
  68. +(id)isArrayWithNil:(NSArray *)arrays index:(NSInteger)index
  69. {
  70. if (arrays.count==0) {
  71. return nil;
  72. }
  73. else if (index>arrays.count-1) {
  74. return nil;
  75. }
  76. return arrays[index];
  77. }
  78. //单独计算文本的高度
  79. + (CGFloat)widthForText:(CGFloat )height content:(NSString *)text fontSize:(CGFloat)fontSize
  80. {
  81. //设置计算文本时字体的大小,以什么标准来计算
  82. NSDictionary *attrbute = @{NSFontAttributeName:[UIFont systemFontOfSize:fontSize]};
  83. return [text boundingRectWithSize:CGSizeMake(1000, height) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:attrbute context:nil].size.width;
  84. }
  85. +(CGRect)slideModuleWithView:(CGFloat)slideModuleHeight
  86. slideViewHeight:(CGFloat)slideViewHeight
  87. defauleWidth:(CGFloat)width
  88. buttonWithX:(CGFloat)buttonX
  89. {
  90. CGFloat y=slideViewHeight-slideModuleHeight;
  91. CGFloat x=buttonX+((width/2)-width/2);
  92. if (y<0) {
  93. return CGRectMake(x, 0, width, slideViewHeight);
  94. }
  95. return CGRectMake(x, y, width, slideModuleHeight);
  96. }
  97. +(NSUInteger)arraCount:(NSUInteger)arrayCount index:(NSInteger)index
  98. {
  99. if (index<0) {
  100. return 0;
  101. }
  102. if (index>arrayCount-1) {
  103. return arrayCount-1;
  104. }
  105. return index;
  106. }
  107. //---
  108. //+(CGFloat)scrollViewWidth:(NSUInteger)width dataArrayCount:(NSUInteger)dataArrayCount
  109. //{
  110. //
  111. // if (dataArrayCount==1 || dataArrayCount==0) {
  112. // return width;
  113. // }
  114. // else if(dataArrayCount==2)
  115. // {
  116. // return width * 2;
  117. // }
  118. //
  119. // return width * 3;
  120. //}
  121. +(CGFloat)scrollViewWidth:(NSUInteger)width dataArrayCount:(NSUInteger)dataArrayCount
  122. {
  123. return width * dataArrayCount;
  124. }
  125. +(CGFloat)scrollViewWidth:(NSUInteger)width
  126. arrayDataCount:(NSUInteger)dataArrayCount
  127. currentIndex:(NSUInteger)currentIndex
  128. {
  129. if (currentIndex==0) {
  130. return 0;
  131. }
  132. else if (currentIndex==dataArrayCount-1)
  133. {
  134. return width*(dataArrayCount-1);
  135. }
  136. return currentIndex * width;
  137. }
  138. +(void)slideSuperView:(CGFloat)slideViewWidth
  139. scrollView:(UIScrollView *)scrollView
  140. currentIndex:(NSUInteger)currentIndex
  141. startOffset:(CGPoint )startOffset
  142. dataArray:(NSUInteger )arrayDataCount
  143. changeIndex:(CHANGEINDEXBLOCK)changeBlock
  144. {
  145. if (arrayDataCount<3) {
  146. return;
  147. }
  148. if (changeBlock==nil) {
  149. return;
  150. }
  151. [self currentIndex:currentIndex
  152. arrayCount:arrayDataCount
  153. scrollView:scrollView
  154. slideSuperView:slideViewWidth
  155. changeIndex:changeBlock];
  156. }
  157. +(void)scrollViewWithStartPoint:(CGPoint)startOffset
  158. moveOffset:(CGPoint)moveOffset
  159. slideModuleWidht:(CGFloat)slideModuleWith
  160. currentIndex:(NSUInteger )currentIndex
  161. dataArray:(NSUInteger)arrayCount
  162. startPointBlock:(ChangeStartPoint)startPointBlock
  163. {
  164. CGPoint hp_startOffset=startOffset;
  165. CGPoint hp_endOffset=CGPointMake(0, 0);
  166. CGFloat changeOffset=startOffset.x-moveOffset.x;
  167. if (changeOffset>0 || (changeOffset==0 && moveOffset.x==0)) {
  168. if (currentIndex==arrayCount-1 && arrayCount>=3) {
  169. hp_startOffset=CGPointMake(2*slideModuleWith, moveOffset.y);
  170. hp_endOffset=CGPointMake(slideModuleWith, moveOffset.y);
  171. }
  172. else
  173. {
  174. hp_startOffset=CGPointMake(slideModuleWith, moveOffset.y);
  175. hp_endOffset=CGPointMake(0, moveOffset.y);
  176. }
  177. }
  178. else if (changeOffset<0 || (changeOffset==0 && moveOffset.x==2*slideModuleWith))
  179. {
  180. if(currentIndex == 0)
  181. {
  182. hp_startOffset=CGPointMake(0, moveOffset.y);
  183. hp_endOffset=CGPointMake(slideModuleWith, moveOffset.y);
  184. }
  185. else
  186. {
  187. hp_startOffset=CGPointMake(slideModuleWith, moveOffset.y);
  188. hp_endOffset=CGPointMake(2*slideModuleWith, moveOffset.y);
  189. }
  190. }
  191. if (startPointBlock!=nil) {
  192. startPointBlock(hp_startOffset,hp_endOffset);
  193. }
  194. }
  195. +(void)scrollView:(UIScrollView *)scrollView
  196. currentIndex:(NSUInteger)currentIndex
  197. startOffset:(CGPoint )startOffset
  198. dataArray:(NSUInteger )arrayDataCount
  199. boardBlock:(BoardBlock)boardBlock
  200. moduleBlock:(ModuleAnimationBlock)moduleAnimationBlock{
  201. CGPoint off=[scrollView contentOffset];
  202. if (off.x > startOffset.x) {
  203. //right
  204. if (moduleAnimationBlock!=nil) {
  205. CGFloat percent=fabs(off.x-startOffset.x)/scrollView.width;
  206. NSUInteger exchangeIndex=[self arraCount:arrayDataCount index:currentIndex+1];
  207. if (exchangeIndex==currentIndex) {
  208. return;
  209. }
  210. moduleAnimationBlock(currentIndex,exchangeIndex,percent);
  211. }
  212. }else if (off.x < startOffset.x){
  213. //left
  214. if (moduleAnimationBlock!=nil) {
  215. CGFloat percent=(startOffset.x-off.x)/scrollView.width;
  216. NSUInteger exchangeIndex=[self arraCount:arrayDataCount index:currentIndex+1];
  217. if (exchangeIndex==currentIndex) {
  218. return;
  219. }
  220. moduleAnimationBlock(exchangeIndex,currentIndex,percent);
  221. }
  222. }
  223. if (boardBlock!=nil) {
  224. boardBlock();
  225. }
  226. }
  227. +(void)currentIndex:(NSUInteger)currentIndex
  228. arrayCount:(NSUInteger)arrayCount
  229. scrollView:(UIScrollView *)scrollView
  230. slideSuperView:(CGFloat)slideViewWidth
  231. changeIndex:(CHANGEINDEXBLOCK)changeBlock
  232. {
  233. if (changeBlock==nil) {
  234. return;
  235. }
  236. if (arrayCount<3) {
  237. [self minIndexJudege:currentIndex
  238. arrayCount:arrayCount
  239. scrollView:scrollView
  240. changeIndex:changeBlock
  241. slideSuperView:slideViewWidth];
  242. return;
  243. }
  244. if (currentIndex==0) {
  245. HPNumber numberLeft=HPNumberMake(currentIndex, nil);
  246. HPNumber numberCentre=HPNumberMake(currentIndex+1, nil);
  247. HPNumber numberRight=HPNumberMake(currentIndex+2, nil);
  248. changeBlock(numberLeft,numberCentre,numberRight,scrollView.contentOffset);
  249. return;
  250. }
  251. else if (currentIndex==arrayCount-1)
  252. {
  253. NSUInteger count=arrayCount-1;
  254. HPNumber numberLeft=HPNumberMake(count-2, nil);
  255. HPNumber numberCentre=HPNumberMake(count-1, nil);
  256. HPNumber numberRight=HPNumberMake(count, nil);
  257. changeBlock(numberLeft,numberCentre,numberRight,scrollView.contentOffset);
  258. return;
  259. }
  260. HPNumber numberLeft=HPNumberMake(currentIndex-1, nil);
  261. HPNumber numberCentre=HPNumberMake(currentIndex, nil);
  262. HPNumber numberRight=HPNumberMake(currentIndex+1, nil);
  263. changeBlock(numberLeft,numberCentre,numberRight,scrollView.contentOffset);
  264. }
  265. +(void)minIndexJudege:(NSUInteger )currentIndex
  266. arrayCount:(NSUInteger)arrayCount
  267. scrollView:(UIScrollView *)scrollView
  268. changeIndex:(CHANGEINDEXBLOCK)changeBlock
  269. slideSuperView:(CGFloat)slideViewWidth
  270. {
  271. if (currentIndex<1) {
  272. scrollView.contentOffset=CGPointMake(0, 0);
  273. }
  274. else
  275. {
  276. scrollView.contentOffset=CGPointMake(slideViewWidth, 0);
  277. }
  278. if (arrayCount==0) {
  279. return;
  280. }
  281. else if (arrayCount==1)
  282. {
  283. HPNumber numberLeft=HPNumberMake(0, nil);
  284. HPNumber numberCentre=HPNumberMake(-1, "Error:arrayCount very max");
  285. HPNumber numberRight=HPNumberMake(-1, "Error:arrayCount very max");
  286. changeBlock(numberLeft,numberCentre,numberRight,scrollView.contentOffset);
  287. return;
  288. }
  289. else if (arrayCount==2)
  290. {
  291. HPNumber numberLeft=HPNumberMake(0, nil);
  292. HPNumber numberCentre=HPNumberMake(1, nil);
  293. HPNumber numberRight=HPNumberMake(-1, "Error:arrayCount very max");
  294. changeBlock(numberLeft,numberCentre,numberRight,scrollView.contentOffset);
  295. return;
  296. }
  297. }
  298. +(void)animationSlideView:(UIView *)slideModule
  299. slideModuleWidht:(CGFloat)slideModuleWith
  300. nowPoint:(HPPoint)nowPoint
  301. readyButton:(HPPoint)readyPoint
  302. movePercent:(CGFloat)movePercent
  303. {
  304. CGFloat nowX=0;
  305. CGFloat nowWidth=0;
  306. CGFloat readyX=0;
  307. CGFloat readyWidth=0;
  308. if (slideModuleWith!=0) {
  309. nowX=nowPoint.x+((nowPoint.width-slideModuleWith)/2);
  310. nowWidth=slideModuleWith;
  311. readyX=readyPoint.x+((readyPoint.width-slideModuleWith)/2);
  312. readyWidth=slideModuleWith;
  313. }
  314. else
  315. {
  316. nowX=nowPoint.x;//+((nowPoint.width-slideModuleWith)/2);
  317. nowWidth=nowPoint.width;
  318. readyX=readyPoint.x;//+((readyPoint.width-slideModuleWith)/2);
  319. readyWidth=readyPoint.width;
  320. }
  321. CGFloat speace=fabs(readyX-nowX);
  322. if (movePercent>=0 && movePercent<=0.5)
  323. {
  324. movePercent=movePercent/0.5;
  325. CGFloat move=0;
  326. CGFloat width=0;
  327. CGFloat slideNowX=0;
  328. if (nowX>readyX) {
  329. move=(nowX-readyX)*movePercent;
  330. // slideNowX=nowPoint.x-move;
  331. slideNowX=nowX-move;
  332. }
  333. else
  334. {
  335. move=((speace-nowWidth)+readyWidth)*movePercent;
  336. // slideNowX=nowPoint.x;
  337. slideNowX=nowX;
  338. }
  339. width=nowWidth+move;
  340. slideModule.frame=CGRectMake(slideNowX, slideModule.y, width, slideModule.height);
  341. }
  342. else if (movePercent >0.5 && movePercent<=1)
  343. {
  344. CGFloat changeX=0.0;
  345. CGFloat changeWidth=0.0;
  346. CGFloat changeSpace=0.0;
  347. if (nowX>readyX) {
  348. movePercent=(1-movePercent)/0.5;
  349. changeX=readyX;
  350. changeSpace=readyWidth+((nowX-(readyX+readyWidth))+nowWidth)*movePercent;
  351. }
  352. else
  353. {
  354. movePercent=(movePercent-0.5)/0.5;
  355. changeX=nowX+(speace*movePercent);
  356. changeWidth=fabs(nowX-changeX);
  357. changeSpace=speace+readyWidth-changeWidth;
  358. }
  359. slideModule.frame=CGRectMake(changeX, slideModule.y, changeSpace, slideModule.height);
  360. }
  361. }
  362. +(void)slideModuleAlignCenter:(UIScrollView *)currentScrollerView
  363. slideModuleWithX:(CGFloat)slideModuleX
  364. {
  365. CGFloat rightSide=currentScrollerView.contentSize.width-currentScrollerView.bounds.size.width;
  366. CGFloat centerHalf=currentScrollerView.bounds.size.width/2;
  367. if (slideModuleX>centerHalf && slideModuleX<rightSide+centerHalf+10) {
  368. CGFloat centerWidth=slideModuleX-currentScrollerView.contentOffset.x;
  369. CGFloat center=centerWidth-centerHalf;
  370. [UIView animateWithDuration:0.25 animations:^{
  371. currentScrollerView.contentOffset=CGPointMake(currentScrollerView.contentOffset.x+(center/2), 0);
  372. }];
  373. }
  374. else if (slideModuleX<centerHalf)
  375. {
  376. [UIView animateWithDuration:0.25 animations:^{
  377. currentScrollerView.contentOffset=CGPointMake(0, 0);
  378. }];
  379. }
  380. else if (slideModuleX>rightSide+centerHalf+10 && currentScrollerView.contentSize.width>currentScrollerView.width)
  381. {
  382. [UIView animateWithDuration:0.25 animations:^{
  383. currentScrollerView.contentOffset=CGPointMake(rightSide, 0);
  384. }];
  385. }
  386. }
  387. @end