HTSegmentedScrollView.m 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. //
  2. // HTSegmentedScrollView.m
  3. // HTSegmentedScrollViewDemo
  4. //
  5. // Created by iOS_zzy on 2018/3/28.
  6. // Copyright © 2018年 runThor. All rights reserved.
  7. //
  8. #import "HTSegmentedScrollView.h"
  9. @interface HTSegmentedScrollView () <UIScrollViewDelegate>
  10. @property (strong, nonatomic) UISegmentedControl *segmentedControl;
  11. @property (strong, nonatomic) UIView *segmentMarkView;
  12. @property (strong, nonatomic) UIScrollView *scrollView;
  13. @property(nonatomic, strong) NSMutableArray *itemArr;
  14. @end
  15. @implementation HTSegmentedScrollView
  16. - (void)addSegmentedItems:(NSArray *)items {
  17. // 分段控件
  18. self.itemArr = [NSMutableArray array];
  19. for (int i=0; i<items.count; i++) {
  20. UIButton *itemBtn = [[UIButton alloc] init];
  21. [itemBtn setTitle:items[i] forState:UIControlStateNormal];
  22. [itemBtn setTitleColor:RGB(119, 119, 119) forState:UIControlStateNormal];
  23. [itemBtn setTitleColor:RGB(59, 59, 59) forState:UIControlStateSelected];
  24. itemBtn.titleLabel.font = [UIFont systemFontOfSize:16];
  25. itemBtn.selected = (i == 0);
  26. [itemBtn addTarget:self action:@selector(handleItemEvent:) forControlEvents:UIControlEventTouchUpInside];
  27. [self addSubview:itemBtn];
  28. [self.itemArr addObject:itemBtn];
  29. }
  30. [self.itemArr mas_distributeViewsAlongAxis:MASAxisTypeHorizontal withFixedSpacing:0 leadSpacing:0 tailSpacing:0];
  31. [self.itemArr mas_makeConstraints:^(MASConstraintMaker *make) {
  32. make.height.equalTo(@22);
  33. make.top.equalTo(self).offset(10);
  34. }];
  35. }
  36. - (void)handleItemEvent:(UIButton *)sender {
  37. for (int i =0; i<self.itemArr.count; i++) {
  38. UIButton *item = self.itemArr[i];
  39. item.selected = NO;
  40. }
  41. int indexPath = [self.itemArr indexOfObject:sender];
  42. [UIView animateWithDuration:0.5 delay:0 usingSpringWithDamping:1 initialSpringVelocity:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
  43. [self.scrollView setContentOffset:CGPointMake(indexPath * self.scrollView.frame.size.width, 0)];
  44. } completion:nil];
  45. sender.selected = YES;
  46. }
  47. - (void)addScrollViews:(NSArray *)views {
  48. self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 35, self.frame.size.width, self.frame.size.height - 100)];
  49. self.scrollView.showsVerticalScrollIndicator = NO;
  50. self.scrollView.showsHorizontalScrollIndicator = NO;
  51. self.scrollView.bounces = NO;
  52. self.scrollView.pagingEnabled = YES;
  53. self.scrollView.delegate = self;
  54. self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width * views.count, self.scrollView.frame.size.height);
  55. [self addSubview:self.scrollView];
  56. for (int i = 0; i < views.count; i++) {
  57. if (![views[i] isKindOfClass:[UIView class]]) {
  58. return;
  59. }
  60. [views[i] setFrame:CGRectMake(self.scrollView.frame.size.width * i, 0, self.scrollView.frame.size.width, self.scrollView.frame.size.height)];
  61. [self.scrollView addSubview:views[i]];
  62. }
  63. }
  64. #pragma mark - Actions
  65. // 点击分段选项
  66. - (void)changeSegment:(UISegmentedControl *)segmentedControl {
  67. [UIView animateWithDuration:0.5 delay:0 usingSpringWithDamping:1 initialSpringVelocity:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
  68. self.segmentMarkView.transform = CGAffineTransformMakeTranslation((self.segmentedControl.frame.size.width/self.segmentedControl.numberOfSegments) * segmentedControl.selectedSegmentIndex, 0);
  69. [self.scrollView setContentOffset:CGPointMake(segmentedControl.selectedSegmentIndex * self.scrollView.frame.size.width, 0)];
  70. } completion:nil];
  71. }
  72. #pragma mark - UIScrollViewDelegate
  73. - (void)scrollViewDidScroll:(UIScrollView *)scrollView {
  74. self.segmentMarkView.transform = CGAffineTransformMakeTranslation(scrollView.contentOffset.x/scrollView.frame.size.width * (self.segmentedControl.frame.size.width/self.segmentedControl.numberOfSegments), 0);
  75. }
  76. - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
  77. [self scrollViewDidEndScrollingAnimation:scrollView];
  78. }
  79. - (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView {
  80. self.segmentedControl.selectedSegmentIndex = (int)(scrollView.contentOffset.x/scrollView.frame.size.width);
  81. }
  82. @end