├── GIF ├── 1-1.gif ├── 1-2.gif ├── 2-1.gif ├── 2-2.gif ├── 3.png ├── 4.png ├── 5.png └── 6.gif ├── README.md └── XLSlideSwitchExample ├── Example ├── CollectionViewController.h ├── CollectionViewController.m ├── Resource │ └── channelAdd.png ├── SegmentedSlideSwitchExample1.h ├── SegmentedSlideSwitchExample1.m ├── SegmentedSlideSwitchExample2.h ├── SegmentedSlideSwitchExample2.m ├── SlideSwitchExample1.h ├── SlideSwitchExample1.m ├── SlideSwitchExample2.h ├── SlideSwitchExample2.m ├── TableViewController.h └── TableViewController.m ├── XLSlideSwitch ├── XLSegmentedSlideSwitch.h ├── XLSegmentedSlideSwitch.m ├── XLSlideSegmentedItem.h ├── XLSlideSegmentedItem.m ├── XLSlideSwitch.h ├── XLSlideSwitch.m ├── XLSlideSwitchDelegate.h ├── XLSlideSwitchHeaderView.h └── XLSlideSwitchHeaderView.m ├── XLSlideSwitchExample.xcodeproj ├── project.pbxproj ├── project.xcworkspace │ ├── contents.xcworkspacedata │ ├── xcshareddata │ │ └── IDEWorkspaceChecks.plist │ └── xcuserdata │ │ └── MengXianLiang.xcuserdatad │ │ └── UserInterfaceState.xcuserstate └── xcuserdata │ └── MengXianLiang.xcuserdatad │ ├── xcdebugger │ └── Breakpoints_v2.xcbkptlist │ └── xcschemes │ ├── XLSlideSwitchExample.xcscheme │ └── xcschememanagement.plist └── XLSlideSwitchExample ├── AppDelegate.h ├── AppDelegate.m ├── Assets.xcassets └── AppIcon.appiconset │ └── Contents.json ├── Base.lproj ├── LaunchScreen.storyboard └── Main.storyboard ├── Info.plist ├── PrefixHeader.pch ├── ViewController.h ├── ViewController.m └── main.m /GIF/1-1.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mengxianliang/XLSlideSwitch/c61b0dc88b2e31f14f802fa09ffb296a38806725/GIF/1-1.gif -------------------------------------------------------------------------------- /GIF/1-2.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mengxianliang/XLSlideSwitch/c61b0dc88b2e31f14f802fa09ffb296a38806725/GIF/1-2.gif -------------------------------------------------------------------------------- /GIF/2-1.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mengxianliang/XLSlideSwitch/c61b0dc88b2e31f14f802fa09ffb296a38806725/GIF/2-1.gif -------------------------------------------------------------------------------- /GIF/2-2.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mengxianliang/XLSlideSwitch/c61b0dc88b2e31f14f802fa09ffb296a38806725/GIF/2-2.gif -------------------------------------------------------------------------------- /GIF/3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mengxianliang/XLSlideSwitch/c61b0dc88b2e31f14f802fa09ffb296a38806725/GIF/3.png -------------------------------------------------------------------------------- /GIF/4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mengxianliang/XLSlideSwitch/c61b0dc88b2e31f14f802fa09ffb296a38806725/GIF/4.png -------------------------------------------------------------------------------- /GIF/5.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mengxianliang/XLSlideSwitch/c61b0dc88b2e31f14f802fa09ffb296a38806725/GIF/5.png -------------------------------------------------------------------------------- /GIF/6.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mengxianliang/XLSlideSwitch/c61b0dc88b2e31f14f802fa09ffb296a38806725/GIF/6.gif -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | # 当前为老版本,新版本请移步[XLPageViewController](https://github.com/mengxianliang/XLPageViewController) 3 | 4 | # XLSlideSwitch 5 | 6 | ### 显示效果: 7 | 8 | | 样式 | 正常显示标题 | 在NavigationBar上显示标题 | 9 | | ---- | ---- | --- | 10 | |标准样式| | | 11 | |Segmented样式| | | 12 | 13 | 14 | ### 原理简介: 15 | 16 | 顶部的标题栏是利用*UICollectionview*实现的;底部视图控制器的切换是利用*UIPageViewController*实现的。 17 |
18 | 最大化的优化内存的使用,每个*ChildViewController*都是随着滚动加载的,避免了同时加载引起的UI卡顿。 19 | 20 | ### 使用方法: 21 | 22 | #### 1、创建数据源:titles、viewControllers 23 | 24 | ```objc 25 | //要显示的标题 26 | NSArray *titles = @[@"今天",@"是个",@"好日子",@"心想的",@"事儿",@"都能成",@"明天",@"是个",@"好日子",@"打开了家门",@"咱迎春风",@"~~~"]; 27 | //创建需要展示的ViewController 28 | NSMutableArray *viewControllers = [NSMutableArray new]; 29 | for (int i = 0 ; i 68 | 69 | 2、设置Segmented横向缩进 70 | ```objc 71 | _slideSwitch.horizontalInset = 50; 72 | ``` 73 | 74 | 75 | 3、设置更多按钮 76 | ```objc 77 | _slideSwitch.moreButton = [self moreButton]; 78 | ``` 79 | ```objc 80 | - (UIButton *)moreButton { 81 | UIButton *button = [[UIButton alloc] init]; 82 | [button setImage:[UIImage imageNamed:@"channelAdd"] forState:UIControlStateNormal]; 83 | [button setImageEdgeInsets:UIEdgeInsetsMake(8, 8, 8, 8)]; 84 | return button; 85 | } 86 | ``` 87 | 88 | 89 | 4、手动设置选中位置 90 | ```objc 91 | _slideSwitch.selectedIndex = 3; 92 | ``` 93 | 94 | 95 | 96 | ### 个人开发过的UI工具集合 [XLUIKit](https://github.com/mengxianliang/XLUIKit) 97 | -------------------------------------------------------------------------------- /XLSlideSwitchExample/Example/CollectionViewController.h: -------------------------------------------------------------------------------- 1 | // 2 | // CollectionViewController.h 3 | // XLSlideSwitchExample 4 | // 5 | // Created by MengXianLiang on 2017/5/18. 6 | // Copyright © 2017年 MengXianLiang. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface CollectionViewController : UIViewController 12 | 13 | @end 14 | -------------------------------------------------------------------------------- /XLSlideSwitchExample/Example/CollectionViewController.m: -------------------------------------------------------------------------------- 1 | // 2 | // CollectionViewController.m 3 | // XLSlideSwitchExample 4 | // 5 | // Created by MengXianLiang on 2017/5/18. 6 | // Copyright © 2017年 MengXianLiang. All rights reserved. 7 | // 8 | 9 | #import "CollectionViewController.h" 10 | #import "TableViewController.h" 11 | 12 | @interface CollectionViewController () { 13 | UICollectionView *_collectionView; 14 | } 15 | @end 16 | 17 | @implementation CollectionViewController 18 | 19 | - (void)viewDidLoad { 20 | [super viewDidLoad]; 21 | [self buildUI]; 22 | } 23 | 24 | - (void)buildUI { 25 | 26 | self.view.backgroundColor = [UIColor whiteColor]; 27 | CGFloat margin = 10.0f; 28 | CGFloat itemWidth = (self.view.bounds.size.width - 4*margin - 1)/3.0f; 29 | UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init]; 30 | layout.itemSize = CGSizeMake(itemWidth, itemWidth); 31 | layout.sectionInset = UIEdgeInsetsMake(margin, margin, margin, margin); 32 | 33 | _collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:layout]; 34 | _collectionView.delegate = self; 35 | _collectionView.dataSource = self; 36 | _collectionView.backgroundColor = [UIColor whiteColor]; 37 | [_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"UICollectionViewCell"]; 38 | [self.view addSubview:_collectionView]; 39 | } 40 | 41 | - (void)viewDidLayoutSubviews { 42 | [super viewDidLayoutSubviews]; 43 | _collectionView.frame = self.view.bounds; 44 | } 45 | 46 | #pragma mark - 47 | #pragma mark CollectionViewDataSource 48 | 49 | - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { 50 | return 30; 51 | } 52 | 53 | - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { 54 | static NSString *cellId = @"UICollectionViewCell"; 55 | UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellId forIndexPath:indexPath]; 56 | cell.backgroundColor = self.navigationController.navigationBar.tintColor; 57 | return cell; 58 | } 59 | 60 | - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath { 61 | TableViewController *vc = [[TableViewController alloc] init]; 62 | [self.navigationController pushViewController:vc animated:true]; 63 | 64 | } 65 | 66 | - (void)didReceiveMemoryWarning { 67 | [super didReceiveMemoryWarning]; 68 | } 69 | @end 70 | -------------------------------------------------------------------------------- /XLSlideSwitchExample/Example/Resource/channelAdd.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mengxianliang/XLSlideSwitch/c61b0dc88b2e31f14f802fa09ffb296a38806725/XLSlideSwitchExample/Example/Resource/channelAdd.png -------------------------------------------------------------------------------- /XLSlideSwitchExample/Example/SegmentedSlideSwitchExample1.h: -------------------------------------------------------------------------------- 1 | // 2 | // SegmentedSlideSwitchExample2.h 3 | // XLSlideSwitchDemo 4 | // 5 | // Created by Apple on 2017/1/9. 6 | // Copyright © 2017年 Apple. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface SegmentedSlideSwitchExample1 : UIViewController 12 | 13 | @end 14 | -------------------------------------------------------------------------------- /XLSlideSwitchExample/Example/SegmentedSlideSwitchExample1.m: -------------------------------------------------------------------------------- 1 | // 2 | // SegmentedSlideSwitchExample2.m 3 | // XLSlideSwitchDemo 4 | // 5 | // Created by Apple on 2017/1/9. 6 | // Copyright © 2017年 Apple. All rights reserved. 7 | // 8 | 9 | #import "SegmentedSlideSwitchExample1.h" 10 | #import "TableViewController.h" 11 | #import "CollectionViewController.h" 12 | 13 | #import "XLSegmentedSlideSwitch.h" 14 | 15 | @interface SegmentedSlideSwitchExample1 () 16 | 17 | @property (nonatomic, strong) XLSegmentedSlideSwitch *slideSwitch; 18 | 19 | @end 20 | 21 | @implementation SegmentedSlideSwitchExample1 22 | 23 | - (void)viewDidLoad { 24 | [super viewDidLoad]; 25 | [self buildUI]; 26 | } 27 | 28 | - (void)buildUI { 29 | 30 | self.view.backgroundColor = [UIColor whiteColor]; 31 | 32 | NSMutableArray *viewControllers = [NSMutableArray new]; 33 | NSArray *titles = @[@"今天",@"是个",@"好日子"]; 34 | for (int i = 0 ; i 10 | 11 | @interface SegmentedSlideSwitchExample2 : UIViewController 12 | 13 | @end 14 | -------------------------------------------------------------------------------- /XLSlideSwitchExample/Example/SegmentedSlideSwitchExample2.m: -------------------------------------------------------------------------------- 1 | // 2 | // SegmentedSlideSwitchExample2.m 3 | // XLSlideSwitchDemo 4 | // 5 | // Created by Apple on 2017/1/9. 6 | // Copyright © 2017年 Apple. All rights reserved. 7 | // 8 | 9 | #import "SegmentedSlideSwitchExample2.h" 10 | #import "TableViewController.h" 11 | #import "CollectionViewController.h" 12 | #import "XLSegmentedSlideSwitch.h" 13 | 14 | @interface SegmentedSlideSwitchExample2 () 15 | 16 | @property (nonatomic, strong) XLSegmentedSlideSwitch *slideSwitch; 17 | 18 | @end 19 | 20 | @implementation SegmentedSlideSwitchExample2 21 | 22 | - (void)viewDidLoad { 23 | [super viewDidLoad]; 24 | [self buildUI]; 25 | } 26 | 27 | - (void)buildUI { 28 | self.view.backgroundColor = [UIColor whiteColor]; 29 | 30 | NSMutableArray *viewControllers = [NSMutableArray new]; 31 | NSArray *titles = @[@"今天",@"是个",@"好日子"]; 32 | for (int i = 0 ; i 10 | 11 | @interface SlideSwitchExample1 : UIViewController 12 | 13 | @end 14 | -------------------------------------------------------------------------------- /XLSlideSwitchExample/Example/SlideSwitchExample1.m: -------------------------------------------------------------------------------- 1 | // 2 | // SlideSwitchExample1.m 3 | // XLSlideSwitchExample 4 | // 5 | // Created by MengXianLiang on 2017/5/2. 6 | // Copyright © 2017年 MengXianLiang. All rights reserved. 7 | // 8 | 9 | #import "SlideSwitchExample1.h" 10 | #import "TableViewController.h" 11 | #import "CollectionViewController.h" 12 | #import "XLSlideSwitch.h" 13 | 14 | @interface SlideSwitchExample1 () 15 | 16 | @property (nonatomic, strong) XLSlideSwitch *slideSwitch; 17 | 18 | @end 19 | 20 | @implementation SlideSwitchExample1 21 | 22 | - (void)viewDidLoad { 23 | [super viewDidLoad]; 24 | [self buildUI]; 25 | } 26 | 27 | - (void)buildUI { 28 | 29 | self.view.backgroundColor = [UIColor whiteColor]; 30 | 31 | self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Next" style:UIBarButtonItemStylePlain target:self action:@selector(showNextView)]; 32 | 33 | //要显示的标题 34 | NSArray *titles = @[@"今天",@"是个",@"好日子",@"心想的",@"事儿",@"都能成",@"明天",@"是个",@"好日子",@"打开了家门",@"咱迎春风",@"~~~"]; 35 | //创建需要展示的ViewController 36 | NSMutableArray *viewControllers = [[NSMutableArray alloc] init]; 37 | for (NSInteger i = 0 ; i 10 | 11 | @interface SlideSwitchExample2 : UIViewController 12 | 13 | @end 14 | -------------------------------------------------------------------------------- /XLSlideSwitchExample/Example/SlideSwitchExample2.m: -------------------------------------------------------------------------------- 1 | // 2 | // SlideSwitchExample2.m 3 | // XLSlideSwitchExample 4 | // 5 | // Created by MengXianLiang on 2017/5/2. 6 | // Copyright © 2017年 MengXianLiang. All rights reserved. 7 | // 8 | 9 | #import "SlideSwitchExample2.h" 10 | #import "TableViewController.h" 11 | #import "CollectionViewController.h" 12 | 13 | #import "XLSlideSwitch.h" 14 | 15 | @interface SlideSwitchExample2 () 16 | 17 | @property (nonatomic, strong) XLSlideSwitch *slideSwitch; 18 | 19 | @end 20 | 21 | @implementation SlideSwitchExample2 22 | 23 | - (void)viewDidLoad { 24 | [super viewDidLoad]; 25 | [self buildUI]; 26 | } 27 | 28 | - (void)buildUI { 29 | self.view.backgroundColor = [UIColor whiteColor]; 30 | 31 | //要显示的标题 32 | NSArray *titles = @[@"今天",@"是个",@"好日子",@"心想的",@"事儿",@"都能成",@"明天",@"是个",@"好日子",@"打开了家门",@"咱迎春风",@"~~~"]; 33 | //创建需要展示的ViewController 34 | NSMutableArray *viewControllers = [NSMutableArray new]; 35 | for (int i = 0 ; i 10 | 11 | @interface TableViewController : UIViewController 12 | 13 | @end 14 | -------------------------------------------------------------------------------- /XLSlideSwitchExample/Example/TableViewController.m: -------------------------------------------------------------------------------- 1 | // 2 | // TableViewController.m 3 | // XLSlideSwitchExample 4 | // 5 | // Created by MengXianLiang on 2017/5/18. 6 | // Copyright © 2017年 MengXianLiang. All rights reserved. 7 | // 8 | 9 | #import "TableViewController.h" 10 | #import "CollectionViewController.h" 11 | 12 | @interface TableViewController () 13 | { 14 | UITableView *_tableView; 15 | } 16 | @end 17 | 18 | @implementation TableViewController 19 | 20 | - (void)viewDidLoad { 21 | [super viewDidLoad]; 22 | [self buildUI]; 23 | } 24 | 25 | - (void)buildUI { 26 | self.view.backgroundColor = [UIColor whiteColor]; 27 | [self buildTable]; 28 | } 29 | 30 | - (void)buildTable { 31 | _tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain]; 32 | _tableView.delegate = self; 33 | _tableView.dataSource = self; 34 | _tableView.backgroundColor = [UIColor clearColor]; 35 | [self.view addSubview:_tableView]; 36 | } 37 | 38 | - (void)viewDidLayoutSubviews { 39 | [super viewDidLayoutSubviews]; 40 | _tableView.frame = self.view.bounds; 41 | } 42 | 43 | #pragma mark - 44 | #pragma mark TableViewDelegate&DataSource 45 | 46 | - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { 47 | return 60; 48 | } 49 | 50 | - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 51 | return 20; 52 | } 53 | 54 | 55 | - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 56 | NSString* cellIdentifier = @"cell"; 57 | UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 58 | if (cell == nil) { 59 | cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier]; 60 | cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 61 | } 62 | cell.textLabel.text = [NSString stringWithFormat:@"第%zd行",indexPath.row]; 63 | cell.detailTextLabel.text = @"点击跳转新界面"; 64 | cell.detailTextLabel.textColor = self.navigationController.navigationBar.tintColor; 65 | return cell; 66 | } 67 | 68 | - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 69 | CollectionViewController *vc = [[CollectionViewController alloc] init]; 70 | [self.navigationController pushViewController:vc animated:true]; 71 | } 72 | 73 | - (void)didReceiveMemoryWarning { 74 | [super didReceiveMemoryWarning]; 75 | 76 | } 77 | 78 | 79 | @end 80 | -------------------------------------------------------------------------------- /XLSlideSwitchExample/XLSlideSwitch/XLSegmentedSlideSwitch.h: -------------------------------------------------------------------------------- 1 | // 2 | // XLSegmentSlideSwitch.h 3 | // SlideSwitchTest 4 | // 5 | // Created by MengXianLiang on 2017/4/28. 6 | // Copyright © 2017年 MengXianLiang. All rights reserved. 7 | // 8 | 9 | #import 10 | #import "XLSlideSwitchDelegate.h" 11 | 12 | @interface XLSegmentedSlideSwitch : UIView 13 | /** 14 | * 需要显示的视图 15 | */ 16 | @property (nonatomic, strong) NSArray *viewControllers; 17 | /** 18 | * 标题 19 | */ 20 | @property (nonatomic, strong) NSArray *titles; 21 | /** 22 | * 选中位置 23 | */ 24 | @property (nonatomic, assign) NSInteger selectedIndex; 25 | /** 26 | * Segmented高亮颜色 27 | */ 28 | @property (nonatomic, strong) UIColor *tintColor; 29 | /** 30 | * segment水平缩进 31 | */ 32 | @property (nonatomic, assign) NSInteger horizontalInset; 33 | 34 | /** 35 | 代理方法 36 | */ 37 | @property (nonatomic, weak) id delegate; 38 | /** 39 | * 初始化方法 40 | */ 41 | -(instancetype)initWithFrame:(CGRect)frame Titles:(NSArray *)titles viewControllers:(NSArray *)viewControllers; 42 | /** 43 | * 标题显示在ViewController中 44 | */ 45 | -(void)showInViewController:(UIViewController *)viewController; 46 | /** 47 | * 标题显示在NavigationBar中 48 | */ 49 | -(void)showInNavigationController:(UINavigationController *)navigationController; 50 | @end 51 | -------------------------------------------------------------------------------- /XLSlideSwitchExample/XLSlideSwitch/XLSegmentedSlideSwitch.m: -------------------------------------------------------------------------------- 1 | // 2 | // XLSegmentSlideSwitch.m 3 | // SlideSwitchTest 4 | // 5 | // Created by MengXianLiang on 2017/4/28. 6 | // Copyright © 2017年 MengXianLiang. All rights reserved. 7 | // 8 | 9 | #import "XLSegmentedSlideSwitch.h" 10 | 11 | //顶部ScrollView高度 12 | static const CGFloat SegmentHeight = 40.0f; 13 | 14 | @interface XLSegmentedSlideSwitch () 15 | 16 | @property (nonatomic, strong) UISegmentedControl *segment; 17 | 18 | @property (nonatomic, strong) UIPageViewController *pageVC; 19 | 20 | @end 21 | 22 | @implementation XLSegmentedSlideSwitch 23 | 24 | - (instancetype)initWithFrame:(CGRect)frame Titles:(NSArray *)titles viewControllers:(NSArray *)viewControllers{ 25 | if (self = [super initWithFrame:frame]) { 26 | [self buildUI]; 27 | self.titles = titles; 28 | self.viewControllers = viewControllers; 29 | } 30 | return self; 31 | } 32 | 33 | - (void)buildUI { 34 | [self addSubview:[UIView new]]; 35 | //添加分段选择器 36 | _segment = [[UISegmentedControl alloc] initWithFrame:CGRectMake(5, 5, self.bounds.size.width - 10, SegmentHeight - 10)]; 37 | [_segment addTarget:self action:@selector(segmentValueChanged:) forControlEvents:UIControlEventValueChanged]; 38 | [self addSubview:_segment]; 39 | 40 | //添加分页滚动视图控制器 41 | _pageVC = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:nil]; 42 | _pageVC.view.frame = CGRectMake(0, SegmentHeight, self.bounds.size.width, self.bounds.size.height - SegmentHeight); 43 | _pageVC.delegate = self; 44 | _pageVC.dataSource = self; 45 | [self addSubview:_pageVC.view]; 46 | } 47 | 48 | - (void)showInViewController:(UIViewController *)viewController { 49 | [viewController addChildViewController:_pageVC]; 50 | [viewController.view addSubview:self]; 51 | } 52 | 53 | - (void)showInNavigationController:(UINavigationController *)navigationController { 54 | [navigationController.topViewController.view addSubview:self]; 55 | [navigationController.topViewController addChildViewController:_pageVC]; 56 | navigationController.topViewController.navigationItem.titleView = _segment; 57 | _pageVC.view.frame = self.bounds; 58 | _segment.backgroundColor = [UIColor clearColor]; 59 | } 60 | 61 | - (void)willMoveToSuperview:(UIView *)newSuperview { 62 | [super willMoveToSuperview:newSuperview]; 63 | [self switchToIndex:_selectedIndex]; 64 | } 65 | 66 | #pragma mark - 67 | #pragma mark Setter&Getter 68 | 69 | - (void)setViewControllers:(NSArray *)viewControllers { 70 | _viewControllers = viewControllers; 71 | } 72 | 73 | - (void)setTitles:(NSArray *)titles { 74 | 75 | _titles = titles; 76 | for (NSString *title in _titles) { 77 | [_segment insertSegmentWithTitle:title atIndex:_segment.numberOfSegments animated:false]; 78 | } 79 | _segment.selectedSegmentIndex = 0; 80 | } 81 | 82 | - (void)setTintColor:(UIColor *)tintColor { 83 | _segment.tintColor = tintColor; 84 | } 85 | 86 | - (void)setSelectedIndex:(NSInteger)selectedIndex { 87 | _selectedIndex = selectedIndex; 88 | _segment.selectedSegmentIndex = _selectedIndex; 89 | [self switchToIndex:_selectedIndex]; 90 | } 91 | 92 | - (void)setHorizontalInset:(NSInteger)horizontalInset { 93 | CGFloat marginY = 5; 94 | CGFloat marginX = horizontalInset; 95 | _segment.frame = CGRectMake(marginX, marginY, self.bounds.size.width - 2*marginX, SegmentHeight - marginY*2); 96 | } 97 | 98 | #pragma mark - 99 | #pragma mark SlideSegmentDelegate 100 | 101 | - (void)segmentValueChanged:(UISegmentedControl *)segment { 102 | NSInteger index = segment.selectedSegmentIndex; 103 | [self switchToIndex:index]; 104 | } 105 | 106 | #pragma mark - 107 | #pragma mark UIPageViewControllerDelegate&DataSource 108 | - (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController { 109 | UIViewController *vc; 110 | if (_selectedIndex + 1 < _viewControllers.count) { 111 | vc = _viewControllers[_selectedIndex + 1]; 112 | vc.view.bounds = pageViewController.view.bounds; 113 | } 114 | return vc; 115 | } 116 | 117 | - (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController { 118 | UIViewController *vc; 119 | if (_selectedIndex - 1 >= 0) { 120 | vc = _viewControllers[_selectedIndex - 1]; 121 | vc.view.bounds = pageViewController.view.bounds; 122 | } 123 | return vc; 124 | } 125 | 126 | - (void)pageViewController:(UIPageViewController *)pageViewController didFinishAnimating:(BOOL)finished previousViewControllers:(NSArray *)previousViewControllers transitionCompleted:(BOOL)completed { 127 | _selectedIndex = [_viewControllers indexOfObject:pageViewController.viewControllers.firstObject]; 128 | _segment.selectedSegmentIndex = _selectedIndex; 129 | [self performSwitchDelegateMethod]; 130 | } 131 | 132 | - (UIInterfaceOrientation)pageViewControllerPreferredInterfaceOrientationForPresentation:(UIPageViewController *)pageViewController NS_AVAILABLE_IOS(7_0) __TVOS_PROHIBITED { 133 | return UIInterfaceOrientationPortrait; 134 | } 135 | 136 | #pragma mark - 137 | #pragma mark 其他方法 138 | 139 | - (void)switchToIndex:(NSInteger)index { 140 | __weak __typeof(self)weekSelf = self; 141 | [_pageVC setViewControllers:@[_viewControllers[index]] direction:index<_selectedIndex animated:YES completion:^(BOOL finished) { 142 | _selectedIndex = index; 143 | [weekSelf performSwitchDelegateMethod]; 144 | }]; 145 | } 146 | 147 | //执行切换代理方法 148 | - (void)performSwitchDelegateMethod { 149 | if ([_delegate respondsToSelector:@selector(slideSwitchDidselectAtIndex:)]) { 150 | [_delegate slideSwitchDidselectAtIndex:_selectedIndex]; 151 | } 152 | } 153 | 154 | 155 | @end 156 | -------------------------------------------------------------------------------- /XLSlideSwitchExample/XLSlideSwitch/XLSlideSegmentedItem.h: -------------------------------------------------------------------------------- 1 | // 2 | // XLSlideSegmentItem.h 3 | // SlideSwitchTest 4 | // 5 | // Created by MengXianLiang on 2017/4/28. 6 | // Copyright © 2017年 MengXianLiang. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface XLSlideSegmentedItem : UICollectionViewCell 12 | 13 | @property (nonatomic, copy) UILabel *textLabel; 14 | 15 | @end 16 | -------------------------------------------------------------------------------- /XLSlideSwitchExample/XLSlideSwitch/XLSlideSegmentedItem.m: -------------------------------------------------------------------------------- 1 | // 2 | // XLSlideSegmentItem.m 3 | // SlideSwitchTest 4 | // 5 | // Created by MengXianLiang on 2017/4/28. 6 | // Copyright © 2017年 MengXianLiang. All rights reserved. 7 | // 8 | 9 | #import "XLSlideSegmentedItem.h" 10 | 11 | @implementation XLSlideSegmentedItem 12 | 13 | - (instancetype)initWithFrame:(CGRect)frame { 14 | if (self = [super initWithFrame:frame]) { 15 | [self buildUI]; 16 | } 17 | return self; 18 | } 19 | 20 | - (void)buildUI { 21 | _textLabel = [[UILabel alloc] init]; 22 | _textLabel.textAlignment = NSTextAlignmentCenter; 23 | [self.contentView addSubview:_textLabel]; 24 | } 25 | 26 | - (void)layoutSubviews { 27 | [super layoutSubviews]; 28 | _textLabel.frame = self.bounds; 29 | } 30 | 31 | @end 32 | -------------------------------------------------------------------------------- /XLSlideSwitchExample/XLSlideSwitch/XLSlideSwitch.h: -------------------------------------------------------------------------------- 1 | // 2 | // XLSlideSwitch.h 3 | // SlideSwitchTest 4 | // 5 | // Created by MengXianLiang on 2017/4/28. 6 | // Copyright © 2017年 MengXianLiang. All rights reserved. 7 | // 8 | 9 | #import 10 | #import "XLSlideSwitchHeaderView.h" 11 | #import "XLSlideSwitchDelegate.h" 12 | 13 | @interface XLSlideSwitch : UIView 14 | 15 | //视图控制器集合 16 | @property (nonatomic, strong) NSArray *viewControllers; 17 | //标题集合 18 | @property (nonatomic, strong) NSArray *titles; 19 | //选中位置 20 | @property (nonatomic, assign) NSInteger selectedIndex; 21 | //标题显示工具 22 | @property (nonatomic, strong) XLSlideSwitchHeaderView *headerView; 23 | /** 24 | * 代理方法 25 | */ 26 | @property (nonatomic, weak) id delegate; 27 | /** 28 | * 标题显示在ViewController中 29 | */ 30 | -(void)showInViewController:(UIViewController *)viewController; 31 | /** 32 | * 标题显示在NavigationBar中 33 | */ 34 | -(void)showInNavigationController:(UINavigationController *)navigationController; 35 | 36 | @end 37 | -------------------------------------------------------------------------------- /XLSlideSwitchExample/XLSlideSwitch/XLSlideSwitch.m: -------------------------------------------------------------------------------- 1 | // 2 | // XLSlideSwitch.m 3 | // SlideSwitchTest 4 | // 5 | // Created by MengXianLiang on 2017/4/28. 6 | // Copyright © 2017年 MengXianLiang. All rights reserved. 7 | // 8 | 9 | #import "XLSlideSwitch.h" 10 | 11 | //顶部ScrollView高度 12 | static const CGFloat SegmentHeight = 40.0f; 13 | 14 | @interface XLSlideSwitch () { 15 | BOOL _showHeaderViewInNaviagtionBar; 16 | } 17 | 18 | @property (nonatomic, strong) UIPageViewController *pageVC; 19 | 20 | @end 21 | 22 | @implementation XLSlideSwitch 23 | 24 | - (instancetype)init { 25 | if (self = [super init]) { 26 | [self buildUI]; 27 | } 28 | return self; 29 | } 30 | 31 | - (void)buildUI { 32 | [self addSubview:[UIView new]]; 33 | 34 | //添加标题栏 35 | _headerView = [[XLSlideSwitchHeaderView alloc] init]; 36 | _headerView.delegate = self; 37 | [self addSubview:_headerView]; 38 | 39 | //添加分页滚动视图控制器 40 | _pageVC = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:nil]; 41 | _pageVC.delegate = self; 42 | _pageVC.dataSource = self; 43 | [self addSubview:_pageVC.view]; 44 | 45 | //设置ScrollView代理 46 | for (UIScrollView *scrollView in _pageVC.view.subviews) { 47 | if ([scrollView isKindOfClass:[UIScrollView class]]) { 48 | scrollView.delegate = self; 49 | } 50 | } 51 | } 52 | 53 | - (void)layoutSubviews { 54 | [super layoutSubviews]; 55 | _headerView.frame = CGRectMake(0, 0, self.bounds.size.width, SegmentHeight); 56 | _pageVC.view.frame = CGRectMake(0, SegmentHeight, self.bounds.size.width, self.bounds.size.height - SegmentHeight); 57 | } 58 | 59 | - (void)showInViewController:(UIViewController *)viewController { 60 | [viewController addChildViewController:_pageVC]; 61 | [viewController.view addSubview:self]; 62 | } 63 | 64 | - (void)showInNavigationController:(UINavigationController *)navigationController { 65 | [navigationController.topViewController.view addSubview:self]; 66 | [navigationController.topViewController addChildViewController:_pageVC]; 67 | navigationController.topViewController.navigationItem.titleView = _headerView; 68 | _pageVC.view.frame = self.bounds; 69 | _headerView.showTitlesInNavBar = true; 70 | _showHeaderViewInNaviagtionBar = true; 71 | } 72 | 73 | - (void)willMoveToSuperview:(UIView *)newSuperview { 74 | [super willMoveToSuperview:newSuperview]; 75 | [self switchToIndex:_selectedIndex]; 76 | } 77 | 78 | #pragma mark - 79 | #pragma mark Setter&Getter 80 | 81 | - (void)setViewControllers:(NSArray *)viewControllers { 82 | _viewControllers = viewControllers; 83 | } 84 | 85 | - (void)setTitles:(NSArray *)titles { 86 | _titles = titles; 87 | _headerView.titles = titles; 88 | } 89 | 90 | - (void)setItemSelectedColor:(UIColor *)itemSelectedColor { 91 | _headerView.itemSelectedColor = itemSelectedColor; 92 | } 93 | 94 | - (void)setItemNormalColor:(UIColor *)itemNormalColor { 95 | _headerView.itemNormalColor = itemNormalColor; 96 | } 97 | 98 | - (void)setSelectedIndex:(NSInteger)selectedIndex { 99 | if (selectedIndex >= _titles.count) {return;} 100 | _selectedIndex = selectedIndex; 101 | [self switchToIndex:_selectedIndex]; 102 | _headerView.selectedIndex = _selectedIndex; 103 | _headerView.ignoreAnimation = true; 104 | } 105 | 106 | #pragma mark - 107 | #pragma mark SlideSegmentDelegate 108 | - (void)slideSegmentDidSelectedAtIndex:(NSInteger)index { 109 | if (index == _selectedIndex) {return;} 110 | [self switchToIndex:index]; 111 | } 112 | 113 | #pragma mark - 114 | #pragma mark UIPageViewControllerDelegate&DataSource 115 | - (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController { 116 | UIViewController *vc; 117 | if (_selectedIndex + 1 < _viewControllers.count) { 118 | vc = _viewControllers[_selectedIndex + 1]; 119 | vc.view.bounds = pageViewController.view.bounds; 120 | } 121 | return vc; 122 | } 123 | 124 | - (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController { 125 | UIViewController *vc; 126 | if (_selectedIndex - 1 >= 0) { 127 | vc = _viewControllers[_selectedIndex - 1]; 128 | vc.view.bounds = pageViewController.view.bounds; 129 | } 130 | return vc; 131 | } 132 | 133 | 134 | - (void)pageViewController:(UIPageViewController *)pageViewController didFinishAnimating:(BOOL)finished previousViewControllers:(NSArray *)previousViewControllers transitionCompleted:(BOOL)completed { 135 | _selectedIndex = [_viewControllers indexOfObject:pageViewController.viewControllers.firstObject]; 136 | _headerView.selectedIndex = _selectedIndex; 137 | [self performSwitchDelegateMethod]; 138 | } 139 | 140 | - (UIInterfaceOrientation)pageViewControllerPreferredInterfaceOrientationForPresentation:(UIPageViewController *)pageViewController NS_AVAILABLE_IOS(7_0) __TVOS_PROHIBITED { 141 | return UIInterfaceOrientationPortrait; 142 | } 143 | 144 | #pragma mark - 145 | #pragma mark ScrollViewDelegate 146 | -(void)scrollViewDidScroll:(UIScrollView *)scrollView{ 147 | if (scrollView.contentOffset.x == scrollView.bounds.size.width) {return;} 148 | CGFloat progress = scrollView.contentOffset.x/scrollView.bounds.size.width; 149 | _headerView.progress = progress; 150 | } 151 | 152 | - (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView { 153 | _headerView.ignoreAnimation = false; 154 | } 155 | 156 | #pragma mark - 157 | #pragma mark 其他方法 158 | - (void)switchToIndex:(NSInteger)index { 159 | __weak __typeof(self)weekSelf = self; 160 | [_pageVC setViewControllers:@[_viewControllers[index]] direction:index<_selectedIndex animated:YES completion:^(BOOL finished) { 161 | _selectedIndex = index; 162 | [weekSelf performSwitchDelegateMethod]; 163 | }]; 164 | } 165 | 166 | //执行切换代理方法 167 | - (void)performSwitchDelegateMethod { 168 | if ([_delegate respondsToSelector:@selector(slideSwitchDidselectAtIndex:)]) { 169 | [_delegate slideSwitchDidselectAtIndex:_selectedIndex]; 170 | } 171 | } 172 | 173 | @end 174 | -------------------------------------------------------------------------------- /XLSlideSwitchExample/XLSlideSwitch/XLSlideSwitchDelegate.h: -------------------------------------------------------------------------------- 1 | // 2 | // XLSlideSwitchDelegate.h 3 | // SlideSwitchTest 4 | // 5 | // Created by MengXianLiang on 2017/5/2. 6 | // Copyright © 2017年 MengXianLiang. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @protocol XLSlideSwitchDelegate 12 | 13 | @optional 14 | /** 15 | * 切换位置后的代理方法 16 | */ 17 | - (void)slideSwitchDidselectAtIndex:(NSInteger)index; 18 | 19 | @end 20 | -------------------------------------------------------------------------------- /XLSlideSwitchExample/XLSlideSwitch/XLSlideSwitchHeaderView.h: -------------------------------------------------------------------------------- 1 | // 2 | // XLSlideSegment.h 3 | // SlideSwitchTest 4 | // 5 | // Created by MengXianLiang on 2017/4/28. 6 | // Copyright © 2017年 MengXianLiang. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @protocol XLSlideSegmentDelegate 12 | 13 | - (void)slideSegmentDidSelectedAtIndex:(NSInteger)index; 14 | 15 | @end 16 | 17 | @interface XLSlideSwitchHeaderView : UIView 18 | 19 | @property (nonatomic, assign) NSInteger selectedIndex; 20 | 21 | @property (nonatomic, strong) NSArray *titles; 22 | 23 | @property (nonatomic, strong) UIColor *itemNormalColor; 24 | 25 | @property (nonatomic, strong) UIColor *itemSelectedColor; 26 | //在navigation上显示标题 27 | @property (nonatomic, assign) BOOL showTitlesInNavBar; 28 | //隐藏标题底部阴影 29 | @property (nonatomic, assign) BOOL hideShadow; 30 | //隐藏底部分割线 31 | @property (nonatomic, assign) BOOL hideBottomLine; 32 | //代理 33 | @property (nonatomic, weak) id delegate; 34 | //动画执行进度 35 | @property (nonatomic, assign) CGFloat progress; 36 | //忽略动画 37 | @property (nonatomic, assign) BOOL ignoreAnimation; 38 | /** 39 | * 用户自定义标题间距 40 | */ 41 | @property (nonatomic, assign) CGFloat customTitleSpacing; 42 | /** 43 | * 更多按钮 44 | */ 45 | @property (nonatomic, strong) UIButton *moreButton; 46 | 47 | @end 48 | -------------------------------------------------------------------------------- /XLSlideSwitchExample/XLSlideSwitch/XLSlideSwitchHeaderView.m: -------------------------------------------------------------------------------- 1 | // 2 | // XLSlideSegment.m 3 | // SlideSwitchTest 4 | // 5 | // Created by MengXianLiang on 2017/4/28. 6 | // Copyright © 2017年 MengXianLiang. All rights reserved. 7 | // 8 | 9 | #import "XLSlideSwitchHeaderView.h" 10 | #import "XLSlideSegmentedItem.h" 11 | 12 | //item间隔 13 | static const CGFloat ItemMargin = 10.0f; 14 | //button标题选中大小 15 | static const CGFloat ItemFontSize = 17.0f; 16 | //最大放大倍数 17 | static const CGFloat ItemMaxScale = 1.1; 18 | 19 | @interface XLSlideSwitchHeaderView () 20 | 21 | //标题Collectionview 22 | @property (nonatomic, strong) UICollectionView *collectionView; 23 | //底部分割线 24 | @property (nonatomic, strong) UIView *bottomLine; 25 | //标题底部阴影 26 | @property (nonatomic, strong) UIView *shadow; 27 | 28 | @end 29 | 30 | @implementation XLSlideSwitchHeaderView 31 | 32 | - (instancetype)init { 33 | if (self = [super init]) { 34 | [self buildUI]; 35 | } 36 | return self; 37 | } 38 | 39 | - (void)buildUI { 40 | 41 | self.backgroundColor = [UIColor colorWithRed:247.0f/255.0f green:246.0f/255.0f blue:245.0f/255.0f alpha:1]; 42 | 43 | [self addSubview:[UIView new]]; 44 | 45 | UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init]; 46 | layout.sectionInset = UIEdgeInsetsMake(0, ItemMargin, 0, ItemMargin); 47 | layout.scrollDirection = UICollectionViewScrollDirectionHorizontal; 48 | 49 | _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout]; 50 | _collectionView.delegate = self; 51 | _collectionView.dataSource = self; 52 | _collectionView.backgroundColor = [UIColor clearColor]; 53 | [_collectionView registerClass:[XLSlideSegmentedItem class] forCellWithReuseIdentifier:@"XLSlideSegmentedItem"]; 54 | _collectionView.showsHorizontalScrollIndicator = false; 55 | [self addSubview:_collectionView]; 56 | 57 | _shadow = [[UIView alloc] init]; 58 | [_collectionView addSubview:_shadow]; 59 | 60 | _bottomLine = [UIView new]; 61 | _bottomLine.backgroundColor = [UIColor colorWithRed:204.0f/255.0f green:204.0f/255.0f blue:204.0f/255.0f alpha:1]; 62 | [self addSubview:_bottomLine]; 63 | } 64 | 65 | - (void)layoutSubviews { 66 | [super layoutSubviews]; 67 | if (_moreButton) { 68 | CGFloat buttonWidth = self.bounds.size.height; 69 | CGFloat collectinWidth = self.bounds.size.width - buttonWidth; 70 | _moreButton.frame = CGRectMake(collectinWidth, 0, buttonWidth, buttonWidth); 71 | _collectionView.frame = CGRectMake(0, 0, collectinWidth, self.bounds.size.height); 72 | }else{ 73 | _collectionView.frame = self.bounds; 74 | } 75 | //如果标题过少 自动居中 76 | [_collectionView performBatchUpdates:nil completion:^(BOOL finished) { 77 | if (_collectionView.contentSize.width < _collectionView.bounds.size.width) { 78 | CGFloat insetX = (_collectionView.bounds.size.width - _collectionView.contentSize.width)/2.0f; 79 | _collectionView.contentInset = UIEdgeInsetsMake(0, insetX, 0, insetX); 80 | } 81 | }]; 82 | //设置阴影 83 | _shadow.backgroundColor = _itemSelectedColor; 84 | self.selectedIndex = _selectedIndex; 85 | _shadow.hidden = _hideShadow; 86 | _bottomLine.frame = CGRectMake(0, self.bounds.size.height - 0.5, self.bounds.size.width, 0.5); 87 | _bottomLine.hidden = _hideBottomLine; 88 | } 89 | 90 | #pragma mark - 91 | #pragma mark Setter 92 | - (void)setSelectedIndex:(NSInteger)selectedIndex { 93 | 94 | _selectedIndex = selectedIndex; 95 | 96 | //更新阴影位置(延迟是为了避免cell不在屏幕上显示时,造成的获取frame失败问题) 97 | CGFloat rectX = [self shadowRectOfIndex:_selectedIndex].origin.x; 98 | if (rectX <= 0) { 99 | dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.25 * NSEC_PER_SEC), dispatch_get_main_queue(), ^(void){ 100 | _shadow.frame = [self shadowRectOfIndex:_selectedIndex]; 101 | }); 102 | }else{ 103 | _shadow.frame = [self shadowRectOfIndex:_selectedIndex]; 104 | } 105 | 106 | //居中滚动标题 107 | [_collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForRow:_selectedIndex inSection:0] atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:true]; 108 | 109 | //更新字体大小 110 | [_collectionView reloadData]; 111 | 112 | //执行代理方法 113 | if ([_delegate respondsToSelector:@selector(slideSegmentDidSelectedAtIndex:)]) { 114 | [_delegate slideSegmentDidSelectedAtIndex:_selectedIndex]; 115 | } 116 | } 117 | 118 | - (void)setShowTitlesInNavBar:(BOOL)showTitlesInNavBar { 119 | _showTitlesInNavBar = showTitlesInNavBar; 120 | self.backgroundColor = [UIColor clearColor]; 121 | _hideBottomLine = true; 122 | _hideShadow = true; 123 | } 124 | 125 | //更新阴影位置 126 | - (void)setProgress:(CGFloat)progress { 127 | _progress = progress; 128 | //如果手动点击则不执行以下动画 129 | if (_ignoreAnimation) {return;} 130 | //更新阴影位置 131 | [self updateShadowPosition:progress]; 132 | //更新标题颜色、大小 133 | [self updateItem:progress]; 134 | } 135 | 136 | - (void)setCustomTitleSpacing:(CGFloat)customTitleSpacing { 137 | _customTitleSpacing = customTitleSpacing; 138 | [_collectionView reloadData]; 139 | 140 | } 141 | 142 | - (void)setMoreButton:(UIButton *)moreButton { 143 | _moreButton = moreButton; 144 | [self addSubview:moreButton]; 145 | } 146 | 147 | #pragma mark - 148 | #pragma mark 执行阴影过渡动画 149 | //更新阴影位置 150 | - (void)updateShadowPosition:(CGFloat)progress { 151 | 152 | //progress > 1 向左滑动表格反之向右滑动表格 153 | NSInteger nextIndex = progress > 1 ? _selectedIndex + 1 : _selectedIndex - 1; 154 | if (nextIndex < 0 || nextIndex == _titles.count) {return;} 155 | //获取当前阴影位置 156 | CGRect currentRect = [self shadowRectOfIndex:_selectedIndex]; 157 | CGRect nextRect = [self shadowRectOfIndex:nextIndex]; 158 | //如果在此时cell不在屏幕上 则不显示动画 159 | if (CGRectGetMinX(currentRect) <= 0 || CGRectGetMinX(nextRect) <= 0) {return;} 160 | 161 | progress = progress > 1 ? progress - 1 : 1 - progress; 162 | 163 | //更新宽度 164 | CGFloat width = currentRect.size.width + progress*(nextRect.size.width - currentRect.size.width); 165 | CGRect bounds = _shadow.bounds; 166 | bounds.size.width = width; 167 | _shadow.bounds = bounds; 168 | 169 | //更新位置 170 | CGFloat distance = CGRectGetMidX(nextRect) - CGRectGetMidX(currentRect); 171 | _shadow.center = CGPointMake(CGRectGetMidX(currentRect) + progress* distance, _shadow.center.y); 172 | } 173 | 174 | //更新标题颜色 175 | - (void)updateItem:(CGFloat)progress { 176 | 177 | NSInteger nextIndex = progress > 1 ? _selectedIndex + 1 : _selectedIndex - 1; 178 | if (nextIndex < 0 || nextIndex == _titles.count) {return;} 179 | 180 | XLSlideSegmentedItem *currentItem = (XLSlideSegmentedItem *)[_collectionView cellForItemAtIndexPath:[NSIndexPath indexPathForRow:_selectedIndex inSection:0]]; 181 | XLSlideSegmentedItem *nextItem = (XLSlideSegmentedItem *)[_collectionView cellForItemAtIndexPath:[NSIndexPath indexPathForRow:nextIndex inSection:0]]; 182 | progress = progress > 1 ? progress - 1 : 1 - progress; 183 | 184 | //更新颜色 185 | currentItem.textLabel.textColor = [self transformFromColor:_itemSelectedColor toColor:_itemNormalColor progress:progress]; 186 | nextItem.textLabel.textColor = [self transformFromColor:_itemNormalColor toColor:_itemSelectedColor progress:progress]; 187 | 188 | //更新放大 189 | CGFloat currentItemScale = ItemMaxScale - (ItemMaxScale - 1) * progress; 190 | CGFloat nextItemScale = 1 + (ItemMaxScale - 1) * progress; 191 | currentItem.transform = CGAffineTransformMakeScale(currentItemScale, currentItemScale); 192 | nextItem.transform = CGAffineTransformMakeScale(nextItemScale, nextItemScale); 193 | } 194 | 195 | #pragma mark - 196 | #pragma mark CollectionViewDelegate 197 | 198 | - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section { 199 | if (_customTitleSpacing) { 200 | return _customTitleSpacing; 201 | } 202 | return ItemMargin; 203 | } 204 | 205 | - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section { 206 | if (_customTitleSpacing) { 207 | return _customTitleSpacing; 208 | } 209 | return ItemMargin; 210 | } 211 | 212 | - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { 213 | return _titles.count; 214 | } 215 | 216 | - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath { 217 | return CGSizeMake([self itemWidthOfIndexPath:indexPath], _collectionView.bounds.size.height); 218 | } 219 | 220 | - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { 221 | XLSlideSegmentedItem *item = [collectionView dequeueReusableCellWithReuseIdentifier:@"XLSlideSegmentedItem" forIndexPath:indexPath]; 222 | item.textLabel.text = _titles[indexPath.row]; 223 | item.textLabel.font = [UIFont boldSystemFontOfSize:ItemFontSize]; 224 | 225 | CGFloat scale = indexPath.row == _selectedIndex ? ItemMaxScale : 1; 226 | item.transform = CGAffineTransformMakeScale(scale, scale); 227 | 228 | item.textLabel.textColor = indexPath.row == _selectedIndex ? _itemSelectedColor : _itemNormalColor; 229 | return item; 230 | } 231 | 232 | //获取文字宽度 233 | - (CGFloat)itemWidthOfIndexPath:(NSIndexPath*)indexPath { 234 | NSString *title = _titles[indexPath.row]; 235 | NSStringDrawingOptions opts = NSStringDrawingUsesLineFragmentOrigin | 236 | NSStringDrawingUsesFontLeading; 237 | NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init]; 238 | [style setLineBreakMode:NSLineBreakByTruncatingTail]; 239 | NSDictionary *attributes = @{ NSFontAttributeName : [UIFont boldSystemFontOfSize:ItemFontSize], NSParagraphStyleAttributeName : style }; 240 | CGSize textSize = [title boundingRectWithSize:CGSizeMake(self.bounds.size.width, self.bounds.size.height) 241 | options:opts 242 | attributes:attributes 243 | context:nil].size; 244 | return textSize.width; 245 | } 246 | 247 | 248 | - (CGRect)shadowRectOfIndex:(NSInteger)index { 249 | return CGRectMake([_collectionView cellForItemAtIndexPath:[NSIndexPath indexPathForRow:index inSection:0]].frame.origin.x, self.bounds.size.height - 2, [self itemWidthOfIndexPath:[NSIndexPath indexPathForRow:index inSection:0]], 2); 250 | } 251 | 252 | - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath { 253 | self.selectedIndex = indexPath.row; 254 | _ignoreAnimation = true; 255 | } 256 | 257 | #pragma mark - 258 | #pragma mark 功能性方法 259 | - (UIColor *)transformFromColor:(UIColor*)fromColor toColor:(UIColor *)toColor progress:(CGFloat)progress { 260 | 261 | if (!fromColor || !toColor) { 262 | NSLog(@"Warning !!! color is nil"); 263 | return [UIColor blackColor]; 264 | } 265 | 266 | progress = progress >= 1 ? 1 : progress; 267 | 268 | progress = progress <= 0 ? 0 : progress; 269 | 270 | const CGFloat * fromeComponents = CGColorGetComponents(fromColor.CGColor); 271 | 272 | const CGFloat * toComponents = CGColorGetComponents(toColor.CGColor); 273 | 274 | size_t fromColorNumber = CGColorGetNumberOfComponents(fromColor.CGColor); 275 | size_t toColorNumber = CGColorGetNumberOfComponents(toColor.CGColor); 276 | 277 | if (fromColorNumber == 2) { 278 | CGFloat white = fromeComponents[0]; 279 | fromColor = [UIColor colorWithRed:white green:white blue:white alpha:1]; 280 | fromeComponents = CGColorGetComponents(fromColor.CGColor); 281 | } 282 | 283 | if (toColorNumber == 2) { 284 | CGFloat white = toComponents[0]; 285 | toColor = [UIColor colorWithRed:white green:white blue:white alpha:1]; 286 | toComponents = CGColorGetComponents(toColor.CGColor); 287 | } 288 | 289 | CGFloat red = fromeComponents[0]*(1 - progress) + toComponents[0]*progress; 290 | CGFloat green = fromeComponents[1]*(1 - progress) + toComponents[1]*progress; 291 | CGFloat blue = fromeComponents[2]*(1 - progress) + toComponents[2]*progress; 292 | 293 | return [UIColor colorWithRed:red green:green blue:blue alpha:1]; 294 | } 295 | 296 | 297 | @end 298 | -------------------------------------------------------------------------------- /XLSlideSwitchExample/XLSlideSwitchExample.xcodeproj/project.pbxproj: -------------------------------------------------------------------------------- 1 | // !$*UTF8*$! 2 | { 3 | archiveVersion = 1; 4 | classes = { 5 | }; 6 | objectVersion = 46; 7 | objects = { 8 | 9 | /* Begin PBXBuildFile section */ 10 | 5E6724811EB88D8C00F4EF2E /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 5E6724801EB88D8C00F4EF2E /* main.m */; }; 11 | 5E6724841EB88D8C00F4EF2E /* AppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = 5E6724831EB88D8C00F4EF2E /* AppDelegate.m */; }; 12 | 5E6724871EB88D8C00F4EF2E /* ViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 5E6724861EB88D8C00F4EF2E /* ViewController.m */; }; 13 | 5E67248A1EB88D8C00F4EF2E /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 5E6724881EB88D8C00F4EF2E /* Main.storyboard */; }; 14 | 5E67248C1EB88D8C00F4EF2E /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 5E67248B1EB88D8C00F4EF2E /* Assets.xcassets */; }; 15 | 5E67248F1EB88D8C00F4EF2E /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 5E67248D1EB88D8C00F4EF2E /* LaunchScreen.storyboard */; }; 16 | 5E6724A11EB88DED00F4EF2E /* XLSegmentedSlideSwitch.m in Sources */ = {isa = PBXBuildFile; fileRef = 5E6724991EB88DED00F4EF2E /* XLSegmentedSlideSwitch.m */; }; 17 | 5E6724A21EB88DED00F4EF2E /* XLSlideSwitchHeaderView.m in Sources */ = {isa = PBXBuildFile; fileRef = 5E67249B1EB88DED00F4EF2E /* XLSlideSwitchHeaderView.m */; }; 18 | 5E6724A31EB88DED00F4EF2E /* XLSlideSegmentedItem.m in Sources */ = {isa = PBXBuildFile; fileRef = 5E67249D1EB88DED00F4EF2E /* XLSlideSegmentedItem.m */; }; 19 | 5E6724A41EB88DED00F4EF2E /* XLSlideSwitch.m in Sources */ = {isa = PBXBuildFile; fileRef = 5E67249F1EB88DED00F4EF2E /* XLSlideSwitch.m */; }; 20 | 5E6724A71EB88E1500F4EF2E /* SlideSwitchExample1.m in Sources */ = {isa = PBXBuildFile; fileRef = 5E6724A61EB88E1500F4EF2E /* SlideSwitchExample1.m */; }; 21 | 5E6724AA1EB88E2200F4EF2E /* SlideSwitchExample2.m in Sources */ = {isa = PBXBuildFile; fileRef = 5E6724A91EB88E2200F4EF2E /* SlideSwitchExample2.m */; }; 22 | 5E6724B51EB88E7600F4EF2E /* SegmentedSlideSwitchExample1.m in Sources */ = {isa = PBXBuildFile; fileRef = 5E6724B21EB88E7600F4EF2E /* SegmentedSlideSwitchExample1.m */; }; 23 | 5E6724B61EB88E7600F4EF2E /* SegmentedSlideSwitchExample2.m in Sources */ = {isa = PBXBuildFile; fileRef = 5E6724B41EB88E7600F4EF2E /* SegmentedSlideSwitchExample2.m */; }; 24 | 5E7C44B61ECD522000EA2372 /* TableViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 5E7C44B51ECD522000EA2372 /* TableViewController.m */; }; 25 | 5E7C44B91ECD522E00EA2372 /* CollectionViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 5E7C44B81ECD522E00EA2372 /* CollectionViewController.m */; }; 26 | 5EBB5A171FEE27AD0029DD41 /* channelAdd.png in Resources */ = {isa = PBXBuildFile; fileRef = 5EBB5A161FEE27AD0029DD41 /* channelAdd.png */; }; 27 | /* End PBXBuildFile section */ 28 | 29 | /* Begin PBXFileReference section */ 30 | 5E67247C1EB88D8C00F4EF2E /* XLSlideSwitchExample.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = XLSlideSwitchExample.app; sourceTree = BUILT_PRODUCTS_DIR; }; 31 | 5E6724801EB88D8C00F4EF2E /* main.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = main.m; sourceTree = ""; }; 32 | 5E6724821EB88D8C00F4EF2E /* AppDelegate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = AppDelegate.h; sourceTree = ""; }; 33 | 5E6724831EB88D8C00F4EF2E /* AppDelegate.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = AppDelegate.m; sourceTree = ""; }; 34 | 5E6724851EB88D8C00F4EF2E /* ViewController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = ViewController.h; sourceTree = ""; }; 35 | 5E6724861EB88D8C00F4EF2E /* ViewController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = ViewController.m; sourceTree = ""; }; 36 | 5E6724891EB88D8C00F4EF2E /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = ""; }; 37 | 5E67248B1EB88D8C00F4EF2E /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; 38 | 5E67248E1EB88D8C00F4EF2E /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = ""; }; 39 | 5E6724901EB88D8C00F4EF2E /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 40 | 5E6724981EB88DED00F4EF2E /* XLSegmentedSlideSwitch.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = XLSegmentedSlideSwitch.h; sourceTree = ""; }; 41 | 5E6724991EB88DED00F4EF2E /* XLSegmentedSlideSwitch.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = XLSegmentedSlideSwitch.m; sourceTree = ""; }; 42 | 5E67249A1EB88DED00F4EF2E /* XLSlideSwitchHeaderView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = XLSlideSwitchHeaderView.h; sourceTree = ""; }; 43 | 5E67249B1EB88DED00F4EF2E /* XLSlideSwitchHeaderView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = XLSlideSwitchHeaderView.m; sourceTree = ""; }; 44 | 5E67249C1EB88DED00F4EF2E /* XLSlideSegmentedItem.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = XLSlideSegmentedItem.h; sourceTree = ""; }; 45 | 5E67249D1EB88DED00F4EF2E /* XLSlideSegmentedItem.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = XLSlideSegmentedItem.m; sourceTree = ""; }; 46 | 5E67249E1EB88DED00F4EF2E /* XLSlideSwitch.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = XLSlideSwitch.h; sourceTree = ""; }; 47 | 5E67249F1EB88DED00F4EF2E /* XLSlideSwitch.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = XLSlideSwitch.m; sourceTree = ""; }; 48 | 5E6724A01EB88DED00F4EF2E /* XLSlideSwitchDelegate.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = XLSlideSwitchDelegate.h; sourceTree = ""; }; 49 | 5E6724A51EB88E1500F4EF2E /* SlideSwitchExample1.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SlideSwitchExample1.h; sourceTree = ""; }; 50 | 5E6724A61EB88E1500F4EF2E /* SlideSwitchExample1.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SlideSwitchExample1.m; sourceTree = ""; }; 51 | 5E6724A81EB88E2200F4EF2E /* SlideSwitchExample2.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SlideSwitchExample2.h; sourceTree = ""; }; 52 | 5E6724A91EB88E2200F4EF2E /* SlideSwitchExample2.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SlideSwitchExample2.m; sourceTree = ""; }; 53 | 5E6724B11EB88E7600F4EF2E /* SegmentedSlideSwitchExample1.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SegmentedSlideSwitchExample1.h; sourceTree = ""; }; 54 | 5E6724B21EB88E7600F4EF2E /* SegmentedSlideSwitchExample1.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SegmentedSlideSwitchExample1.m; sourceTree = ""; }; 55 | 5E6724B31EB88E7600F4EF2E /* SegmentedSlideSwitchExample2.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SegmentedSlideSwitchExample2.h; sourceTree = ""; }; 56 | 5E6724B41EB88E7600F4EF2E /* SegmentedSlideSwitchExample2.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SegmentedSlideSwitchExample2.m; sourceTree = ""; }; 57 | 5E6724B71EB88F3E00F4EF2E /* PrefixHeader.pch */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = PrefixHeader.pch; sourceTree = ""; }; 58 | 5E7C44B41ECD522000EA2372 /* TableViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TableViewController.h; sourceTree = ""; }; 59 | 5E7C44B51ECD522000EA2372 /* TableViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = TableViewController.m; sourceTree = ""; }; 60 | 5E7C44B71ECD522E00EA2372 /* CollectionViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CollectionViewController.h; sourceTree = ""; }; 61 | 5E7C44B81ECD522E00EA2372 /* CollectionViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = CollectionViewController.m; sourceTree = ""; }; 62 | 5EBB5A161FEE27AD0029DD41 /* channelAdd.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = channelAdd.png; sourceTree = ""; }; 63 | /* End PBXFileReference section */ 64 | 65 | /* Begin PBXFrameworksBuildPhase section */ 66 | 5E6724791EB88D8C00F4EF2E /* Frameworks */ = { 67 | isa = PBXFrameworksBuildPhase; 68 | buildActionMask = 2147483647; 69 | files = ( 70 | ); 71 | runOnlyForDeploymentPostprocessing = 0; 72 | }; 73 | /* End PBXFrameworksBuildPhase section */ 74 | 75 | /* Begin PBXGroup section */ 76 | 5E6724731EB88D8C00F4EF2E = { 77 | isa = PBXGroup; 78 | children = ( 79 | 5E6724971EB88DD200F4EF2E /* XLSlideSwitch */, 80 | 5E6724961EB88DD200F4EF2E /* Example */, 81 | 5E67247E1EB88D8C00F4EF2E /* XLSlideSwitchExample */, 82 | 5E67247D1EB88D8C00F4EF2E /* Products */, 83 | ); 84 | sourceTree = ""; 85 | }; 86 | 5E67247D1EB88D8C00F4EF2E /* Products */ = { 87 | isa = PBXGroup; 88 | children = ( 89 | 5E67247C1EB88D8C00F4EF2E /* XLSlideSwitchExample.app */, 90 | ); 91 | name = Products; 92 | sourceTree = ""; 93 | }; 94 | 5E67247E1EB88D8C00F4EF2E /* XLSlideSwitchExample */ = { 95 | isa = PBXGroup; 96 | children = ( 97 | 5E6724821EB88D8C00F4EF2E /* AppDelegate.h */, 98 | 5E6724831EB88D8C00F4EF2E /* AppDelegate.m */, 99 | 5E6724851EB88D8C00F4EF2E /* ViewController.h */, 100 | 5E6724861EB88D8C00F4EF2E /* ViewController.m */, 101 | 5E6724881EB88D8C00F4EF2E /* Main.storyboard */, 102 | 5E67248B1EB88D8C00F4EF2E /* Assets.xcassets */, 103 | 5E67248D1EB88D8C00F4EF2E /* LaunchScreen.storyboard */, 104 | 5E6724901EB88D8C00F4EF2E /* Info.plist */, 105 | 5E67247F1EB88D8C00F4EF2E /* Supporting Files */, 106 | ); 107 | path = XLSlideSwitchExample; 108 | sourceTree = ""; 109 | }; 110 | 5E67247F1EB88D8C00F4EF2E /* Supporting Files */ = { 111 | isa = PBXGroup; 112 | children = ( 113 | 5E6724801EB88D8C00F4EF2E /* main.m */, 114 | 5E6724B71EB88F3E00F4EF2E /* PrefixHeader.pch */, 115 | ); 116 | name = "Supporting Files"; 117 | sourceTree = ""; 118 | }; 119 | 5E6724961EB88DD200F4EF2E /* Example */ = { 120 | isa = PBXGroup; 121 | children = ( 122 | 5EBB5A151FEE27A70029DD41 /* Resource */, 123 | 5E7C44BD1ECD525F00EA2372 /* ExampleViewController */, 124 | 5E7C44BE1ECD527500EA2372 /* Support */, 125 | ); 126 | path = Example; 127 | sourceTree = ""; 128 | }; 129 | 5E6724971EB88DD200F4EF2E /* XLSlideSwitch */ = { 130 | isa = PBXGroup; 131 | children = ( 132 | 5E67249E1EB88DED00F4EF2E /* XLSlideSwitch.h */, 133 | 5E67249F1EB88DED00F4EF2E /* XLSlideSwitch.m */, 134 | 5E6724A01EB88DED00F4EF2E /* XLSlideSwitchDelegate.h */, 135 | 5E67249A1EB88DED00F4EF2E /* XLSlideSwitchHeaderView.h */, 136 | 5E67249B1EB88DED00F4EF2E /* XLSlideSwitchHeaderView.m */, 137 | 5E67249C1EB88DED00F4EF2E /* XLSlideSegmentedItem.h */, 138 | 5E67249D1EB88DED00F4EF2E /* XLSlideSegmentedItem.m */, 139 | 5E6724981EB88DED00F4EF2E /* XLSegmentedSlideSwitch.h */, 140 | 5E6724991EB88DED00F4EF2E /* XLSegmentedSlideSwitch.m */, 141 | ); 142 | path = XLSlideSwitch; 143 | sourceTree = ""; 144 | }; 145 | 5E7C44BD1ECD525F00EA2372 /* ExampleViewController */ = { 146 | isa = PBXGroup; 147 | children = ( 148 | 5E6724A51EB88E1500F4EF2E /* SlideSwitchExample1.h */, 149 | 5E6724A61EB88E1500F4EF2E /* SlideSwitchExample1.m */, 150 | 5E6724A81EB88E2200F4EF2E /* SlideSwitchExample2.h */, 151 | 5E6724A91EB88E2200F4EF2E /* SlideSwitchExample2.m */, 152 | 5E6724B11EB88E7600F4EF2E /* SegmentedSlideSwitchExample1.h */, 153 | 5E6724B21EB88E7600F4EF2E /* SegmentedSlideSwitchExample1.m */, 154 | 5E6724B31EB88E7600F4EF2E /* SegmentedSlideSwitchExample2.h */, 155 | 5E6724B41EB88E7600F4EF2E /* SegmentedSlideSwitchExample2.m */, 156 | ); 157 | name = ExampleViewController; 158 | sourceTree = ""; 159 | }; 160 | 5E7C44BE1ECD527500EA2372 /* Support */ = { 161 | isa = PBXGroup; 162 | children = ( 163 | 5E7C44B41ECD522000EA2372 /* TableViewController.h */, 164 | 5E7C44B51ECD522000EA2372 /* TableViewController.m */, 165 | 5E7C44B71ECD522E00EA2372 /* CollectionViewController.h */, 166 | 5E7C44B81ECD522E00EA2372 /* CollectionViewController.m */, 167 | ); 168 | name = Support; 169 | sourceTree = ""; 170 | }; 171 | 5EBB5A151FEE27A70029DD41 /* Resource */ = { 172 | isa = PBXGroup; 173 | children = ( 174 | 5EBB5A161FEE27AD0029DD41 /* channelAdd.png */, 175 | ); 176 | path = Resource; 177 | sourceTree = ""; 178 | }; 179 | /* End PBXGroup section */ 180 | 181 | /* Begin PBXNativeTarget section */ 182 | 5E67247B1EB88D8C00F4EF2E /* XLSlideSwitchExample */ = { 183 | isa = PBXNativeTarget; 184 | buildConfigurationList = 5E6724931EB88D8C00F4EF2E /* Build configuration list for PBXNativeTarget "XLSlideSwitchExample" */; 185 | buildPhases = ( 186 | 5E6724781EB88D8C00F4EF2E /* Sources */, 187 | 5E6724791EB88D8C00F4EF2E /* Frameworks */, 188 | 5E67247A1EB88D8C00F4EF2E /* Resources */, 189 | ); 190 | buildRules = ( 191 | ); 192 | dependencies = ( 193 | ); 194 | name = XLSlideSwitchExample; 195 | productName = XLSlideSwitchExample; 196 | productReference = 5E67247C1EB88D8C00F4EF2E /* XLSlideSwitchExample.app */; 197 | productType = "com.apple.product-type.application"; 198 | }; 199 | /* End PBXNativeTarget section */ 200 | 201 | /* Begin PBXProject section */ 202 | 5E6724741EB88D8C00F4EF2E /* Project object */ = { 203 | isa = PBXProject; 204 | attributes = { 205 | LastUpgradeCheck = 0830; 206 | ORGANIZATIONNAME = MengXianLiang; 207 | TargetAttributes = { 208 | 5E67247B1EB88D8C00F4EF2E = { 209 | CreatedOnToolsVersion = 8.3.1; 210 | DevelopmentTeam = C78VF44FUP; 211 | ProvisioningStyle = Manual; 212 | }; 213 | }; 214 | }; 215 | buildConfigurationList = 5E6724771EB88D8C00F4EF2E /* Build configuration list for PBXProject "XLSlideSwitchExample" */; 216 | compatibilityVersion = "Xcode 3.2"; 217 | developmentRegion = English; 218 | hasScannedForEncodings = 0; 219 | knownRegions = ( 220 | English, 221 | en, 222 | Base, 223 | ); 224 | mainGroup = 5E6724731EB88D8C00F4EF2E; 225 | productRefGroup = 5E67247D1EB88D8C00F4EF2E /* Products */; 226 | projectDirPath = ""; 227 | projectRoot = ""; 228 | targets = ( 229 | 5E67247B1EB88D8C00F4EF2E /* XLSlideSwitchExample */, 230 | ); 231 | }; 232 | /* End PBXProject section */ 233 | 234 | /* Begin PBXResourcesBuildPhase section */ 235 | 5E67247A1EB88D8C00F4EF2E /* Resources */ = { 236 | isa = PBXResourcesBuildPhase; 237 | buildActionMask = 2147483647; 238 | files = ( 239 | 5E67248F1EB88D8C00F4EF2E /* LaunchScreen.storyboard in Resources */, 240 | 5E67248C1EB88D8C00F4EF2E /* Assets.xcassets in Resources */, 241 | 5E67248A1EB88D8C00F4EF2E /* Main.storyboard in Resources */, 242 | 5EBB5A171FEE27AD0029DD41 /* channelAdd.png in Resources */, 243 | ); 244 | runOnlyForDeploymentPostprocessing = 0; 245 | }; 246 | /* End PBXResourcesBuildPhase section */ 247 | 248 | /* Begin PBXSourcesBuildPhase section */ 249 | 5E6724781EB88D8C00F4EF2E /* Sources */ = { 250 | isa = PBXSourcesBuildPhase; 251 | buildActionMask = 2147483647; 252 | files = ( 253 | 5E6724A71EB88E1500F4EF2E /* SlideSwitchExample1.m in Sources */, 254 | 5E6724B61EB88E7600F4EF2E /* SegmentedSlideSwitchExample2.m in Sources */, 255 | 5E6724871EB88D8C00F4EF2E /* ViewController.m in Sources */, 256 | 5E6724841EB88D8C00F4EF2E /* AppDelegate.m in Sources */, 257 | 5E6724B51EB88E7600F4EF2E /* SegmentedSlideSwitchExample1.m in Sources */, 258 | 5E7C44B61ECD522000EA2372 /* TableViewController.m in Sources */, 259 | 5E6724A31EB88DED00F4EF2E /* XLSlideSegmentedItem.m in Sources */, 260 | 5E7C44B91ECD522E00EA2372 /* CollectionViewController.m in Sources */, 261 | 5E6724A41EB88DED00F4EF2E /* XLSlideSwitch.m in Sources */, 262 | 5E6724AA1EB88E2200F4EF2E /* SlideSwitchExample2.m in Sources */, 263 | 5E6724A11EB88DED00F4EF2E /* XLSegmentedSlideSwitch.m in Sources */, 264 | 5E6724A21EB88DED00F4EF2E /* XLSlideSwitchHeaderView.m in Sources */, 265 | 5E6724811EB88D8C00F4EF2E /* main.m in Sources */, 266 | ); 267 | runOnlyForDeploymentPostprocessing = 0; 268 | }; 269 | /* End PBXSourcesBuildPhase section */ 270 | 271 | /* Begin PBXVariantGroup section */ 272 | 5E6724881EB88D8C00F4EF2E /* Main.storyboard */ = { 273 | isa = PBXVariantGroup; 274 | children = ( 275 | 5E6724891EB88D8C00F4EF2E /* Base */, 276 | ); 277 | name = Main.storyboard; 278 | sourceTree = ""; 279 | }; 280 | 5E67248D1EB88D8C00F4EF2E /* LaunchScreen.storyboard */ = { 281 | isa = PBXVariantGroup; 282 | children = ( 283 | 5E67248E1EB88D8C00F4EF2E /* Base */, 284 | ); 285 | name = LaunchScreen.storyboard; 286 | sourceTree = ""; 287 | }; 288 | /* End PBXVariantGroup section */ 289 | 290 | /* Begin XCBuildConfiguration section */ 291 | 5E6724911EB88D8C00F4EF2E /* Debug */ = { 292 | isa = XCBuildConfiguration; 293 | buildSettings = { 294 | ALWAYS_SEARCH_USER_PATHS = NO; 295 | CLANG_ANALYZER_NONNULL = YES; 296 | CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; 297 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 298 | CLANG_CXX_LIBRARY = "libc++"; 299 | CLANG_ENABLE_MODULES = YES; 300 | CLANG_ENABLE_OBJC_ARC = YES; 301 | CLANG_WARN_BOOL_CONVERSION = YES; 302 | CLANG_WARN_CONSTANT_CONVERSION = YES; 303 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 304 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 305 | CLANG_WARN_EMPTY_BODY = YES; 306 | CLANG_WARN_ENUM_CONVERSION = YES; 307 | CLANG_WARN_INFINITE_RECURSION = YES; 308 | CLANG_WARN_INT_CONVERSION = YES; 309 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 310 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 311 | CLANG_WARN_UNREACHABLE_CODE = YES; 312 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 313 | CODE_SIGN_IDENTITY = "iPhone Developer: xianliang meng (79SX5888C3)"; 314 | CODE_SIGN_STYLE = Manual; 315 | COPY_PHASE_STRIP = NO; 316 | DEBUG_INFORMATION_FORMAT = dwarf; 317 | ENABLE_STRICT_OBJC_MSGSEND = YES; 318 | ENABLE_TESTABILITY = YES; 319 | GCC_C_LANGUAGE_STANDARD = gnu99; 320 | GCC_DYNAMIC_NO_PIC = NO; 321 | GCC_NO_COMMON_BLOCKS = YES; 322 | GCC_OPTIMIZATION_LEVEL = 0; 323 | GCC_PREPROCESSOR_DEFINITIONS = ( 324 | "DEBUG=1", 325 | "$(inherited)", 326 | ); 327 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 328 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 329 | GCC_WARN_UNDECLARED_SELECTOR = YES; 330 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 331 | GCC_WARN_UNUSED_FUNCTION = YES; 332 | GCC_WARN_UNUSED_VARIABLE = YES; 333 | IPHONEOS_DEPLOYMENT_TARGET = 10.3; 334 | MTL_ENABLE_DEBUG_INFO = YES; 335 | ONLY_ACTIVE_ARCH = YES; 336 | PROVISIONING_PROFILE = "63d0bee2-4f74-4b71-a719-620d70b5a076"; 337 | SDKROOT = iphoneos; 338 | }; 339 | name = Debug; 340 | }; 341 | 5E6724921EB88D8C00F4EF2E /* Release */ = { 342 | isa = XCBuildConfiguration; 343 | buildSettings = { 344 | ALWAYS_SEARCH_USER_PATHS = NO; 345 | CLANG_ANALYZER_NONNULL = YES; 346 | CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; 347 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 348 | CLANG_CXX_LIBRARY = "libc++"; 349 | CLANG_ENABLE_MODULES = YES; 350 | CLANG_ENABLE_OBJC_ARC = YES; 351 | CLANG_WARN_BOOL_CONVERSION = YES; 352 | CLANG_WARN_CONSTANT_CONVERSION = YES; 353 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 354 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 355 | CLANG_WARN_EMPTY_BODY = YES; 356 | CLANG_WARN_ENUM_CONVERSION = YES; 357 | CLANG_WARN_INFINITE_RECURSION = YES; 358 | CLANG_WARN_INT_CONVERSION = YES; 359 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 360 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 361 | CLANG_WARN_UNREACHABLE_CODE = YES; 362 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 363 | CODE_SIGN_IDENTITY = "iPhone Developer: xianliang meng (79SX5888C3)"; 364 | CODE_SIGN_STYLE = Manual; 365 | COPY_PHASE_STRIP = NO; 366 | DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; 367 | ENABLE_NS_ASSERTIONS = NO; 368 | ENABLE_STRICT_OBJC_MSGSEND = YES; 369 | GCC_C_LANGUAGE_STANDARD = gnu99; 370 | GCC_NO_COMMON_BLOCKS = YES; 371 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 372 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 373 | GCC_WARN_UNDECLARED_SELECTOR = YES; 374 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 375 | GCC_WARN_UNUSED_FUNCTION = YES; 376 | GCC_WARN_UNUSED_VARIABLE = YES; 377 | IPHONEOS_DEPLOYMENT_TARGET = 10.3; 378 | MTL_ENABLE_DEBUG_INFO = NO; 379 | PROVISIONING_PROFILE = "63d0bee2-4f74-4b71-a719-620d70b5a076"; 380 | SDKROOT = iphoneos; 381 | VALIDATE_PRODUCT = YES; 382 | }; 383 | name = Release; 384 | }; 385 | 5E6724941EB88D8C00F4EF2E /* Debug */ = { 386 | isa = XCBuildConfiguration; 387 | buildSettings = { 388 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 389 | CODE_SIGN_IDENTITY = "iPhone Developer: CHEN LI (L89D9YKKEN)"; 390 | CODE_SIGN_STYLE = Manual; 391 | DEVELOPMENT_TEAM = C78VF44FUP; 392 | GCC_PRECOMPILE_PREFIX_HEADER = YES; 393 | GCC_PREFIX_HEADER = "$(SRCROOT)/XLSlideSwitchExample/PrefixHeader.pch"; 394 | INFOPLIST_FILE = XLSlideSwitchExample/Info.plist; 395 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; 396 | PRODUCT_BUNDLE_IDENTIFIER = com.jwzt.XLSlideSwitchExample; 397 | PRODUCT_NAME = "$(TARGET_NAME)"; 398 | PROVISIONING_PROFILE_SPECIFIER = jwztTeamDevelop; 399 | }; 400 | name = Debug; 401 | }; 402 | 5E6724951EB88D8C00F4EF2E /* Release */ = { 403 | isa = XCBuildConfiguration; 404 | buildSettings = { 405 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 406 | CODE_SIGN_IDENTITY = "iPhone Developer: CHEN LI (L89D9YKKEN)"; 407 | CODE_SIGN_STYLE = Manual; 408 | DEVELOPMENT_TEAM = C78VF44FUP; 409 | GCC_PRECOMPILE_PREFIX_HEADER = YES; 410 | GCC_PREFIX_HEADER = "$(SRCROOT)/XLSlideSwitchExample/PrefixHeader.pch"; 411 | INFOPLIST_FILE = XLSlideSwitchExample/Info.plist; 412 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; 413 | PRODUCT_BUNDLE_IDENTIFIER = com.jwzt.XLSlideSwitchExample; 414 | PRODUCT_NAME = "$(TARGET_NAME)"; 415 | PROVISIONING_PROFILE_SPECIFIER = jwztTeamDevelop; 416 | }; 417 | name = Release; 418 | }; 419 | /* End XCBuildConfiguration section */ 420 | 421 | /* Begin XCConfigurationList section */ 422 | 5E6724771EB88D8C00F4EF2E /* Build configuration list for PBXProject "XLSlideSwitchExample" */ = { 423 | isa = XCConfigurationList; 424 | buildConfigurations = ( 425 | 5E6724911EB88D8C00F4EF2E /* Debug */, 426 | 5E6724921EB88D8C00F4EF2E /* Release */, 427 | ); 428 | defaultConfigurationIsVisible = 0; 429 | defaultConfigurationName = Release; 430 | }; 431 | 5E6724931EB88D8C00F4EF2E /* Build configuration list for PBXNativeTarget "XLSlideSwitchExample" */ = { 432 | isa = XCConfigurationList; 433 | buildConfigurations = ( 434 | 5E6724941EB88D8C00F4EF2E /* Debug */, 435 | 5E6724951EB88D8C00F4EF2E /* Release */, 436 | ); 437 | defaultConfigurationIsVisible = 0; 438 | defaultConfigurationName = Release; 439 | }; 440 | /* End XCConfigurationList section */ 441 | }; 442 | rootObject = 5E6724741EB88D8C00F4EF2E /* Project object */; 443 | } 444 | -------------------------------------------------------------------------------- /XLSlideSwitchExample/XLSlideSwitchExample.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /XLSlideSwitchExample/XLSlideSwitchExample.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | IDEDidComputeMac32BitWarning 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /XLSlideSwitchExample/XLSlideSwitchExample.xcodeproj/project.xcworkspace/xcuserdata/MengXianLiang.xcuserdatad/UserInterfaceState.xcuserstate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mengxianliang/XLSlideSwitch/c61b0dc88b2e31f14f802fa09ffb296a38806725/XLSlideSwitchExample/XLSlideSwitchExample.xcodeproj/project.xcworkspace/xcuserdata/MengXianLiang.xcuserdatad/UserInterfaceState.xcuserstate -------------------------------------------------------------------------------- /XLSlideSwitchExample/XLSlideSwitchExample.xcodeproj/xcuserdata/MengXianLiang.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | -------------------------------------------------------------------------------- /XLSlideSwitchExample/XLSlideSwitchExample.xcodeproj/xcuserdata/MengXianLiang.xcuserdatad/xcschemes/XLSlideSwitchExample.xcscheme: -------------------------------------------------------------------------------- 1 | 2 | 5 | 8 | 9 | 15 | 21 | 22 | 23 | 24 | 25 | 30 | 31 | 32 | 33 | 39 | 40 | 41 | 42 | 43 | 44 | 54 | 56 | 62 | 63 | 64 | 65 | 66 | 67 | 73 | 75 | 81 | 82 | 83 | 84 | 86 | 87 | 90 | 91 | 92 | -------------------------------------------------------------------------------- /XLSlideSwitchExample/XLSlideSwitchExample.xcodeproj/xcuserdata/MengXianLiang.xcuserdatad/xcschemes/xcschememanagement.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | SchemeUserState 6 | 7 | XLSlideSwitchExample.xcscheme 8 | 9 | orderHint 10 | 0 11 | 12 | 13 | SuppressBuildableAutocreation 14 | 15 | 5E67247B1EB88D8C00F4EF2E 16 | 17 | primary 18 | 19 | 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /XLSlideSwitchExample/XLSlideSwitchExample/AppDelegate.h: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.h 3 | // XLSlideSwitchExample 4 | // 5 | // Created by MengXianLiang on 2017/5/2. 6 | // Copyright © 2017年 MengXianLiang. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface AppDelegate : UIResponder 12 | 13 | @property (strong, nonatomic) UIWindow *window; 14 | 15 | 16 | @end 17 | 18 | -------------------------------------------------------------------------------- /XLSlideSwitchExample/XLSlideSwitchExample/AppDelegate.m: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.m 3 | // XLSlideSwitchExample 4 | // 5 | // Created by MengXianLiang on 2017/5/2. 6 | // Copyright © 2017年 MengXianLiang. All rights reserved. 7 | // 8 | 9 | #import "AppDelegate.h" 10 | #import "ViewController.h" 11 | 12 | @interface AppDelegate () 13 | 14 | @end 15 | 16 | @implementation AppDelegate 17 | 18 | 19 | - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 20 | self.window.rootViewController = [[UINavigationController alloc] initWithRootViewController:[ViewController new]]; 21 | return YES; 22 | } 23 | 24 | 25 | - (void)applicationWillResignActive:(UIApplication *)application { 26 | // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. 27 | // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game. 28 | } 29 | 30 | 31 | - (void)applicationDidEnterBackground:(UIApplication *)application { 32 | // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 33 | // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. 34 | } 35 | 36 | 37 | - (void)applicationWillEnterForeground:(UIApplication *)application { 38 | // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background. 39 | } 40 | 41 | 42 | - (void)applicationDidBecomeActive:(UIApplication *)application { 43 | // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. 44 | } 45 | 46 | 47 | - (void)applicationWillTerminate:(UIApplication *)application { 48 | // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. 49 | } 50 | 51 | 52 | @end 53 | -------------------------------------------------------------------------------- /XLSlideSwitchExample/XLSlideSwitchExample/Assets.xcassets/AppIcon.appiconset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "iphone", 5 | "size" : "20x20", 6 | "scale" : "2x" 7 | }, 8 | { 9 | "idiom" : "iphone", 10 | "size" : "20x20", 11 | "scale" : "3x" 12 | }, 13 | { 14 | "idiom" : "iphone", 15 | "size" : "29x29", 16 | "scale" : "2x" 17 | }, 18 | { 19 | "idiom" : "iphone", 20 | "size" : "29x29", 21 | "scale" : "3x" 22 | }, 23 | { 24 | "idiom" : "iphone", 25 | "size" : "40x40", 26 | "scale" : "2x" 27 | }, 28 | { 29 | "idiom" : "iphone", 30 | "size" : "40x40", 31 | "scale" : "3x" 32 | }, 33 | { 34 | "idiom" : "iphone", 35 | "size" : "60x60", 36 | "scale" : "2x" 37 | }, 38 | { 39 | "idiom" : "iphone", 40 | "size" : "60x60", 41 | "scale" : "3x" 42 | }, 43 | { 44 | "idiom" : "ios-marketing", 45 | "size" : "1024x1024", 46 | "scale" : "1x" 47 | } 48 | ], 49 | "info" : { 50 | "version" : 1, 51 | "author" : "xcode" 52 | } 53 | } -------------------------------------------------------------------------------- /XLSlideSwitchExample/XLSlideSwitchExample/Base.lproj/LaunchScreen.storyboard: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /XLSlideSwitchExample/XLSlideSwitchExample/Base.lproj/Main.storyboard: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | -------------------------------------------------------------------------------- /XLSlideSwitchExample/XLSlideSwitchExample/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | en 7 | CFBundleExecutable 8 | $(EXECUTABLE_NAME) 9 | CFBundleIdentifier 10 | $(PRODUCT_BUNDLE_IDENTIFIER) 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundleName 14 | $(PRODUCT_NAME) 15 | CFBundlePackageType 16 | APPL 17 | CFBundleShortVersionString 18 | 1.0 19 | CFBundleVersion 20 | 1 21 | LSRequiresIPhoneOS 22 | 23 | NSAppTransportSecurity 24 | 25 | NSAllowsArbitraryLoads 26 | 27 | 28 | UILaunchStoryboardName 29 | LaunchScreen 30 | UIMainStoryboardFile 31 | Main 32 | UIRequiredDeviceCapabilities 33 | 34 | armv7 35 | 36 | UISupportedInterfaceOrientations 37 | 38 | UIInterfaceOrientationPortrait 39 | UIInterfaceOrientationLandscapeLeft 40 | UIInterfaceOrientationLandscapeRight 41 | 42 | 43 | 44 | -------------------------------------------------------------------------------- /XLSlideSwitchExample/XLSlideSwitchExample/PrefixHeader.pch: -------------------------------------------------------------------------------- 1 | // 2 | // PrefixHeader.pch 3 | // XLSlideSwitchExample 4 | // 5 | // Created by MengXianLiang on 2017/5/2. 6 | // Copyright © 2017年 MengXianLiang. All rights reserved. 7 | // 8 | 9 | #ifndef PrefixHeader_pch 10 | #define PrefixHeader_pch 11 | 12 | // Include any system framework and library headers here that should be included in all compilation units. 13 | // You will also need to set the Prefix Header build setting of one or more of your targets to reference this file. 14 | 15 | #define RGB(r, g, b) [UIColor colorWithRed:r/255.0 green:g/255.0 blue:b/255.0 alpha:1] 16 | 17 | #define RedColor RGB(212, 61, 61) 18 | 19 | #define GrayColor RGB(34, 34, 34) 20 | 21 | 22 | #endif /* PrefixHeader_pch */ 23 | -------------------------------------------------------------------------------- /XLSlideSwitchExample/XLSlideSwitchExample/ViewController.h: -------------------------------------------------------------------------------- 1 | // 2 | // ViewController.h 3 | // XLSlideSwitchExample 4 | // 5 | // Created by MengXianLiang on 2017/5/2. 6 | // Copyright © 2017年 MengXianLiang. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface ViewController : UIViewController 12 | 13 | 14 | @end 15 | 16 | -------------------------------------------------------------------------------- /XLSlideSwitchExample/XLSlideSwitchExample/ViewController.m: -------------------------------------------------------------------------------- 1 | // 2 | // ViewController.m 3 | // XLSlideSwitchExample 4 | // 5 | // Created by MengXianLiang on 2017/5/2. 6 | // Copyright © 2017年 MengXianLiang. All rights reserved. 7 | // 8 | 9 | #import "ViewController.h" 10 | 11 | #import "SlideSwitchExample1.h" 12 | #import "SlideSwitchExample2.h" 13 | 14 | #import "SegmentedSlideSwitchExample1.h" 15 | #import "SegmentedSlideSwitchExample2.h" 16 | 17 | @interface ViewController () { 18 | UITableView *_tableView; 19 | } 20 | @end 21 | 22 | @implementation ViewController 23 | 24 | - (void)viewDidLoad { 25 | [super viewDidLoad]; 26 | [self buildUI]; 27 | } 28 | 29 | - (void)buildUI { 30 | self.title = @"XLSlideSwitchExample"; 31 | self.view.backgroundColor = [UIColor whiteColor]; 32 | [self buildTable]; 33 | } 34 | 35 | - (void)buildTable { 36 | _tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain]; 37 | _tableView.delegate = self; 38 | _tableView.dataSource = self; 39 | _tableView.backgroundColor = [UIColor clearColor]; 40 | _tableView.tableFooterView = [UIView new]; 41 | [self.view addSubview:_tableView]; 42 | } 43 | 44 | #pragma mark - 45 | #pragma mark TableViewDelegate&DataSource 46 | 47 | - (NSArray *)titles { 48 | return @[@[@"SlideSwitchExample1",@"SlideSwitchExample2"],@[@"SegmentedSlideSwitchExample1",@"SegmentedSlideSwitchExample2"]]; 49 | } 50 | 51 | - (NSArray *)subTitles { 52 | return @[@[@"正常显示",@"标题在NavigationBar上显示"],@[@"正常显示",@"标题在NavigationBar上显示"]]; 53 | } 54 | 55 | - (NSArray*)sectionTitles { 56 | return @[@"XLSlideSiwtch",@"XLSegmentedSlideSiwtch"]; 57 | } 58 | 59 | - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { 60 | return 55; 61 | } 62 | 63 | - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 64 | NSArray *arr = [self titles][section]; 65 | return arr.count; 66 | } 67 | 68 | - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { 69 | return [self sectionTitles][section]; 70 | } 71 | 72 | - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 73 | return [self sectionTitles].count; 74 | } 75 | 76 | - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 77 | NSString* cellIdentifier = @"cell"; 78 | UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 79 | if (cell == nil) { 80 | cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier]; 81 | cell.detailTextLabel.textColor = self.navigationController.navigationBar.tintColor; 82 | } 83 | cell.textLabel.text = [self titles][indexPath.section][indexPath.row]; 84 | cell.detailTextLabel.text = [self subTitles][indexPath.section][indexPath.row]; 85 | return cell; 86 | } 87 | 88 | - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 89 | NSString *title = [self titles][indexPath.section][indexPath.row]; 90 | if (indexPath.section == 0) { 91 | switch (indexPath.row) { 92 | case 0:{ 93 | SlideSwitchExample1 *vc = [SlideSwitchExample1 new]; 94 | vc.title = title; 95 | [self.navigationController pushViewController:vc animated:true]; 96 | } 97 | break; 98 | case 1:{ 99 | SlideSwitchExample2 *vc = [SlideSwitchExample2 new]; 100 | vc.title = title; 101 | [self.navigationController pushViewController:vc animated:true]; 102 | } 103 | break; 104 | default: 105 | break; 106 | } 107 | }else{ 108 | switch (indexPath.row) { 109 | case 0:{ 110 | SegmentedSlideSwitchExample1 *vc = [SegmentedSlideSwitchExample1 new]; 111 | vc.title = title; 112 | [self.navigationController pushViewController:vc animated:true]; 113 | } 114 | break; 115 | case 1:{ 116 | SegmentedSlideSwitchExample2 *vc = [SegmentedSlideSwitchExample2 new]; 117 | vc.title = title; 118 | [self.navigationController pushViewController:vc animated:true]; 119 | } 120 | break; 121 | default: 122 | break; 123 | } 124 | } 125 | } 126 | 127 | 128 | - (void)didReceiveMemoryWarning { 129 | [super didReceiveMemoryWarning]; 130 | } 131 | 132 | 133 | @end 134 | 135 | -------------------------------------------------------------------------------- /XLSlideSwitchExample/XLSlideSwitchExample/main.m: -------------------------------------------------------------------------------- 1 | // 2 | // main.m 3 | // XLSlideSwitchExample 4 | // 5 | // Created by MengXianLiang on 2017/5/2. 6 | // Copyright © 2017年 MengXianLiang. All rights reserved. 7 | // 8 | 9 | #import 10 | #import "AppDelegate.h" 11 | 12 | int main(int argc, char * argv[]) { 13 | @autoreleasepool { 14 | return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class])); 15 | } 16 | } 17 | --------------------------------------------------------------------------------