SliderView.m 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. //
  2. // SliderView.m
  3. // 乐销
  4. //
  5. // Created by 隋林栋 on 2016/12/19.
  6. // Copyright © 2016年 ping. All rights reserved.
  7. //
  8. #import "SliderView.h"
  9. @implementation SliderView
  10. #define Height_View 44//默认总高度
  11. #pragma mark 懒加载
  12. - (UIScrollView *)scrollView{
  13. if (_scrollView == nil) {
  14. _scrollView = [UIScrollView new];
  15. _scrollView.frame = CGRectMake(0, 0, self.width, self.height);
  16. _scrollView.contentSize = CGSizeMake(0, 0);
  17. _scrollView.backgroundColor = [UIColor clearColor];
  18. _scrollView.showsHorizontalScrollIndicator = false;
  19. _scrollView.showsVerticalScrollIndicator = false;
  20. }
  21. return _scrollView;
  22. }
  23. - (UIView *)viewSlid{
  24. if (!_viewSlid) {
  25. _viewSlid = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 0, self.viewSlidHeight)];
  26. _viewSlid.backgroundColor = self.viewSlidColor;
  27. [GlobalMethod setRoundView:_viewSlid color:[UIColor clearColor] numRound:5/2 width:0];
  28. }
  29. return _viewSlid;
  30. }
  31. - (UIColor *)viewSlidColor{
  32. if (!_viewSlidColor) {
  33. _viewSlidColor = COLOR_BLUE;
  34. }
  35. return _viewSlidColor;
  36. }
  37. - (CGFloat )viewSlidHeight{
  38. if (!_viewSlidHeight) {
  39. _viewSlidHeight = 2;
  40. }
  41. return _viewSlidHeight;
  42. }
  43. - (NSArray *)aryModels{
  44. if (!_aryModels) {
  45. _aryModels = [NSArray new];
  46. }
  47. return _aryModels;
  48. }
  49. #pragma mark 初始化
  50. - (instancetype)initWithFrame:(CGRect)frame{
  51. self = [super initWithFrame:frame];
  52. if (self) {
  53. //设置frame
  54. self.widthHeight = XY(SCREEN_WIDTH, Height_View);
  55. self.backgroundColor = [UIColor whiteColor];
  56. //add scroll
  57. [self addSubview:self.scrollView];
  58. }
  59. return self;
  60. }
  61. - (void)resetWithAry:(NSArray *)aryModel{
  62. if (!isAry(aryModel)) {
  63. return;
  64. }
  65. self.aryModels = aryModel;
  66. [self.scrollView removeAllSubViews];
  67. self.scrollView.frame = CGRectMake(0, 0, self.width, self.height);
  68. //添加按钮
  69. float btnWidth = self.isScroll ? 0 : self.width/aryModel.count;
  70. float btnHeight = self.isHasSlider? self.height - self.viewSlidHeight : self.height;
  71. float numX = 0;
  72. //添加滑动条
  73. if (self.isHasSlider) {
  74. [self.scrollView addSubview:self.viewSlid];
  75. self.viewSlid.backgroundColor = self.viewSlidColor;
  76. self.viewSlid.height = self.viewSlidHeight;
  77. self.viewSlid.top = btnHeight;
  78. }
  79. for (int i=0; i<aryModel.count; i++) {
  80. ModelBtn * model = aryModel[i];
  81. CustomSliderControl * control = [CustomSliderControl new];
  82. control.textColor = model.color;
  83. control.textColorSelect = model.colorSelect;
  84. [control resetControlFrame:CGRectMake(numX, 0, btnWidth, btnHeight) tag:i title:model.title imageName:model.imageName highImageName:model.highImageName isImageLeft:self.isImageLeft hasLineVertical:self.isLineVerticalHide?false: i<aryModel.count-1];
  85. [control addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
  86. if (i==self.sliderIndex) {
  87. self.selectSliderIndex = self.sliderIndex;
  88. self.viewSlid.width = self.viewSlidWidth?:control.width;
  89. self.viewSlid.centerX = control.centerX;
  90. control.selected = YES;
  91. }
  92. numX = control.right;
  93. [self.scrollView addSubview:control];
  94. self.scrollView.contentSize = CGSizeMake(numX, 0);
  95. }
  96. }
  97. - (void)resetWithMaxAry:(NSArray *)aryModel{
  98. if (!isAry(aryModel)) {
  99. return;
  100. }
  101. self.aryModels = aryModel;
  102. [self.scrollView removeAllSubViews];
  103. self.scrollView.frame = CGRectMake(0, 0, self.width, self.height);
  104. //添加按钮
  105. float btnWidth = self.isScroll ? 0 : self.width/aryModel.count;
  106. float btnHeight = self.isHasSlider? self.height - self.viewSlidHeight : self.height;
  107. float numX = 0;
  108. //添加滑动条
  109. if (self.isHasSlider) {
  110. [self.scrollView addSubview:self.viewSlid];
  111. self.viewSlid.backgroundColor = self.viewSlidColor;
  112. self.viewSlid.height = self.viewSlidHeight;
  113. self.viewSlid.top = btnHeight;
  114. }
  115. for (int i=0; i<aryModel.count; i++) {
  116. ModelBtn * model = aryModel[i];
  117. CustomSliderMaxControl * control = [CustomSliderMaxControl new];
  118. control.textColor = model.color;
  119. control.textColorSelect = model.colorSelect;
  120. [control resetControlFrame:CGRectMake(numX, 0, btnWidth, btnHeight) tag:i title:model.title imageName:model.imageName highImageName:model.highImageName isImageLeft:self.isImageLeft hasLineVertical:self.isLineVerticalHide?false: i<aryModel.count-1];
  121. [control addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
  122. if (i == 0) {
  123. self.selectSliderIndex = 0;
  124. self.viewSlid.width = self.viewSlidWidth?:control.width;
  125. self.viewSlid.centerX = control.centerX;
  126. control.selected = YES;
  127. }
  128. numX = control.right;
  129. [self.scrollView addSubview:control];
  130. self.scrollView.contentSize = CGSizeMake(numX, 0);
  131. }
  132. }
  133. //按钮点击
  134. - (void)btnClick:(CustomSliderControl *)btn
  135. {
  136. //设置按钮选择
  137. NSArray * ary = self.scrollView.subviews;
  138. for (UIView * viewSub in ary) {
  139. if ([viewSub isKindOfClass:[CustomSliderControl class]]) {
  140. CustomSliderControl * btnSub = (CustomSliderControl *)viewSub;
  141. btnSub.selected = NO;
  142. }
  143. }
  144. //滑动条滑动
  145. if (self.aryModels.count > btn.tag) {
  146. ModelBtn * modelBtn = self.aryModels[btn.tag];
  147. if (!modelBtn.isNotShowAnimated) {
  148. [self animateSlide:btn];
  149. }
  150. }
  151. if (btn.tag==0){
  152. if (self.blockModel) {
  153. self.blockModel(self);
  154. }
  155. }
  156. //代理回调
  157. if (self.delegate != nil && [self.delegate respondsToSelector:@selector(protocolSliderViewBtnSelect:btn:)]) {
  158. [self.delegate protocolSliderViewBtnSelect:btn.tag btn:btn];
  159. }
  160. }
  161. #pragma mark 滑动滑块
  162. - (void)sliderToIndex:(int)index noticeDelegate:(BOOL)notice animate:(BOOL)animate{
  163. //设置按钮选择
  164. NSArray * ary = self.scrollView.subviews;
  165. for (UIView * viewSub in ary) {
  166. if ([viewSub isKindOfClass:[CustomSliderControl class]]) {
  167. CustomSliderControl * btnSub = (CustomSliderControl *)viewSub;
  168. //如果选择的没变则返回
  169. if (btnSub.tag == index) {
  170. if (btnSub.selected) {
  171. return;
  172. }
  173. //滑动条滑动
  174. [self animateSlide:btnSub animated:animate];
  175. //代理回调
  176. if (notice) {
  177. if (self.delegate != nil && [self.delegate respondsToSelector:@selector(protocolSliderViewBtnSelect:btn:)]) {
  178. [self.delegate protocolSliderViewBtnSelect:index btn:btnSub];
  179. }
  180. }
  181. }else{
  182. btnSub.selected = NO;
  183. }
  184. }
  185. }
  186. }
  187. - (void)sliderToIndex:(int)index noticeDelegate:(BOOL)notice{
  188. [self sliderToIndex:index noticeDelegate:notice animate:true];
  189. }
  190. - (void)refreshBtn:(int)index title:(NSString *)strTitle{
  191. CustomSliderControl * btn = (CustomSliderControl *) [self viewWithTag:index];
  192. if (btn != nil && [btn isKindOfClass:[CustomSliderControl class]]) {
  193. [btn changeTitle:strTitle];
  194. }
  195. }
  196. #pragma mark 滑动动画
  197. - (void)animateSlide:(UIControl *)btn {
  198. [self animateSlide:btn animated:true];
  199. }
  200. - (void)animateSlide:(UIControl *)btn animated:(BOOL)animate{
  201. btn.selected = YES;
  202. self.selectSliderIndex = btn.tag;
  203. //滑动条滑动
  204. [UIView animateWithDuration:animate?0.5:0 animations:^{
  205. self.viewSlid.width = self.viewSlidWidth?:btn.width;
  206. self.viewSlid.centerX = btn.centerX;
  207. float numX = SCREEN_WIDTH/2-btn.centerX;
  208. //判断是否可以滑动
  209. if (self.scrollView.contentSize.width-SCREEN_WIDTH>0) {
  210. if (-numX<0) {
  211. self.scrollView.contentOffset = CGPointMake(0, 0);
  212. }else if(-numX>self.scrollView.contentSize.width-SCREEN_WIDTH){
  213. self.scrollView.contentOffset = CGPointMake(self.scrollView.contentSize.width-SCREEN_WIDTH, 0);
  214. }else{
  215. self.scrollView.contentOffset = CGPointMake(-numX, 0);
  216. }
  217. }
  218. }];
  219. }
  220. @end
  221. @implementation CustomSliderControl
  222. @synthesize textColor = _textColor;
  223. @synthesize textColorSelect = _textColorSelect;
  224. #pragma mark 初始化
  225. - (instancetype)init{
  226. self = [super init];
  227. if (self) {
  228. [self addSubview:self.labelTitle];
  229. [self addSubview:self.iv];
  230. }
  231. return self;
  232. }
  233. //懒加载
  234. - (UILabel *)labelTitle{
  235. if (_labelTitle == nil) {
  236. _labelTitle = [UILabel new];
  237. [GlobalMethod setLabel:_labelTitle widthLimit:0 numLines:1 fontNum:16 textColor:COLOR_LABEL text:@""];
  238. }
  239. return _labelTitle;
  240. }
  241. - (UIImageView *)iv{
  242. if (_iv == nil) {
  243. _iv = [UIImageView new];
  244. _iv.backgroundColor = [UIColor clearColor];
  245. }
  246. return _iv;
  247. }
  248. - (UIColor *)textColor{
  249. if (_textColor == nil) {
  250. _textColor = [UIColor colorWithHexString:@"#777777"];
  251. }
  252. return _textColor;
  253. }
  254. - (UIColor *)textColorSelect{
  255. if (_textColorSelect == nil) {
  256. _textColorSelect = [UIColor blackColor];
  257. }
  258. return _textColorSelect;
  259. }
  260. - (void)setSelected:(BOOL)selected{
  261. [super setSelected:selected];
  262. self.labelTitle.textColor = selected ? self.textColorSelect : self.textColor;
  263. self.iv.highlighted = selected;
  264. }
  265. #pragma mark 加载页面
  266. - (void)resetControlFrame:(CGRect )rect
  267. tag:(int )tag
  268. title:(NSString *)title
  269. imageName:(NSString *)imageName
  270. highImageName:(NSString *)highImageName
  271. isImageLeft:(BOOL)isImageLeft
  272. hasLineVertical:(BOOL)hasLineVertical
  273. {
  274. self.frame = rect;
  275. self.tag = tag;
  276. [self.labelTitle fitTitle:title variable:0];
  277. if (rect.size.width == 0) {
  278. self.width = self.labelTitle.width + 10*2;
  279. }
  280. self.labelTitle.center = CGPointMake(self.width/2.0, self.height/2.0);
  281. if (imageName != nil) {
  282. UIImage * image = [UIImage imageNamed:imageName];
  283. self.iv.image = image;
  284. self.iv.width = image.size.width;
  285. self.iv.height = image.size.height;
  286. if (highImageName != nil) {
  287. [self.iv setHighlightedImage:[UIImage imageNamed:highImageName]];
  288. }
  289. if (rect.size.width == 0) {
  290. self.width +=self.iv.width + 10;
  291. self.labelTitle.center = CGPointMake(self.width/2.0, self.height/2.0);
  292. }
  293. if (isImageLeft) {
  294. self.labelTitle.left += self.iv.width/2.0 + 10/2.0;
  295. self.iv.left = self.labelTitle.left - self.iv.width - 10;
  296. } else {
  297. self.labelTitle.left -= self.iv.width/2.0 + 10/2.0;
  298. self.iv.left = self.labelTitle.right + 10;
  299. }
  300. self.iv.centerY = self.height/2.0;
  301. }
  302. self.labelTitle.textColor = self.textColor;
  303. if (hasLineVertical) {
  304. [self addLineFrame:CGRectMake(self.width - 1, 4, 1, self.height - 8)];
  305. }
  306. }
  307. //更改title
  308. - (void)changeTitle:(NSString *)strTitle{
  309. self.labelTitle.text = strTitle;
  310. }
  311. @end
  312. @implementation CustomSliderMaxControl
  313. #pragma mark 加载页面
  314. - (void)resetControlFrame:(CGRect )rect
  315. tag:(int )tag
  316. title:(NSString *)title
  317. imageName:(NSString *)imageName
  318. highImageName:(NSString *)highImageName
  319. isImageLeft:(BOOL)isImageLeft
  320. hasLineVertical:(BOOL)hasLineVertical
  321. {
  322. self.frame = rect;
  323. self.tag = tag;
  324. [GlobalMethod setLabel:self.labelTitle widthLimit:0 numLines:1 fontNum:23 textColor:self.textColor text:@""];
  325. [self.labelTitle fitTitle:title variable:0];
  326. if (rect.size.width == 0) {
  327. self.width = self.labelTitle.width + 10*2;
  328. }
  329. self.labelTitle.center = CGPointMake(self.width/2.0, self.height/2.0);
  330. if (imageName != nil) {
  331. UIImage * image = [UIImage imageNamed:imageName];
  332. self.iv.image = image;
  333. self.iv.width = image.size.width;
  334. self.iv.height = image.size.height;
  335. if (highImageName != nil) {
  336. [self.iv setHighlightedImage:[UIImage imageNamed:highImageName]];
  337. }
  338. if (rect.size.width == 0) {
  339. self.width +=self.iv.width + 10;
  340. self.labelTitle.center = CGPointMake(self.width/2.0, self.height/2.0);
  341. }
  342. if (isImageLeft) {
  343. self.labelTitle.left += self.iv.width/2.0 + 10/2.0;
  344. self.iv.left = self.labelTitle.left - self.iv.width - 10;
  345. } else {
  346. self.labelTitle.left -= self.iv.width/2.0 + 10/2.0;
  347. self.iv.left = self.labelTitle.right + 10;
  348. }
  349. self.iv.centerY = self.height/2.0;
  350. }
  351. self.labelTitle.textColor = self.textColor;
  352. if (hasLineVertical) {
  353. [self addLineFrame:CGRectMake(self.width - 1, 4, 1, self.height - 8)];
  354. }
  355. }
  356. @end