财神随手记账

HPSlideModuleView.m 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  1. //
  2. // HPSlideView.m
  3. // HPSlideSegmentView
  4. //
  5. // Created by 何鹏 on 17/6/12.
  6. // Copyright © 2017年 何鹏. All rights reserved.
  7. //
  8. #import "HPSlideModuleView.h"
  9. #import "HPSlideSegmentLogic.h"
  10. #import "UIView+HPSlideSegmentRect.h"
  11. #import <objc/runtime.h>
  12. @interface HPSlideModuleView ()<UIScrollViewDelegate>
  13. @property(nonatomic,strong) UIScrollView *scrollView;
  14. @property(nonatomic,strong) UIView *backgroundView;
  15. @property(nonatomic,strong) NSMutableArray<UIButton *> *arrayButtons;
  16. @property(nonatomic,strong) NSMutableArray<UIView *> *arraySpaceLines;
  17. @property(nonatomic,weak) id hpWeakObj;
  18. @property(nonatomic,strong) HPSLIDEMODULBUTTONBLOCK hpActionBlock;
  19. @property(nonatomic,strong) UIButton *oldActionButton;
  20. @end
  21. @implementation HPSlideModuleView
  22. -(instancetype)initWithFrame:(CGRect)frame
  23. {
  24. if (self=[super initWithFrame:frame]) {
  25. self.hiddenModule=NO;
  26. [self layoutSlideModule];
  27. }
  28. return self;
  29. }
  30. -(void)layoutSubviews
  31. {
  32. [super layoutSubviews];
  33. [self updateLayout];
  34. }
  35. -(void)updateLayout
  36. {
  37. self.scrollView.contentSize=CGSizeMake(0, 0);
  38. [self layoutSlideModule];
  39. }
  40. -(void)layoutSlideModule
  41. {
  42. self.scrollView.frame=CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height);
  43. [self addSubview:_scrollView];
  44. [_scrollView addSubview:self.backgroundView];
  45. [self scrollViewLayoutWithCount:self.showCount
  46. cacheArray:self.arrayButtons
  47. delegateContent:_delegate
  48. layoutView:self
  49. moduleHeight:self.bounds.size.height];
  50. self.backgroundColor=[UIColor whiteColor];
  51. self.slideModuleColor=_slideModuleColor;
  52. self.slideModuleView=_slideModuleView;
  53. [self buttonAction:_arrayButtons];
  54. [self slideModuleLayout];
  55. }
  56. -(void)slideModuleLayout
  57. {
  58. UIButton *defaultButton=nil;
  59. if (self.arrayButtons.count!=0) {
  60. defaultButton=self.arrayButtons[0];
  61. }
  62. self.slideModuleView.backgroundColor=self.slideModuleColor;
  63. self.slideModuleView.frame=[HPSlideSegmentLogic slideModuleWithView:self.slideModuleView.bounds.size.height
  64. slideViewHeight:self.bounds.size.height
  65. defauleWidth:defaultButton.bounds.size.width
  66. buttonWithX:defaultButton.x];
  67. [HPSlideSegmentLogic animationSlideView:self.slideModuleView
  68. slideModuleWidht:_slideModeuleWidth
  69. nowPoint:HPPointMake(defaultButton.x, defaultButton.width)
  70. readyButton:HPPointMake(defaultButton.x, defaultButton.width)
  71. movePercent:0];
  72. [self.backgroundView insertSubview:_slideModuleView atIndex:0];
  73. }
  74. -(void)buttonAction:(NSArray<UIButton *> *)arrayButtons
  75. {
  76. for (int i=0; i<arrayButtons.count; i++) {
  77. UIButton *button=arrayButtons[i];
  78. [button addTarget:self action:@selector(actionButton:) forControlEvents:UIControlEventTouchUpInside];
  79. }
  80. }
  81. -(void)hp_weak:(id)weakObj actionButton:(HPSLIDEMODULBUTTONBLOCK)actionBlock
  82. {
  83. _hpWeakObj=weakObj;
  84. _hpActionBlock=actionBlock;
  85. }
  86. -(void)updateLayoutWithIndex:(NSUInteger)index
  87. {
  88. UIButton *button=[HPSlideSegmentLogic isArrayWithNil:_arrayButtons index:index];
  89. [self actionButton:button];
  90. }
  91. -(void)actionButton:(UIButton *)button
  92. {
  93. if (_hpActionBlock!=nil && button!=_oldActionButton) {
  94. [self selectButton:button];
  95. _oldActionButton=button;
  96. [HPSlideSegmentLogic animationSlideView:self.slideModuleView
  97. slideModuleWidht:_slideModeuleWidth
  98. nowPoint:HPPointMake(button.x, button.width)
  99. readyButton:HPPointMake(button.x, button.width)
  100. movePercent:0];
  101. _hpActionBlock(_hpWeakObj,button.index);
  102. [HPSlideSegmentLogic slideModuleAlignCenter:self.scrollView
  103. slideModuleWithX:self.slideModuleView.x];
  104. }
  105. }
  106. -(void)slideBetweenWithButton:(UIButton *)button
  107. {
  108. if (button.x-self.width/2>0) {
  109. if (_scrollView.contentOffset.x+self.width<_scrollView.contentSize.width) {
  110. _scrollView.contentOffset=CGPointMake(button.x-self.width/2, 0);
  111. }
  112. }
  113. else{
  114. _scrollView.contentOffset=CGPointMake(0, 0);
  115. }
  116. }
  117. -(void)slideWithNowIndex:(NSUInteger)nowIndex readyIndex:(NSUInteger)readyIndex movePercent:(CGFloat)movePercent
  118. {
  119. UIButton *nowButton=_arrayButtons[nowIndex];
  120. UIButton *readyButton=_arrayButtons[readyIndex];
  121. [HPSlideSegmentLogic animationSlideView:self.slideModuleView
  122. slideModuleWidht:_slideModeuleWidth
  123. nowPoint:HPPointMake(nowButton.x, nowButton.width)
  124. readyButton:HPPointMake(readyButton.x, readyButton.width)
  125. movePercent:movePercent];
  126. [HPSlideSegmentLogic slideModuleAlignCenter:self.scrollView
  127. slideModuleWithX:self.slideModuleView.x];
  128. [self selectButton:readyButton];
  129. }
  130. -(void)scrollViewLayoutWithCount:(NSUInteger )count
  131. cacheArray:(NSMutableArray<UIButton *> *)arrayButtons
  132. delegateContent:(id)delegate
  133. layoutView:(HPSlideModuleView *)moduleView
  134. moduleHeight:(CGFloat)height
  135. {
  136. for (int i=0; i<count; i++) {
  137. UIButton *module=[self cacheButton:arrayButtons
  138. getCacheButton:i
  139. delegateContent:delegate
  140. moduleHeight:height];
  141. // UIView *view = nil;
  142. // if (_spaceLine == YES) {
  143. // view = [self spaceLineWithIndex:i];
  144. // }
  145. //
  146. // view.frame = CGRectMake(module.width+module.x + 3, view.y, view.width, view.height);
  147. if (count-1==i) {
  148. CGFloat widthAddY=module.width+module.x;
  149. moduleView.scrollView.contentSize =CGSizeMake(widthAddY, 0);
  150. }
  151. if (_backgroundSelectColor!=nil && i==0) {
  152. module.backgroundColor=_backgroundSelectColor;
  153. }
  154. else if(_backgroundDefaultColor!=nil)
  155. {
  156. module.backgroundColor=_backgroundDefaultColor;
  157. }
  158. moduleView.backgroundView.frame=CGRectMake(0, 0, moduleView.scrollView.contentSize.width,moduleView.scrollView.height);
  159. [moduleView.backgroundView addSubview:module];
  160. // [moduleView.backgroundView addSubview:view];
  161. module.index=i;
  162. module=nil;
  163. }
  164. }
  165. -(UIButton *)cacheButton:(NSMutableArray<UIButton *> *)arrayButtons
  166. getCacheButton:(NSUInteger)index
  167. delegateContent:(id)delegate
  168. moduleHeight:(CGFloat)height
  169. {
  170. UIButton *currenButton=[HPSlideSegmentLogic isArrayWithNil:arrayButtons index:index];
  171. UIButton *oldButton=[HPSlideSegmentLogic arrayCount:arrayButtons index:index-1];
  172. if (currenButton==nil) {
  173. return [self creatModule:index
  174. casheArray:arrayButtons
  175. delegateContent:delegate
  176. moduleHeight:height];
  177. }else
  178. {
  179. [self buttonLayoutWithNew:currenButton currentIndex:index oldButton:oldButton];
  180. }
  181. return [self buttonLayoutWithOldButton:oldButton
  182. newButton:currenButton
  183. moduleHeight:height
  184. buttonContent:currenButton.titleLabel.text];
  185. }
  186. -(UIButton *)creatModule:(NSUInteger)index
  187. casheArray:(NSMutableArray<UIButton *> *)arrayButtons
  188. delegateContent:(id)delegate
  189. moduleHeight:(CGFloat)height
  190. {
  191. UIButton *module=nil;
  192. if ([delegate respondsToSelector:@selector(hp_slideWithIndex:)]) {
  193. module=[delegate hp_slideWithIndex:index];
  194. }
  195. else
  196. {
  197. module=[[UIButton alloc] init];
  198. }
  199. UIButton *oldButton=[HPSlideSegmentLogic arrayCount:arrayButtons index:index-1];
  200. [self buttonLayoutWithNew:module currentIndex:index oldButton:oldButton];
  201. [arrayButtons addObject:module];
  202. return module;
  203. }
  204. -(UIView *)spaceLineWithIndex:(NSUInteger)index{
  205. if (index == _arrayButtons.count-1) {
  206. return nil;
  207. }
  208. UIView *lineView = nil;
  209. if (_arraySpaceLines.count > index) {
  210. lineView = _arrayButtons[index];
  211. return lineView;
  212. }
  213. else{
  214. lineView = [[UIView alloc] init];
  215. lineView.backgroundColor = [UIColor blueColor];
  216. lineView.frame = CGRectMake(0, 3, 1, self.height - 6);
  217. [_arraySpaceLines addObject:lineView];
  218. }
  219. return lineView;
  220. }
  221. -(void)buttonLayoutWithNew:(UIButton *)module currentIndex:(NSUInteger)index oldButton:(UIButton *)oldButton
  222. {
  223. NSString *content=@"";
  224. if ([self.delegate respondsToSelector:@selector(hp_slideContentWithIndex:)]) {
  225. content=[self.delegate hp_slideContentWithIndex:index];
  226. }
  227. [module setTitle:content forState:UIControlStateNormal];
  228. [module setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
  229. module.titleLabel.font=[UIFont systemFontOfSize:14];
  230. module = [self buttonLayoutWithOldButton:oldButton
  231. newButton:module
  232. moduleHeight:self.bounds.size.height
  233. buttonContent:content];
  234. content=nil;
  235. }
  236. -(UIButton *)buttonLayoutWithOldButton:(UIButton *)oldButton
  237. newButton:(UIButton *)module
  238. moduleHeight:(CGFloat)height
  239. buttonContent:(NSString *)content
  240. {
  241. module.frame=CGRectMake(0, 0, 0, 0);
  242. module.frame=[HPSlideSegmentLogic oldButtonPoint:HPPointMake(oldButton.frame.origin.x, oldButton.bounds.size.width)
  243. slideViewHeight:height
  244. isModule:self.hiddenModule
  245. content:content
  246. fontSize:module.titleLabel.font.pointSize
  247. edgeInsets:self.edgeInsets
  248. minWidth:self.minWidth
  249. autoType:self.type];
  250. return module;
  251. }
  252. -(void)selectButton:(UIButton *)selectButton
  253. {
  254. for (int i=0; i<_arrayButtons.count; i++) {
  255. UIButton *button=_arrayButtons[i];
  256. if (button==selectButton) {
  257. if (_backgroundSelectColor!=nil) {
  258. button.backgroundColor=_backgroundSelectColor;
  259. }
  260. button.selected=YES;
  261. }
  262. else
  263. {
  264. if (_backgroundDefaultColor!=nil) {
  265. button.backgroundColor=_backgroundDefaultColor;
  266. }
  267. button.selected=NO;
  268. }
  269. }
  270. }
  271. #pragma mark - 懒加载
  272. -(void)setBackgroundDefaultColor:(UIColor *)backgroundDefaultColor
  273. {
  274. _backgroundDefaultColor=backgroundDefaultColor;
  275. [self updateLayout];
  276. }
  277. -(void)setBackgroundSelectColor:(UIColor *)backgroundSelectColor
  278. {
  279. _backgroundSelectColor=backgroundSelectColor;
  280. [self updateLayout];
  281. }
  282. -(void)setDelegate:(id<SlideModuleViewDelegate>)delegate
  283. {
  284. _delegate=delegate;
  285. for (int i=0; i<self.arrayButtons.count; i++) {
  286. UIButton *button=_arrayButtons[i];
  287. [button removeFromSuperview];
  288. }
  289. [self.arrayButtons removeAllObjects];
  290. [self updateLayout];
  291. }
  292. -(void)setHiddenModule:(BOOL)hiddenModule
  293. {
  294. _hiddenModule=hiddenModule;
  295. if (hiddenModule==YES) {
  296. [_slideModuleView removeFromSuperview];
  297. _slideModuleView=nil;
  298. [self updateLayout];
  299. }
  300. }
  301. -(void)setEdgeInsets:(UIEdgeInsets)edgeInsets
  302. {
  303. _edgeInsets=edgeInsets;
  304. [self updateLayout];
  305. }
  306. -(void)setMinWidth:(CGFloat)minWidth
  307. {
  308. _minWidth=minWidth;
  309. [self updateLayout];
  310. }
  311. -(void)setType:(AutoSizeType)type
  312. {
  313. _type=type;
  314. [self updateLayout];
  315. }
  316. -(void)setShowCount:(NSUInteger)showCount
  317. {
  318. _showCount=showCount;
  319. [self updateLayout];
  320. }
  321. -(UIScrollView *)scrollView
  322. {
  323. if (_scrollView==nil) {
  324. _scrollView=[[UIScrollView alloc] init];
  325. _scrollView.backgroundColor=[UIColor clearColor];
  326. _scrollView.delegate=self;
  327. _scrollView.showsVerticalScrollIndicator=NO;
  328. _scrollView.showsHorizontalScrollIndicator=NO;
  329. }
  330. return _scrollView;
  331. }
  332. -(void)setSlideModuleView:(UIView *)slideModuleView
  333. {
  334. if (self.hiddenModule==YES) {
  335. [_slideModuleView removeFromSuperview];
  336. _slideModuleView=nil;
  337. _slideModuleView=slideModuleView;
  338. return;
  339. }
  340. if (slideModuleView==nil) {
  341. _slideModuleView=[[UIView alloc] init];
  342. self.slideModuleColor=_slideModuleColor;
  343. _slideModuleView.backgroundColor=self.slideModuleColor;
  344. _slideModuleView.frame=CGRectMake(0, 0, 10, 3);
  345. }
  346. else
  347. {
  348. self.slideModuleColor=slideModuleView.backgroundColor;
  349. [self slideModuleLayout];
  350. }
  351. }
  352. -(void)setSlideModuleColor:(UIColor *)slideModuleColor
  353. {
  354. if (slideModuleColor==nil ) {
  355. _slideModuleColor=[UIColor redColor];
  356. }
  357. else
  358. {
  359. _slideModuleColor=slideModuleColor;
  360. [self slideModuleLayout];
  361. }
  362. }
  363. -(UIView *)backgroundView
  364. {
  365. if (_backgroundView==nil) {
  366. _backgroundView=[[UIView alloc] init];
  367. _backgroundView.backgroundColor=[UIColor clearColor];
  368. }
  369. return _backgroundView;
  370. }
  371. -(NSMutableArray<UIView *> *)arraySpaceLines{
  372. if (_arraySpaceLines==nil) {
  373. _arraySpaceLines = [NSMutableArray array];
  374. }
  375. return _arraySpaceLines;
  376. }
  377. -(NSMutableArray<UIButton *> *)arrayButtons
  378. {
  379. if (_arrayButtons==nil) {
  380. _arrayButtons=[NSMutableArray array];
  381. }
  382. return _arrayButtons;
  383. }
  384. @end
  385. #pragma mark - HPSlideSegment
  386. static void *indexKey = &indexKey;
  387. @implementation UIButton (HPSlideSegment)
  388. -(void)setIndex:(NSUInteger)index
  389. {
  390. objc_setAssociatedObject(self, &indexKey, @(index), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  391. }
  392. -(NSUInteger)index
  393. {
  394. return [objc_getAssociatedObject(self, &indexKey) integerValue];
  395. }
  396. @end