├── 分段.gif ├── WechatIMG212.png ├── MainScrollView ├── 0.png ├── 1.png ├── 2.png ├── 3.png ├── 4.png ├── MonitsTableViewCell.h ├── ViewController.h ├── AppDelegate.h ├── main.m ├── MonitsViewController.h ├── MonitsTableViewCell.m ├── ImageInfo.plist ├── Assets.xcassets │ └── AppIcon.appiconset │ │ └── Contents.json ├── SGSegmentedControl │ ├── SGImageButton.h │ ├── UIView+SGExtension.h │ ├── SGImageButton.m │ ├── UIView+SGExtension.m │ ├── SGSegmentedControl.h │ └── SGSegmentedControl.m ├── MonitsAllViewController.h ├── ViewController.m ├── Info.plist ├── Base.lproj │ ├── Main.storyboard │ └── LaunchScreen.storyboard ├── AppDelegate.m ├── MonitsViewController.m └── MonitsAllViewController.m ├── README.md ├── 30E7FC2879A1570E2C8D53213AAA6A29.jpg └── MainScrollView.xcodeproj ├── project.xcworkspace ├── contents.xcworkspacedata └── xcuserdata │ └── Rillakkuma.xcuserdatad │ └── UserInterfaceState.xcuserstate ├── xcuserdata └── Rillakkuma.xcuserdatad │ ├── xcschemes │ ├── xcschememanagement.plist │ └── MainScrollView.xcscheme │ └── xcdebugger │ └── Breakpoints_v2.xcbkptlist └── project.pbxproj /分段.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Andrew5/MainScrollView/HEAD/分段.gif -------------------------------------------------------------------------------- /WechatIMG212.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Andrew5/MainScrollView/HEAD/WechatIMG212.png -------------------------------------------------------------------------------- /MainScrollView/0.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Andrew5/MainScrollView/HEAD/MainScrollView/0.png -------------------------------------------------------------------------------- /MainScrollView/1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Andrew5/MainScrollView/HEAD/MainScrollView/1.png -------------------------------------------------------------------------------- /MainScrollView/2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Andrew5/MainScrollView/HEAD/MainScrollView/2.png -------------------------------------------------------------------------------- /MainScrollView/3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Andrew5/MainScrollView/HEAD/MainScrollView/3.png -------------------------------------------------------------------------------- /MainScrollView/4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Andrew5/MainScrollView/HEAD/MainScrollView/4.png -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # 分段导航栏 滑动切换控制器 2 | ![Alt Text](https://github.com/Andrew5/MainScrollView/blob/master/分段.gif) 3 | -------------------------------------------------------------------------------- /30E7FC2879A1570E2C8D53213AAA6A29.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Andrew5/MainScrollView/HEAD/30E7FC2879A1570E2C8D53213AAA6A29.jpg -------------------------------------------------------------------------------- /MainScrollView.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /MainScrollView.xcodeproj/project.xcworkspace/xcuserdata/Rillakkuma.xcuserdatad/UserInterfaceState.xcuserstate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Andrew5/MainScrollView/HEAD/MainScrollView.xcodeproj/project.xcworkspace/xcuserdata/Rillakkuma.xcuserdatad/UserInterfaceState.xcuserstate -------------------------------------------------------------------------------- /MainScrollView/MonitsTableViewCell.h: -------------------------------------------------------------------------------- 1 | // 2 | // MonitsTableViewCell.h 3 | // Test 4 | // 5 | // Created by Rillakkuma on 2016/12/14. 6 | // Copyright © 2016年 Rillakkuma. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface MonitsTableViewCell : UITableViewCell 12 | 13 | @end 14 | -------------------------------------------------------------------------------- /MainScrollView/ViewController.h: -------------------------------------------------------------------------------- 1 | // 2 | // ViewController.h 3 | // MainScrollView 4 | // 5 | // Created by Rillakkuma on 2016/12/14. 6 | // Copyright © 2016年 Rillakkuma. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface ViewController : UIViewController 12 | 13 | 14 | @end 15 | 16 | -------------------------------------------------------------------------------- /MainScrollView/AppDelegate.h: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.h 3 | // MainScrollView 4 | // 5 | // Created by Rillakkuma on 2016/12/14. 6 | // Copyright © 2016年 Rillakkuma. 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 | -------------------------------------------------------------------------------- /MainScrollView/main.m: -------------------------------------------------------------------------------- 1 | // 2 | // main.m 3 | // MainScrollView 4 | // 5 | // Created by Rillakkuma on 2016/12/14. 6 | // Copyright © 2016年 Rillakkuma. 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 | -------------------------------------------------------------------------------- /MainScrollView/MonitsViewController.h: -------------------------------------------------------------------------------- 1 | // 2 | // MonitsViewController.h 3 | // Test 4 | // 5 | // Created by Rillakkuma on 2016/12/14. 6 | // Copyright © 2016年 Rillakkuma. All rights reserved. 7 | // 8 | 9 | #import 10 | #import "SGSegmentedControl.h" 11 | #import "MonitsAllViewController.h" 12 | 13 | @interface MonitsViewController : UIViewController 14 | 15 | @property (nonatomic, strong) SGSegmentedControl *SG; 16 | @property (nonatomic, strong) UIScrollView *mainScrollView; 17 | @end 18 | -------------------------------------------------------------------------------- /MainScrollView/MonitsTableViewCell.m: -------------------------------------------------------------------------------- 1 | // 2 | // MonitsTableViewCell.m 3 | // Test 4 | // 5 | // Created by Rillakkuma on 2016/12/14. 6 | // Copyright © 2016年 Rillakkuma. All rights reserved. 7 | // 8 | 9 | #import "MonitsTableViewCell.h" 10 | 11 | @implementation MonitsTableViewCell 12 | 13 | - (void)awakeFromNib { 14 | [super awakeFromNib]; 15 | // Initialization code 16 | } 17 | 18 | - (void)setSelected:(BOOL)selected animated:(BOOL)animated { 19 | [super setSelected:selected animated:animated]; 20 | 21 | // Configure the view for the selected state 22 | } 23 | 24 | @end 25 | -------------------------------------------------------------------------------- /MainScrollView.xcodeproj/xcuserdata/Rillakkuma.xcuserdatad/xcschemes/xcschememanagement.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | SchemeUserState 6 | 7 | MainScrollView.xcscheme 8 | 9 | orderHint 10 | 0 11 | 12 | 13 | SuppressBuildableAutocreation 14 | 15 | 8320E4331E00E8C400A8D2A2 16 | 17 | primary 18 | 19 | 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /MainScrollView/ImageInfo.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 10 | 11 | 12 | 0.png 13 | WATCH,它,终于来了。 14 | 1.png 15 | iPhone 6,无双,有此一双。 16 | 2.png 17 | MacBook,轻于时代,先于时代。 18 | 3.png 19 | iPad Air 2,轻轻地,改变一切。 20 | 4.png 21 | iOS 9,它,将要到来。 22 | 23 | 24 | -------------------------------------------------------------------------------- /MainScrollView/Assets.xcassets/AppIcon.appiconset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "iphone", 5 | "size" : "29x29", 6 | "scale" : "2x" 7 | }, 8 | { 9 | "idiom" : "iphone", 10 | "size" : "29x29", 11 | "scale" : "3x" 12 | }, 13 | { 14 | "idiom" : "iphone", 15 | "size" : "40x40", 16 | "scale" : "2x" 17 | }, 18 | { 19 | "idiom" : "iphone", 20 | "size" : "40x40", 21 | "scale" : "3x" 22 | }, 23 | { 24 | "idiom" : "iphone", 25 | "size" : "60x60", 26 | "scale" : "2x" 27 | }, 28 | { 29 | "idiom" : "iphone", 30 | "size" : "60x60", 31 | "scale" : "3x" 32 | } 33 | ], 34 | "info" : { 35 | "version" : 1, 36 | "author" : "xcode" 37 | } 38 | } -------------------------------------------------------------------------------- /MainScrollView/SGSegmentedControl/SGImageButton.h: -------------------------------------------------------------------------------- 1 | // 2 | // SGImageButton.h 3 | // SGSegmentedControlExample 4 | // 5 | // Created by Sorgle on 2016/10/6. 6 | // Copyright © 2016年 Sorgle. All rights reserved. 7 | // 8 | // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // 9 | // 10 | // - - 如在使用中, 遇到什么问题或者有更好建议者, 请于kingsic@126.com邮箱联系 - - - - // 11 | // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 12 | // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 13 | // - - GitHub下载地址 https://github.com/kingsic/SGSegmentedControl.git - - - // 14 | // 15 | // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // 16 | 17 | #import 18 | 19 | @interface SGImageButton : UIButton 20 | 21 | @end 22 | -------------------------------------------------------------------------------- /MainScrollView/MonitsAllViewController.h: -------------------------------------------------------------------------------- 1 | // 2 | // MonitsAllViewController.h 3 | // Test 4 | // 5 | // Created by Rillakkuma on 2016/12/14. 6 | // Copyright © 2016年 Rillakkuma. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface MonitsAllViewController : UIViewController 12 | { 13 | UITableView *table; 14 | UICollectionView *collectionview; 15 | 16 | UIScrollView *_scrollView; 17 | UIPageControl *pageC; 18 | UIImageView *imgVLeft; 19 | UIImageView *imgVCenter; 20 | UIImageView *imgVRight; 21 | UILabel *lblImageDesc; 22 | NSMutableDictionary *mDicImageData; 23 | NSUInteger currentImageIndexs; 24 | NSUInteger imageCount; 25 | 26 | } 27 | @property(nonatomic,assign)int tagString; 28 | 29 | @end 30 | -------------------------------------------------------------------------------- /MainScrollView/ViewController.m: -------------------------------------------------------------------------------- 1 | // 2 | // ViewController.m 3 | // MainScrollView 4 | // 5 | // Created by Rillakkuma on 2016/12/14. 6 | // Copyright © 2016年 Rillakkuma. All rights reserved. 7 | // 8 | 9 | #import "ViewController.h" 10 | #import "MonitsViewController.h" 11 | @interface ViewController () 12 | 13 | @end 14 | 15 | @implementation ViewController 16 | 17 | - (void)viewDidLoad { 18 | [super viewDidLoad]; 19 | MonitsViewController *monitsVC = [[MonitsViewController alloc]init]; 20 | // [self.navigationController pushViewController:monitsVC animated:YES]; 21 | [self presentViewController:monitsVC animated:YES completion:nil]; 22 | // Do any additional setup after loading the view, typically from a nib. 23 | } 24 | 25 | 26 | - (void)didReceiveMemoryWarning { 27 | [super didReceiveMemoryWarning]; 28 | // Dispose of any resources that can be recreated. 29 | } 30 | 31 | 32 | @end 33 | -------------------------------------------------------------------------------- /MainScrollView.xcodeproj/xcuserdata/Rillakkuma.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 8 | 20 | 21 | 22 | 23 | 24 | -------------------------------------------------------------------------------- /MainScrollView/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 | UILaunchStoryboardName 24 | LaunchScreen 25 | UIMainStoryboardFile 26 | Main 27 | UIRequiredDeviceCapabilities 28 | 29 | armv7 30 | 31 | UISupportedInterfaceOrientations 32 | 33 | UIInterfaceOrientationPortrait 34 | UIInterfaceOrientationLandscapeLeft 35 | UIInterfaceOrientationLandscapeRight 36 | 37 | 38 | 39 | -------------------------------------------------------------------------------- /MainScrollView/SGSegmentedControl/UIView+SGExtension.h: -------------------------------------------------------------------------------- 1 | // 2 | // UIView+SGExtension.h 3 | // UIView+SGExtension 4 | // 5 | // Created by Sorgle on 15/6/21. 6 | // Copyright © 2015年 Sorgle. All rights reserved. 7 | // 8 | // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // 9 | // 10 | // - - 如在使用中, 遇到什么问题或者有更好建议者, 请于kingsic@126.com邮箱联系 - - - - // 11 | // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 12 | // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 13 | // - - GitHub下载地址 https://github.com/kingsic/SGSegmentedControl.git - - - // 14 | // 15 | // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // 16 | 17 | #import 18 | 19 | @interface UIView (SGExtension) 20 | @property (nonatomic ,assign) CGFloat SG_x; 21 | @property (nonatomic ,assign) CGFloat SG_y; 22 | @property (nonatomic ,assign) CGFloat SG_width; 23 | @property (nonatomic ,assign) CGFloat SG_height; 24 | @property (nonatomic ,assign) CGFloat SG_centerX; 25 | @property (nonatomic ,assign) CGFloat SG_centerY; 26 | 27 | @property (nonatomic ,assign) CGSize SG_size; 28 | 29 | @property (nonatomic, assign) CGFloat SG_right; 30 | @property (nonatomic, assign) CGFloat SG_bottom; 31 | 32 | + (instancetype)SG_viewFromXib; 33 | /** 在分类中声明@property, 只会生成方法的声明, 不会生成方法的实现和带有_线成员变量 */ 34 | @end 35 | -------------------------------------------------------------------------------- /MainScrollView/SGSegmentedControl/SGImageButton.m: -------------------------------------------------------------------------------- 1 | // 2 | // SGImageButton.m 3 | // SGSegmentedControlExample 4 | // 5 | // Created by Sorgle on 2016/10/6. 6 | // Copyright © 2016年 Sorgle. All rights reserved. 7 | // 8 | // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // 9 | // 10 | // - - 如在使用中, 遇到什么问题或者有更好建议者, 请于kingsic@126.com邮箱联系 - - - - // 11 | // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 12 | // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 13 | // - - GitHub下载地址 https://github.com/kingsic/SGSegmentedControl.git - - - // 14 | // 15 | // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // 16 | 17 | #import "SGImageButton.h" 18 | #import "UIView+SGExtension.h" 19 | 20 | @implementation SGImageButton 21 | 22 | - (void)awakeFromNib{ 23 | [super awakeFromNib]; 24 | 25 | self.titleLabel.textAlignment = NSTextAlignmentCenter; 26 | } 27 | 28 | - (instancetype)initWithFrame:(CGRect)frame { 29 | if (self = [super initWithFrame:frame]) { 30 | self.titleLabel.textAlignment = NSTextAlignmentCenter; 31 | } 32 | return self; 33 | } 34 | 35 | - (void)layoutSubviews{ 36 | [super layoutSubviews]; 37 | 38 | // 调整图片 39 | self.imageView.SG_y = 5; 40 | self.imageView.SG_centerX = self.SG_width * 0.5; 41 | 42 | // 调整文字 43 | self.titleLabel.SG_x = 0; 44 | self.titleLabel.SG_y = self.imageView.SG_bottom + 5; 45 | self.titleLabel.SG_height = self.SG_height - self.titleLabel.SG_y - self.imageView.SG_y; 46 | self.titleLabel.SG_width = self.SG_width; 47 | } 48 | 49 | 50 | @end 51 | -------------------------------------------------------------------------------- /MainScrollView/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 | -------------------------------------------------------------------------------- /MainScrollView/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 | -------------------------------------------------------------------------------- /MainScrollView/AppDelegate.m: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.m 3 | // MainScrollView 4 | // 5 | // Created by Rillakkuma on 2016/12/14. 6 | // Copyright © 2016年 Rillakkuma. All rights reserved. 7 | // 8 | 9 | #import "AppDelegate.h" 10 | #import "MonitsViewController.h" 11 | @interface AppDelegate () 12 | 13 | @end 14 | 15 | @implementation AppDelegate 16 | 17 | 18 | - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 19 | MonitsViewController *monitsVC = [[MonitsViewController alloc]init]; 20 | 21 | UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:monitsVC]; 22 | self.window.rootViewController = nav; 23 | 24 | 25 | // [self.navigationController pushViewController:monitsVC animated:YES]; 26 | // Override point for customization after application launch. 27 | return YES; 28 | } 29 | 30 | 31 | - (void)applicationWillResignActive:(UIApplication *)application { 32 | // 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. 33 | // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game. 34 | } 35 | 36 | 37 | - (void)applicationDidEnterBackground:(UIApplication *)application { 38 | // 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. 39 | // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. 40 | } 41 | 42 | 43 | - (void)applicationWillEnterForeground:(UIApplication *)application { 44 | // 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. 45 | } 46 | 47 | 48 | - (void)applicationDidBecomeActive:(UIApplication *)application { 49 | // 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. 50 | } 51 | 52 | 53 | - (void)applicationWillTerminate:(UIApplication *)application { 54 | // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. 55 | } 56 | 57 | 58 | @end 59 | -------------------------------------------------------------------------------- /MainScrollView/SGSegmentedControl/UIView+SGExtension.m: -------------------------------------------------------------------------------- 1 | // 2 | // UIView+SGExtension.m 3 | // UIView+SGExtension 4 | // 5 | // Created by Sorgle on 15/6/21. 6 | // Copyright © 2015年 Sorgle. All rights reserved. 7 | // 8 | // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // 9 | // 10 | // - - 如在使用中, 遇到什么问题或者有更好建议者, 请于kingsic@126.com邮箱联系 - - - - // 11 | // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 12 | // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 13 | // - - GitHub下载地址 https://github.com/kingsic/SGSegmentedControl.git - - - // 14 | // 15 | // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // 16 | 17 | #import "UIView+SGExtension.h" 18 | 19 | @implementation UIView (SGExtension) 20 | 21 | - (void)setSG_x:(CGFloat)SG_x{ 22 | CGRect frame = self.frame; 23 | frame.origin.x = SG_x; 24 | self.frame = frame; 25 | } 26 | 27 | - (void)setSG_y:(CGFloat)SG_y{ 28 | CGRect frame = self.frame; 29 | frame.origin.y = SG_y; 30 | self.frame = frame; 31 | } 32 | 33 | - (void)setSG_width:(CGFloat)SG_width{ 34 | CGRect frame = self.frame; 35 | frame.size.width = SG_width; 36 | self.frame = frame; 37 | } 38 | 39 | - (void)setSG_height:(CGFloat)SG_height{ 40 | CGRect frame = self.frame; 41 | frame.size.height = SG_height; 42 | self.frame = frame; 43 | } 44 | 45 | - (CGFloat)SG_x{ 46 | return self.frame.origin.x; 47 | } 48 | 49 | - (CGFloat)SG_y{ 50 | return self.frame.origin.y; 51 | } 52 | 53 | - (CGFloat)SG_width{ 54 | return self.frame.size.width; 55 | } 56 | 57 | - (CGFloat)SG_height{ 58 | return self.frame.size.height; 59 | } 60 | 61 | - (CGFloat)SG_centerX{ 62 | return self.center.x; 63 | } 64 | - (void)setSG_centerX:(CGFloat)SG_centerX{ 65 | CGPoint center = self.center; 66 | center.x = SG_centerX; 67 | self.center = center; 68 | } 69 | 70 | - (CGFloat)SG_centerY{ 71 | return self.center.y; 72 | } 73 | - (void)setSG_centerY:(CGFloat)SG_centerY{ 74 | CGPoint center = self.center; 75 | center.y = SG_centerY; 76 | self.center = center; 77 | } 78 | 79 | - (void)setSG_size:(CGSize)SG_size{ 80 | CGRect frame = self.frame; 81 | frame.size = SG_size; 82 | self.frame = frame; 83 | } 84 | - (CGSize)SG_size{ 85 | return self.frame.size; 86 | } 87 | 88 | 89 | - (CGFloat)SG_right{ 90 | // return self.SG_x + self.SG_width; 91 | return CGRectGetMaxX(self.frame); 92 | } 93 | - (CGFloat)SG_bottom{ 94 | // return self.SG_y + self.SG_height; 95 | return CGRectGetMaxY(self.frame); 96 | } 97 | 98 | - (void)setSG_right:(CGFloat)SG_right{ 99 | self.SG_x = SG_right - self.SG_width; 100 | } 101 | - (void)setSG_bottom:(CGFloat)SG_bottom{ 102 | self.SG_y = SG_bottom - self.SG_height; 103 | } 104 | 105 | + (instancetype)SG_viewFromXib { 106 | return [[NSBundle mainBundle] loadNibNamed:NSStringFromClass(self) owner:nil options:nil].lastObject; 107 | } 108 | 109 | @end 110 | 111 | 112 | -------------------------------------------------------------------------------- /MainScrollView.xcodeproj/xcuserdata/Rillakkuma.xcuserdatad/xcschemes/MainScrollView.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 | -------------------------------------------------------------------------------- /MainScrollView/SGSegmentedControl/SGSegmentedControl.h: -------------------------------------------------------------------------------- 1 | // 2 | // SGSegmentedControl.h 3 | // SGSegmentedControlExample 4 | // 5 | // Created by Sorgle on 2016/10/6. 6 | // Copyright © 2016年 Sorgle. All rights reserved. 7 | // 8 | // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // 9 | // 10 | // - - 如在使用中, 遇到什么问题或者有更好建议者, 请于kingsic@126.com邮箱联系 - - - - // 11 | // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 12 | // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 13 | // - - GitHub下载地址 https://github.com/kingsic/SGSegmentedControl.git - - - // 14 | // 15 | // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // 16 | 17 | #import 18 | @class SGSegmentedControl; 19 | 20 | typedef enum : NSUInteger { 21 | SGSegmentedControlTypeScroll, // 滚动风格 22 | SGSegmentedControlTypeStatic, // 静止风格 23 | } SGSegmentedControlType; // 默认为滚动风格 24 | 25 | typedef enum : NSUInteger { 26 | SGSegmentedControlIndicatorTypeBottom, // 指示器底部样式 27 | SGSegmentedControlIndicatorTypeCenter, // 指示器背景样式 28 | SGSegmentedControlIndicatorTypeBankground, // 指示器背景样式 29 | } SGSegmentedControlIndicatorType; // SGSegmentedControlIndicatorType 指示器样式, 默认为底部样式 30 | 31 | @protocol SGSegmentedControlDelegate 32 | // delegate 方法 33 | - (void)SGSegmentedControl:(SGSegmentedControl *)segmentedControl didSelectBtnAtIndex:(NSInteger)index; 34 | 35 | @end 36 | 37 | 38 | @interface SGSegmentedControl : UIScrollView 39 | 40 | /** 对象方法创建 SGSegmentedControl */ 41 | - (instancetype)initWithFrame:(CGRect)frame delegate:(id)delegate segmentedControlType:(SGSegmentedControlType)segmentedControlType titleArr:(NSArray *)titleArr; 42 | /** 类方法创建 SGSegmentedControl */ 43 | + (instancetype)segmentedControlWithFrame:(CGRect)frame delegate:(id)delegate segmentedControlType:(SGSegmentedControlType)segmentedControlType titleArr:(NSArray *)titleArr; 44 | 45 | /** 对象方法创建,带有图片的SGSegmentedControl */ 46 | - (instancetype)initWithFrame:(CGRect)frame delegate:(id)delegate segmentedControlType:(SGSegmentedControlType)segmentedControlType nomalImageArr:(NSArray *)nomalImageArr selectedImageArr:(NSArray *)selectedImageArr titleArr:(NSArray *)titleArr; 47 | /** 类方法创建,带有图片的SGSegmentedControl */ 48 | + (instancetype)segmentedControlWithFrame:(CGRect)frame delegate:(id)delegate segmentedControlType:(SGSegmentedControlType)segmentedControlType nomalImageArr:(NSArray *)nomalImageArr selectedImageArr:(NSArray *)selectedImageArr titleArr:(NSArray *)titleArr; 49 | 50 | /** 默认为滚动风格 */ 51 | @property (nonatomic, assign) SGSegmentedControlType segmentedControlType; 52 | 53 | /** 标题文字颜色(默认为黑色) */ 54 | @property (nonatomic, strong) UIColor *titleColorStateNormal; 55 | /** 选中时标题文字颜色(默认为红色) */ 56 | @property (nonatomic, strong) UIColor *titleColorStateSelected; 57 | /** 指示器的颜色(默认为红色) */ 58 | @property (nonatomic, strong) UIColor *indicatorColor; 59 | /** 是否显示底部滚动指示器(默认为YES, 显示) */ 60 | @property (nonatomic, assign) BOOL showsBottomScrollIndicator; 61 | /** 指示器样式(默认为底部样式) */ 62 | @property (nonatomic, assign) SGSegmentedControlIndicatorType segmentedControlIndicatorType; 63 | 64 | @property (nonatomic, weak) id delegate_SG; 65 | 66 | 67 | /** 标题选中颜色改变以及指示器位置变化 */ 68 | - (void)titleBtnSelectedWithScrollView:(UIScrollView *)scrollView; 69 | 70 | /** 标题文字渐变效果(默认为NO), 与titleBtnColorGradualChangeScrollViewDidScroll方法,一起才会生效*/ 71 | @property (nonatomic, assign) BOOL titleColorGradualChange; 72 | /** 标题文字缩放效果(默认为NO), 与titleBtnColorGradualChangeScrollViewDidScroll方法,一起才会生效*/ 73 | @property (nonatomic, assign) BOOL titleFondGradualChange; 74 | /** 给外界scrollViewDidScroll方法提供文字渐显效果 */ 75 | - (void)titleBtnColorGradualChangeScrollViewDidScroll:(UIScrollView *)scrollView; 76 | 77 | 78 | @end 79 | 80 | 81 | -------------------------------------------------------------------------------- /MainScrollView/MonitsViewController.m: -------------------------------------------------------------------------------- 1 | // 2 | // MonitsViewController.m 3 | // Test 4 | // 5 | // Created by Rillakkuma on 2016/12/14. 6 | // Copyright © 2016年 Rillakkuma. All rights reserved. 7 | // 8 | 9 | #import "MonitsViewController.h" 10 | 11 | @interface MonitsViewController () 12 | 13 | @end 14 | 15 | @implementation MonitsViewController 16 | 17 | - (void)viewDidLoad { 18 | [super viewDidLoad]; 19 | 20 | self.navigationItem.title = @"分段函数"; 21 | self.automaticallyAdjustsScrollViewInsets = NO; 22 | self.view.backgroundColor = [UIColor whiteColor]; 23 | 24 | // 1.添加所有子控制器 25 | [self setupChildViewController]; 26 | 27 | [self setupSegmentedControl]; 28 | 29 | } 30 | - (void)setupSegmentedControl { 31 | 32 | NSArray *title_arr = @[@"全部", @"新数据", @"好朋友", @"知己", @"同事"]; 33 | 34 | // 创建底部滚动视图 35 | self.mainScrollView = [[UIScrollView alloc] init]; 36 | _mainScrollView.frame = CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height-64); 37 | _mainScrollView.contentSize = CGSizeMake(self.view.frame.size.width * title_arr.count, 0); 38 | _mainScrollView.backgroundColor = [UIColor clearColor]; 39 | // 开启分页 40 | _mainScrollView.pagingEnabled = YES; 41 | // 没有弹簧效果 42 | _mainScrollView.bounces = NO; 43 | // 隐藏水平滚动条 44 | _mainScrollView.showsHorizontalScrollIndicator = NO; 45 | // 设置代理 46 | _mainScrollView.delegate = self; 47 | [self.view addSubview:_mainScrollView]; 48 | 49 | 50 | self.SG = [SGSegmentedControl segmentedControlWithFrame:CGRectMake(0, 64, self.view.frame.size.width, 44) delegate:self segmentedControlType:(SGSegmentedControlTypeStatic) titleArr:title_arr]; 51 | _SG.titleColorStateSelected = [UIColor redColor]; 52 | _SG.indicatorColor = [UIColor orangeColor]; 53 | [self.view addSubview:_SG]; 54 | 55 | UIView *linView = [[UIView alloc]init]; 56 | linView.frame = CGRectMake(0, 43+64, [UIScreen mainScreen].bounds.size.width, 1); 57 | linView.backgroundColor = [UIColor whiteColor];//底线 58 | [self.SG addSubview:linView]; 59 | 60 | MonitsAllViewController *complainListVC = [[MonitsAllViewController alloc] init]; 61 | [self.mainScrollView addSubview:complainListVC.view]; 62 | [self addChildViewController:complainListVC]; 63 | } 64 | 65 | 66 | - (void)SGSegmentedControl:(SGSegmentedControl *)segmentedControl didSelectBtnAtIndex:(NSInteger)index { 67 | // 1 计算滚动的位置 68 | CGFloat offsetX = index * self.view.frame.size.width; 69 | self.mainScrollView.contentOffset = CGPointMake(offsetX, 0); 70 | 71 | // 2.给对应位置添加对应子控制器 72 | [self showVc:index]; 73 | } 74 | 75 | // 添加所有子控制器 76 | - (void)setupChildViewController { 77 | NSArray *title_arr = @[@"全部", @"新数据", @"好朋友", @"知己", @"同事"]; 78 | for (int i = 0; i kWidthOfScreen) { //向左滑动 333 | currentImageIndexs = (currentImageIndexs + 1) % imageCount; 334 | } else if (contentOffset.x < kWidthOfScreen) { //向右滑动 335 | currentImageIndexs = (currentImageIndexs - 1 + imageCount) % imageCount; 336 | } 337 | 338 | [self setInfoByCurrentImageIndex:currentImageIndexs]; 339 | } 340 | 341 | - (void)layoutUI { 342 | 343 | [self loadImageData]; 344 | [self addScrollView]; 345 | [self addImageViewsToScrollView]; 346 | [self addPageControl]; 347 | [self addLabel]; 348 | [self setDefaultInfo]; 349 | } 350 | 351 | // scrollview 减速停止 352 | - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView { 353 | [self reloadImage]; 354 | 355 | _scrollView.contentOffset = CGPointMake(kWidthOfScreen, 0.0); 356 | pageC.currentPage = currentImageIndexs; 357 | 358 | NSString *currentImageNamed = [NSString stringWithFormat:@"%lu.png", (unsigned long)currentImageIndexs]; 359 | lblImageDesc.text = mDicImageData[currentImageNamed]; 360 | } 361 | 362 | /* 363 | #pragma mark - Navigation 364 | 365 | // In a storyboard-based application, you will often want to do a little preparation before navigation 366 | - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { 367 | // Get the new view controller using [segue destinationViewController]. 368 | // Pass the selected object to the new view controller. 369 | } 370 | */ 371 | 372 | @end 373 | -------------------------------------------------------------------------------- /MainScrollView.xcodeproj/project.pbxproj: -------------------------------------------------------------------------------- 1 | // !$*UTF8*$! 2 | { 3 | archiveVersion = 1; 4 | classes = { 5 | }; 6 | objectVersion = 46; 7 | objects = { 8 | 9 | /* Begin PBXBuildFile section */ 10 | 8320E4391E00E8C400A8D2A2 /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 8320E4381E00E8C400A8D2A2 /* main.m */; }; 11 | 8320E43C1E00E8C400A8D2A2 /* AppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = 8320E43B1E00E8C400A8D2A2 /* AppDelegate.m */; }; 12 | 8320E43F1E00E8C400A8D2A2 /* ViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 8320E43E1E00E8C400A8D2A2 /* ViewController.m */; }; 13 | 8320E4421E00E8C400A8D2A2 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 8320E4401E00E8C400A8D2A2 /* Main.storyboard */; }; 14 | 8320E4441E00E8C400A8D2A2 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 8320E4431E00E8C400A8D2A2 /* Assets.xcassets */; }; 15 | 8320E4471E00E8C400A8D2A2 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 8320E4451E00E8C400A8D2A2 /* LaunchScreen.storyboard */; }; 16 | 8320E4541E00E8E000A8D2A2 /* MonitsAllViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 8320E44F1E00E8E000A8D2A2 /* MonitsAllViewController.m */; }; 17 | 8320E4551E00E8E000A8D2A2 /* MonitsTableViewCell.m in Sources */ = {isa = PBXBuildFile; fileRef = 8320E4511E00E8E000A8D2A2 /* MonitsTableViewCell.m */; }; 18 | 8320E4561E00E8E000A8D2A2 /* MonitsViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 8320E4531E00E8E000A8D2A2 /* MonitsViewController.m */; }; 19 | 8320E45E1E00E90A00A8D2A2 /* SGImageButton.m in Sources */ = {isa = PBXBuildFile; fileRef = 8320E4591E00E90A00A8D2A2 /* SGImageButton.m */; }; 20 | 8320E45F1E00E90A00A8D2A2 /* SGSegmentedControl.m in Sources */ = {isa = PBXBuildFile; fileRef = 8320E45B1E00E90A00A8D2A2 /* SGSegmentedControl.m */; }; 21 | 8320E4601E00E90A00A8D2A2 /* UIView+SGExtension.m in Sources */ = {isa = PBXBuildFile; fileRef = 8320E45D1E00E90A00A8D2A2 /* UIView+SGExtension.m */; }; 22 | 83838CC71E00F010004B3A77 /* 0.png in Resources */ = {isa = PBXBuildFile; fileRef = 83838CC21E00F010004B3A77 /* 0.png */; }; 23 | 83838CC81E00F010004B3A77 /* 1.png in Resources */ = {isa = PBXBuildFile; fileRef = 83838CC31E00F010004B3A77 /* 1.png */; }; 24 | 83838CC91E00F010004B3A77 /* 2.png in Resources */ = {isa = PBXBuildFile; fileRef = 83838CC41E00F010004B3A77 /* 2.png */; }; 25 | 83838CCA1E00F010004B3A77 /* 3.png in Resources */ = {isa = PBXBuildFile; fileRef = 83838CC51E00F010004B3A77 /* 3.png */; }; 26 | 83838CCB1E00F010004B3A77 /* 4.png in Resources */ = {isa = PBXBuildFile; fileRef = 83838CC61E00F010004B3A77 /* 4.png */; }; 27 | 83838CCD1E00F029004B3A77 /* ImageInfo.plist in Resources */ = {isa = PBXBuildFile; fileRef = 83838CCC1E00F029004B3A77 /* ImageInfo.plist */; }; 28 | /* End PBXBuildFile section */ 29 | 30 | /* Begin PBXFileReference section */ 31 | 8320E4341E00E8C400A8D2A2 /* MainScrollView.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = MainScrollView.app; sourceTree = BUILT_PRODUCTS_DIR; }; 32 | 8320E4381E00E8C400A8D2A2 /* main.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = main.m; sourceTree = ""; }; 33 | 8320E43A1E00E8C400A8D2A2 /* AppDelegate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = AppDelegate.h; sourceTree = ""; }; 34 | 8320E43B1E00E8C400A8D2A2 /* AppDelegate.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = AppDelegate.m; sourceTree = ""; }; 35 | 8320E43D1E00E8C400A8D2A2 /* ViewController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = ViewController.h; sourceTree = ""; }; 36 | 8320E43E1E00E8C400A8D2A2 /* ViewController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = ViewController.m; sourceTree = ""; }; 37 | 8320E4411E00E8C400A8D2A2 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = ""; }; 38 | 8320E4431E00E8C400A8D2A2 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; 39 | 8320E4461E00E8C400A8D2A2 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = ""; }; 40 | 8320E4481E00E8C400A8D2A2 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 41 | 8320E44E1E00E8E000A8D2A2 /* MonitsAllViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MonitsAllViewController.h; sourceTree = ""; }; 42 | 8320E44F1E00E8E000A8D2A2 /* MonitsAllViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MonitsAllViewController.m; sourceTree = ""; }; 43 | 8320E4501E00E8E000A8D2A2 /* MonitsTableViewCell.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MonitsTableViewCell.h; sourceTree = ""; }; 44 | 8320E4511E00E8E000A8D2A2 /* MonitsTableViewCell.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MonitsTableViewCell.m; sourceTree = ""; }; 45 | 8320E4521E00E8E000A8D2A2 /* MonitsViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MonitsViewController.h; sourceTree = ""; }; 46 | 8320E4531E00E8E000A8D2A2 /* MonitsViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MonitsViewController.m; sourceTree = ""; }; 47 | 8320E4581E00E90A00A8D2A2 /* SGImageButton.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SGImageButton.h; sourceTree = ""; }; 48 | 8320E4591E00E90A00A8D2A2 /* SGImageButton.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SGImageButton.m; sourceTree = ""; }; 49 | 8320E45A1E00E90A00A8D2A2 /* SGSegmentedControl.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SGSegmentedControl.h; sourceTree = ""; }; 50 | 8320E45B1E00E90A00A8D2A2 /* SGSegmentedControl.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SGSegmentedControl.m; sourceTree = ""; }; 51 | 8320E45C1E00E90A00A8D2A2 /* UIView+SGExtension.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "UIView+SGExtension.h"; sourceTree = ""; }; 52 | 8320E45D1E00E90A00A8D2A2 /* UIView+SGExtension.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "UIView+SGExtension.m"; sourceTree = ""; }; 53 | 83838CC21E00F010004B3A77 /* 0.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = 0.png; sourceTree = ""; }; 54 | 83838CC31E00F010004B3A77 /* 1.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = 1.png; sourceTree = ""; }; 55 | 83838CC41E00F010004B3A77 /* 2.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = 2.png; sourceTree = ""; }; 56 | 83838CC51E00F010004B3A77 /* 3.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = 3.png; sourceTree = ""; }; 57 | 83838CC61E00F010004B3A77 /* 4.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = 4.png; sourceTree = ""; }; 58 | 83838CCC1E00F029004B3A77 /* ImageInfo.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = ImageInfo.plist; sourceTree = ""; }; 59 | /* End PBXFileReference section */ 60 | 61 | /* Begin PBXFrameworksBuildPhase section */ 62 | 8320E4311E00E8C400A8D2A2 /* Frameworks */ = { 63 | isa = PBXFrameworksBuildPhase; 64 | buildActionMask = 2147483647; 65 | files = ( 66 | ); 67 | runOnlyForDeploymentPostprocessing = 0; 68 | }; 69 | /* End PBXFrameworksBuildPhase section */ 70 | 71 | /* Begin PBXGroup section */ 72 | 8320E42B1E00E8C400A8D2A2 = { 73 | isa = PBXGroup; 74 | children = ( 75 | 8320E4361E00E8C400A8D2A2 /* MainScrollView */, 76 | 8320E4351E00E8C400A8D2A2 /* Products */, 77 | ); 78 | sourceTree = ""; 79 | }; 80 | 8320E4351E00E8C400A8D2A2 /* Products */ = { 81 | isa = PBXGroup; 82 | children = ( 83 | 8320E4341E00E8C400A8D2A2 /* MainScrollView.app */, 84 | ); 85 | name = Products; 86 | sourceTree = ""; 87 | }; 88 | 8320E4361E00E8C400A8D2A2 /* MainScrollView */ = { 89 | isa = PBXGroup; 90 | children = ( 91 | 8320E43A1E00E8C400A8D2A2 /* AppDelegate.h */, 92 | 8320E43B1E00E8C400A8D2A2 /* AppDelegate.m */, 93 | 8320E4521E00E8E000A8D2A2 /* MonitsViewController.h */, 94 | 8320E4531E00E8E000A8D2A2 /* MonitsViewController.m */, 95 | 8320E44E1E00E8E000A8D2A2 /* MonitsAllViewController.h */, 96 | 8320E44F1E00E8E000A8D2A2 /* MonitsAllViewController.m */, 97 | 8320E4501E00E8E000A8D2A2 /* MonitsTableViewCell.h */, 98 | 8320E4511E00E8E000A8D2A2 /* MonitsTableViewCell.m */, 99 | 83838CCC1E00F029004B3A77 /* ImageInfo.plist */, 100 | 83838CC21E00F010004B3A77 /* 0.png */, 101 | 83838CC31E00F010004B3A77 /* 1.png */, 102 | 83838CC41E00F010004B3A77 /* 2.png */, 103 | 83838CC51E00F010004B3A77 /* 3.png */, 104 | 83838CC61E00F010004B3A77 /* 4.png */, 105 | 8320E4571E00E90A00A8D2A2 /* SGSegmentedControl */, 106 | 8320E43D1E00E8C400A8D2A2 /* ViewController.h */, 107 | 8320E43E1E00E8C400A8D2A2 /* ViewController.m */, 108 | 8320E4401E00E8C400A8D2A2 /* Main.storyboard */, 109 | 8320E4431E00E8C400A8D2A2 /* Assets.xcassets */, 110 | 8320E4451E00E8C400A8D2A2 /* LaunchScreen.storyboard */, 111 | 8320E4481E00E8C400A8D2A2 /* Info.plist */, 112 | 8320E4371E00E8C400A8D2A2 /* Supporting Files */, 113 | ); 114 | path = MainScrollView; 115 | sourceTree = ""; 116 | }; 117 | 8320E4371E00E8C400A8D2A2 /* Supporting Files */ = { 118 | isa = PBXGroup; 119 | children = ( 120 | 8320E4381E00E8C400A8D2A2 /* main.m */, 121 | ); 122 | name = "Supporting Files"; 123 | sourceTree = ""; 124 | }; 125 | 8320E4571E00E90A00A8D2A2 /* SGSegmentedControl */ = { 126 | isa = PBXGroup; 127 | children = ( 128 | 8320E4581E00E90A00A8D2A2 /* SGImageButton.h */, 129 | 8320E4591E00E90A00A8D2A2 /* SGImageButton.m */, 130 | 8320E45A1E00E90A00A8D2A2 /* SGSegmentedControl.h */, 131 | 8320E45B1E00E90A00A8D2A2 /* SGSegmentedControl.m */, 132 | 8320E45C1E00E90A00A8D2A2 /* UIView+SGExtension.h */, 133 | 8320E45D1E00E90A00A8D2A2 /* UIView+SGExtension.m */, 134 | ); 135 | path = SGSegmentedControl; 136 | sourceTree = ""; 137 | }; 138 | /* End PBXGroup section */ 139 | 140 | /* Begin PBXNativeTarget section */ 141 | 8320E4331E00E8C400A8D2A2 /* MainScrollView */ = { 142 | isa = PBXNativeTarget; 143 | buildConfigurationList = 8320E44B1E00E8C400A8D2A2 /* Build configuration list for PBXNativeTarget "MainScrollView" */; 144 | buildPhases = ( 145 | 8320E4301E00E8C400A8D2A2 /* Sources */, 146 | 8320E4311E00E8C400A8D2A2 /* Frameworks */, 147 | 8320E4321E00E8C400A8D2A2 /* Resources */, 148 | ); 149 | buildRules = ( 150 | ); 151 | dependencies = ( 152 | ); 153 | name = MainScrollView; 154 | productName = MainScrollView; 155 | productReference = 8320E4341E00E8C400A8D2A2 /* MainScrollView.app */; 156 | productType = "com.apple.product-type.application"; 157 | }; 158 | /* End PBXNativeTarget section */ 159 | 160 | /* Begin PBXProject section */ 161 | 8320E42C1E00E8C400A8D2A2 /* Project object */ = { 162 | isa = PBXProject; 163 | attributes = { 164 | LastUpgradeCheck = 0810; 165 | ORGANIZATIONNAME = Rillakkuma; 166 | TargetAttributes = { 167 | 8320E4331E00E8C400A8D2A2 = { 168 | CreatedOnToolsVersion = 8.1; 169 | ProvisioningStyle = Automatic; 170 | }; 171 | }; 172 | }; 173 | buildConfigurationList = 8320E42F1E00E8C400A8D2A2 /* Build configuration list for PBXProject "MainScrollView" */; 174 | compatibilityVersion = "Xcode 3.2"; 175 | developmentRegion = English; 176 | hasScannedForEncodings = 0; 177 | knownRegions = ( 178 | en, 179 | Base, 180 | ); 181 | mainGroup = 8320E42B1E00E8C400A8D2A2; 182 | productRefGroup = 8320E4351E00E8C400A8D2A2 /* Products */; 183 | projectDirPath = ""; 184 | projectRoot = ""; 185 | targets = ( 186 | 8320E4331E00E8C400A8D2A2 /* MainScrollView */, 187 | ); 188 | }; 189 | /* End PBXProject section */ 190 | 191 | /* Begin PBXResourcesBuildPhase section */ 192 | 8320E4321E00E8C400A8D2A2 /* Resources */ = { 193 | isa = PBXResourcesBuildPhase; 194 | buildActionMask = 2147483647; 195 | files = ( 196 | 83838CC81E00F010004B3A77 /* 1.png in Resources */, 197 | 8320E4471E00E8C400A8D2A2 /* LaunchScreen.storyboard in Resources */, 198 | 8320E4441E00E8C400A8D2A2 /* Assets.xcassets in Resources */, 199 | 83838CCD1E00F029004B3A77 /* ImageInfo.plist in Resources */, 200 | 83838CC71E00F010004B3A77 /* 0.png in Resources */, 201 | 83838CCA1E00F010004B3A77 /* 3.png in Resources */, 202 | 83838CC91E00F010004B3A77 /* 2.png in Resources */, 203 | 83838CCB1E00F010004B3A77 /* 4.png in Resources */, 204 | 8320E4421E00E8C400A8D2A2 /* Main.storyboard in Resources */, 205 | ); 206 | runOnlyForDeploymentPostprocessing = 0; 207 | }; 208 | /* End PBXResourcesBuildPhase section */ 209 | 210 | /* Begin PBXSourcesBuildPhase section */ 211 | 8320E4301E00E8C400A8D2A2 /* Sources */ = { 212 | isa = PBXSourcesBuildPhase; 213 | buildActionMask = 2147483647; 214 | files = ( 215 | 8320E4561E00E8E000A8D2A2 /* MonitsViewController.m in Sources */, 216 | 8320E4551E00E8E000A8D2A2 /* MonitsTableViewCell.m in Sources */, 217 | 8320E43F1E00E8C400A8D2A2 /* ViewController.m in Sources */, 218 | 8320E43C1E00E8C400A8D2A2 /* AppDelegate.m in Sources */, 219 | 8320E4601E00E90A00A8D2A2 /* UIView+SGExtension.m in Sources */, 220 | 8320E45E1E00E90A00A8D2A2 /* SGImageButton.m in Sources */, 221 | 8320E4541E00E8E000A8D2A2 /* MonitsAllViewController.m in Sources */, 222 | 8320E45F1E00E90A00A8D2A2 /* SGSegmentedControl.m in Sources */, 223 | 8320E4391E00E8C400A8D2A2 /* main.m in Sources */, 224 | ); 225 | runOnlyForDeploymentPostprocessing = 0; 226 | }; 227 | /* End PBXSourcesBuildPhase section */ 228 | 229 | /* Begin PBXVariantGroup section */ 230 | 8320E4401E00E8C400A8D2A2 /* Main.storyboard */ = { 231 | isa = PBXVariantGroup; 232 | children = ( 233 | 8320E4411E00E8C400A8D2A2 /* Base */, 234 | ); 235 | name = Main.storyboard; 236 | sourceTree = ""; 237 | }; 238 | 8320E4451E00E8C400A8D2A2 /* LaunchScreen.storyboard */ = { 239 | isa = PBXVariantGroup; 240 | children = ( 241 | 8320E4461E00E8C400A8D2A2 /* Base */, 242 | ); 243 | name = LaunchScreen.storyboard; 244 | sourceTree = ""; 245 | }; 246 | /* End PBXVariantGroup section */ 247 | 248 | /* Begin XCBuildConfiguration section */ 249 | 8320E4491E00E8C400A8D2A2 /* Debug */ = { 250 | isa = XCBuildConfiguration; 251 | buildSettings = { 252 | ALWAYS_SEARCH_USER_PATHS = NO; 253 | CLANG_ANALYZER_NONNULL = YES; 254 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 255 | CLANG_CXX_LIBRARY = "libc++"; 256 | CLANG_ENABLE_MODULES = YES; 257 | CLANG_ENABLE_OBJC_ARC = YES; 258 | CLANG_WARN_BOOL_CONVERSION = YES; 259 | CLANG_WARN_CONSTANT_CONVERSION = YES; 260 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 261 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 262 | CLANG_WARN_EMPTY_BODY = YES; 263 | CLANG_WARN_ENUM_CONVERSION = YES; 264 | CLANG_WARN_INFINITE_RECURSION = YES; 265 | CLANG_WARN_INT_CONVERSION = YES; 266 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 267 | CLANG_WARN_SUSPICIOUS_MOVES = YES; 268 | CLANG_WARN_UNREACHABLE_CODE = YES; 269 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 270 | "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; 271 | COPY_PHASE_STRIP = NO; 272 | DEBUG_INFORMATION_FORMAT = dwarf; 273 | ENABLE_STRICT_OBJC_MSGSEND = YES; 274 | ENABLE_TESTABILITY = YES; 275 | GCC_C_LANGUAGE_STANDARD = gnu99; 276 | GCC_DYNAMIC_NO_PIC = NO; 277 | GCC_NO_COMMON_BLOCKS = YES; 278 | GCC_OPTIMIZATION_LEVEL = 0; 279 | GCC_PREPROCESSOR_DEFINITIONS = ( 280 | "DEBUG=1", 281 | "$(inherited)", 282 | ); 283 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 284 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 285 | GCC_WARN_UNDECLARED_SELECTOR = YES; 286 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 287 | GCC_WARN_UNUSED_FUNCTION = YES; 288 | GCC_WARN_UNUSED_VARIABLE = YES; 289 | IPHONEOS_DEPLOYMENT_TARGET = 10.1; 290 | MTL_ENABLE_DEBUG_INFO = YES; 291 | ONLY_ACTIVE_ARCH = YES; 292 | SDKROOT = iphoneos; 293 | }; 294 | name = Debug; 295 | }; 296 | 8320E44A1E00E8C400A8D2A2 /* Release */ = { 297 | isa = XCBuildConfiguration; 298 | buildSettings = { 299 | ALWAYS_SEARCH_USER_PATHS = NO; 300 | CLANG_ANALYZER_NONNULL = YES; 301 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 302 | CLANG_CXX_LIBRARY = "libc++"; 303 | CLANG_ENABLE_MODULES = YES; 304 | CLANG_ENABLE_OBJC_ARC = YES; 305 | CLANG_WARN_BOOL_CONVERSION = YES; 306 | CLANG_WARN_CONSTANT_CONVERSION = YES; 307 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 308 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 309 | CLANG_WARN_EMPTY_BODY = YES; 310 | CLANG_WARN_ENUM_CONVERSION = YES; 311 | CLANG_WARN_INFINITE_RECURSION = YES; 312 | CLANG_WARN_INT_CONVERSION = YES; 313 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 314 | CLANG_WARN_SUSPICIOUS_MOVES = YES; 315 | CLANG_WARN_UNREACHABLE_CODE = YES; 316 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 317 | "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; 318 | COPY_PHASE_STRIP = NO; 319 | DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; 320 | ENABLE_NS_ASSERTIONS = NO; 321 | ENABLE_STRICT_OBJC_MSGSEND = YES; 322 | GCC_C_LANGUAGE_STANDARD = gnu99; 323 | GCC_NO_COMMON_BLOCKS = YES; 324 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 325 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 326 | GCC_WARN_UNDECLARED_SELECTOR = YES; 327 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 328 | GCC_WARN_UNUSED_FUNCTION = YES; 329 | GCC_WARN_UNUSED_VARIABLE = YES; 330 | IPHONEOS_DEPLOYMENT_TARGET = 10.1; 331 | MTL_ENABLE_DEBUG_INFO = NO; 332 | SDKROOT = iphoneos; 333 | VALIDATE_PRODUCT = YES; 334 | }; 335 | name = Release; 336 | }; 337 | 8320E44C1E00E8C400A8D2A2 /* Debug */ = { 338 | isa = XCBuildConfiguration; 339 | buildSettings = { 340 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 341 | INFOPLIST_FILE = MainScrollView/Info.plist; 342 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; 343 | PRODUCT_BUNDLE_IDENTIFIER = "com.-.MainScrollView"; 344 | PRODUCT_NAME = "$(TARGET_NAME)"; 345 | }; 346 | name = Debug; 347 | }; 348 | 8320E44D1E00E8C400A8D2A2 /* Release */ = { 349 | isa = XCBuildConfiguration; 350 | buildSettings = { 351 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 352 | INFOPLIST_FILE = MainScrollView/Info.plist; 353 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; 354 | PRODUCT_BUNDLE_IDENTIFIER = "com.-.MainScrollView"; 355 | PRODUCT_NAME = "$(TARGET_NAME)"; 356 | }; 357 | name = Release; 358 | }; 359 | /* End XCBuildConfiguration section */ 360 | 361 | /* Begin XCConfigurationList section */ 362 | 8320E42F1E00E8C400A8D2A2 /* Build configuration list for PBXProject "MainScrollView" */ = { 363 | isa = XCConfigurationList; 364 | buildConfigurations = ( 365 | 8320E4491E00E8C400A8D2A2 /* Debug */, 366 | 8320E44A1E00E8C400A8D2A2 /* Release */, 367 | ); 368 | defaultConfigurationIsVisible = 0; 369 | defaultConfigurationName = Release; 370 | }; 371 | 8320E44B1E00E8C400A8D2A2 /* Build configuration list for PBXNativeTarget "MainScrollView" */ = { 372 | isa = XCConfigurationList; 373 | buildConfigurations = ( 374 | 8320E44C1E00E8C400A8D2A2 /* Debug */, 375 | 8320E44D1E00E8C400A8D2A2 /* Release */, 376 | ); 377 | defaultConfigurationIsVisible = 0; 378 | defaultConfigurationName = Release; 379 | }; 380 | /* End XCConfigurationList section */ 381 | }; 382 | rootObject = 8320E42C1E00E8C400A8D2A2 /* Project object */; 383 | } 384 | -------------------------------------------------------------------------------- /MainScrollView/SGSegmentedControl/SGSegmentedControl.m: -------------------------------------------------------------------------------- 1 | // 2 | // SGSegmentedControl.m 3 | // SGSegmentedControlExample 4 | // 5 | // Created by Sorgle on 2016/10/6. 6 | // Copyright © 2016年 Sorgle. All rights reserved. 7 | // 8 | // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // 9 | // 10 | // - - 如在使用中, 遇到什么问题或者有更好建议者, 请于kingsic@126.com邮箱联系 - - - - // 11 | // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 12 | // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 13 | // - - GitHub下载地址 https://github.com/kingsic/SGSegmentedControl.git - - - // 14 | // 15 | // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // 16 | 17 | #import "SGSegmentedControl.h" 18 | #import "SGImageButton.h" 19 | #import "UIView+SGExtension.h" 20 | #define IndicatorWidth 41 21 | 22 | #define SG_screenWidth [UIScreen mainScreen].bounds.size.width 23 | #define indicatorViewColorDefualt [UIColor redColor] 24 | 25 | @interface SGSegmentedControl () 26 | /** 标题按钮 */ 27 | @property (nonatomic, strong) UIButton *title_btn; 28 | /** 带有图片的标题按钮 */ 29 | @property (nonatomic, strong) SGImageButton *image_title_btn; 30 | /** 存入所有标题按钮 */ 31 | @property (nonatomic, strong) NSMutableArray *titleBtn_mArr; 32 | /** 普通状态下的图片数组 */ 33 | @property (nonatomic, strong) NSArray *nomal_image_Arr; 34 | /** 选中状态下的图片数组 */ 35 | @property (nonatomic, strong) NSArray *selected_image_Arr; 36 | /** 标题数组 */ 37 | @property (nonatomic, strong) NSArray *title_Arr; 38 | /** 指示器 */ 39 | @property (nonatomic, strong) UIView *indicatorView; 40 | /** 背景指示器下面的小indicatorView */ 41 | @property (nonatomic, strong) UIView *bgIndicatorView; 42 | 43 | /** 临时button用来转换button的点击状态 */ 44 | @property (nonatomic, strong) UIButton *temp_btn; 45 | 46 | @end 47 | 48 | @implementation SGSegmentedControl 49 | 50 | /** 按钮之间的间距(滚动时按钮之间的间距) */ 51 | static CGFloat const btn_Margin = 15; 52 | /** 按钮字体的大小(字号) */ 53 | static CGFloat const btn_fondOfSize = 14; 54 | /** 指示器的高度 */ 55 | static CGFloat const indicatorViewHeight = 2; 56 | /** 点击按钮时, 指示器的动画移动时间 */ 57 | static CGFloat const indicatorViewTimeOfAnimation = 0.4; 58 | 59 | - (NSMutableArray *)titleBtn_mArr { 60 | if (!_titleBtn_mArr) { 61 | _titleBtn_mArr = [NSMutableArray array]; 62 | } 63 | return _titleBtn_mArr; 64 | } 65 | 66 | - (instancetype)initWithFrame:(CGRect)frame delegate:(id)delegate segmentedControlType:(SGSegmentedControlType)segmentedControlType titleArr:(NSArray *)titleArr { 67 | if (self = [super initWithFrame:frame]) { 68 | self.backgroundColor = [[UIColor whiteColor] colorWithAlphaComponent:0.7]; 69 | self.showsHorizontalScrollIndicator = NO; 70 | self.bounces = NO; 71 | self.delegate_SG = delegate; 72 | self.segmentedControlType = segmentedControlType; 73 | self.title_Arr = titleArr; 74 | 75 | [self setupTitleArr]; 76 | } 77 | return self; 78 | } 79 | 80 | + (instancetype)segmentedControlWithFrame:(CGRect)frame delegate:(id)delegate segmentedControlType:(SGSegmentedControlType)segmentedControlType titleArr:(NSArray *)titleArr { 81 | return [[self alloc] initWithFrame:frame delegate:delegate segmentedControlType:segmentedControlType titleArr:titleArr]; 82 | } 83 | 84 | - (instancetype)initWithFrame:(CGRect)frame delegate:(id)delegate segmentedControlType:(SGSegmentedControlType)segmentedControlType nomalImageArr:(NSArray *)nomalImageArr selectedImageArr:(NSArray *)selectedImageArr titleArr:(NSArray *)titleArr { 85 | if (self = [super initWithFrame:frame]) { 86 | self.backgroundColor = [[UIColor whiteColor] colorWithAlphaComponent:0.7]; 87 | self.showsHorizontalScrollIndicator = NO; 88 | self.bounces = NO; 89 | self.delegate_SG = delegate; 90 | self.nomal_image_Arr = nomalImageArr; 91 | self.selected_image_Arr = selectedImageArr; 92 | self.segmentedControlType = segmentedControlType; 93 | self.title_Arr = titleArr; 94 | 95 | [self setupImageAndTitleArr]; 96 | } 97 | return self; 98 | } 99 | 100 | + (instancetype)segmentedControlWithFrame:(CGRect)frame delegate:(id)delegate segmentedControlType:(SGSegmentedControlType)segmentedControlType nomalImageArr:(NSArray *)nomalImageArr selectedImageArr:(NSArray *)selectedImageArr titleArr:(NSArray *)titleArr { 101 | return [[self alloc] initWithFrame:frame delegate:delegate segmentedControlType:segmentedControlType nomalImageArr:nomalImageArr selectedImageArr:selectedImageArr titleArr:titleArr]; 102 | } 103 | 104 | /** 105 | * 计算文字尺寸 106 | * 107 | * @param text 需要计算尺寸的文字 108 | * @param font 文字的字体 109 | * @param maxSize 文字的最大尺寸 110 | */ 111 | - (CGSize)sizeWithText:(NSString *)text font:(UIFont *)font maxSize:(CGSize)maxSize { 112 | NSDictionary *attrs = @{NSFontAttributeName : font}; 113 | return [text boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:attrs context:nil].size; 114 | } 115 | 116 | /** 创建标题按钮 */ 117 | - (void)setupTitleArr { 118 | 119 | if (self.segmentedControlType == SGSegmentedControlTypeScroll) { 120 | 121 | CGFloat button_X = 0; 122 | CGFloat button_Y = 0; 123 | CGFloat button_H = self.frame.size.height; 124 | 125 | for (NSUInteger i = 0; i < _title_Arr.count; i++) { 126 | /** 创建滚动时的标题Label */ 127 | self.title_btn = [UIButton buttonWithType:(UIButtonTypeCustom)]; 128 | _title_btn.titleLabel.font = [UIFont systemFontOfSize:btn_fondOfSize]; 129 | _title_btn.tag = i; 130 | 131 | // 计算内容的Size 132 | CGSize buttonSize = [self sizeWithText:_title_Arr[i] font:[UIFont systemFontOfSize:btn_fondOfSize] maxSize:CGSizeMake(MAXFLOAT, button_H)]; 133 | 134 | // 计算内容的宽度 135 | CGFloat button_W = 2 * btn_Margin + buttonSize.width; 136 | _title_btn.frame = CGRectMake(button_X, button_Y, button_W, button_H); 137 | 138 | [_title_btn setTitle:_title_Arr[i] forState:(UIControlStateNormal)]; 139 | [_title_btn setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)]; 140 | [_title_btn setTitleColor:[UIColor redColor] forState:(UIControlStateSelected)]; 141 | 142 | // 计算每个label的X值 143 | button_X = button_X + button_W; 144 | 145 | // 点击事件 146 | [_title_btn addTarget:self action:@selector(buttonAction:) forControlEvents:(UIControlEventTouchUpInside)]; 147 | 148 | // 默认选中第0个button 149 | if (i == 0) { 150 | [self buttonAction:_title_btn]; 151 | } 152 | 153 | // 存入所有的title_btn 154 | [self.titleBtn_mArr addObject:_title_btn]; 155 | [self addSubview:_title_btn]; 156 | } 157 | 158 | // 计算scrollView的宽度 159 | CGFloat scrollViewWidth = CGRectGetMaxX(self.subviews.lastObject.frame); 160 | self.contentSize = CGSizeMake(scrollViewWidth, self.frame.size.height); 161 | 162 | // 取出第一个子控件 163 | UIButton *firstButton = self.subviews.firstObject; 164 | 165 | // 添加指示器 166 | self.indicatorView = [[UIView alloc] init]; 167 | _indicatorView.backgroundColor = indicatorViewColorDefualt; 168 | _indicatorView.SG_height = indicatorViewHeight; 169 | _indicatorView.SG_y = self.frame.size.height - indicatorViewHeight; 170 | [self addSubview:_indicatorView]; 171 | 172 | // 指示器默认在第一个选中位置 173 | // 计算TitleLabel内容的Size 174 | CGSize buttonSize = [self sizeWithText:firstButton.titleLabel.text font:[UIFont systemFontOfSize:btn_fondOfSize] maxSize:CGSizeMake(MAXFLOAT, self.frame.size.height)]; 175 | _indicatorView.SG_width = buttonSize.width; 176 | _indicatorView.SG_centerX = firstButton.SG_centerX; 177 | 178 | } else { 179 | 180 | // 计算scrollView的宽度 181 | CGFloat scrollViewWidth = self.frame.size.width; 182 | CGFloat button_X = 0; 183 | CGFloat button_Y = 0; 184 | CGFloat button_W = scrollViewWidth / _title_Arr.count; 185 | CGFloat button_H = self.frame.size.height; 186 | 187 | for (NSInteger i = 0; i < _title_Arr.count; i++) { 188 | // 创建静止时的标题Label 189 | self.title_btn = [UIButton buttonWithType:(UIButtonTypeCustom)]; 190 | 191 | _title_btn.titleLabel.font = [UIFont systemFontOfSize:btn_fondOfSize]; 192 | _title_btn.tag = i; 193 | 194 | // 计算title_btn的x值 195 | button_X = i * button_W; 196 | _title_btn.frame = CGRectMake(button_X, button_Y, button_W, button_H); 197 | 198 | [_title_btn setTitle:_title_Arr[i] forState:(UIControlStateNormal)]; 199 | [_title_btn setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)]; 200 | [_title_btn setTitleColor:[UIColor redColor] forState:(UIControlStateSelected)]; 201 | 202 | // 点击事件 203 | [_title_btn addTarget:self action:@selector(buttonAction:) forControlEvents:(UIControlEventTouchUpInside)]; 204 | 205 | // 默认选中第0个button 206 | if (i == 0) { 207 | [self buttonAction:_title_btn]; 208 | } 209 | 210 | // 存入所有的title_btn 211 | [self.titleBtn_mArr addObject:_title_btn]; 212 | [self addSubview:_title_btn]; 213 | } 214 | 215 | // 取出第一个子控件 216 | UIButton *firstButton = self.subviews.firstObject; 217 | 218 | // 添加指示器 219 | self.indicatorView = [[UIView alloc] init]; 220 | _indicatorView.backgroundColor = indicatorViewColorDefualt; 221 | _indicatorView.SG_height = indicatorViewHeight; 222 | _indicatorView.SG_y = self.frame.size.height - indicatorViewHeight; 223 | [self addSubview:_indicatorView]; 224 | 225 | // 指示器默认在第一个选中位置 226 | // 计算TitleLabel内容的Size 227 | CGSize buttonSize = [self sizeWithText:firstButton.titleLabel.text font:[UIFont systemFontOfSize:btn_fondOfSize] maxSize:CGSizeMake(MAXFLOAT, self.frame.size.height)]; 228 | _indicatorView.SG_width = buttonSize.width + IndicatorWidth; 229 | _indicatorView.SG_centerX = firstButton.SG_centerX; 230 | } 231 | } 232 | 233 | /** 创建带有图片的标题按钮 */ 234 | - (void)setupImageAndTitleArr { 235 | 236 | if (self.segmentedControlType == SGSegmentedControlTypeScroll) { 237 | 238 | CGFloat button_X = 0; 239 | CGFloat button_Y = 0; 240 | CGFloat button_H = self.frame.size.height; 241 | 242 | for (NSUInteger i = 0; i < _title_Arr.count; i++) { 243 | /** 创建滚动时的标题Label */ 244 | self.image_title_btn = [[SGImageButton alloc] init]; 245 | 246 | _image_title_btn.titleLabel.font = [UIFont systemFontOfSize:btn_fondOfSize]; 247 | _image_title_btn.tag = i; 248 | 249 | // 计算内容的Size 250 | CGSize buttonSize = [self sizeWithText:_title_Arr[i] font:[UIFont systemFontOfSize:btn_fondOfSize] maxSize:CGSizeMake(MAXFLOAT, button_H)]; 251 | 252 | // 计算内容的宽度 253 | CGFloat button_W = 2 * btn_Margin + buttonSize.width; 254 | _image_title_btn.frame = CGRectMake(button_X, button_Y, button_W, button_H); 255 | 256 | [_image_title_btn setTitle:_title_Arr[i] forState:(UIControlStateNormal)]; 257 | [_image_title_btn setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)]; 258 | [_image_title_btn setTitleColor:[UIColor redColor] forState:(UIControlStateSelected)]; 259 | [_image_title_btn setImage:[UIImage imageNamed:_nomal_image_Arr[i]] forState:(UIControlStateNormal)]; 260 | [_image_title_btn setImage:[UIImage imageNamed:_selected_image_Arr[i]] forState:(UIControlStateSelected)]; 261 | 262 | // 计算每个label的X值 263 | button_X = button_X + button_W; 264 | 265 | // 点击事件 266 | [_image_title_btn addTarget:self action:@selector(buttonAction:) forControlEvents:(UIControlEventTouchUpInside)]; 267 | 268 | // 默认选中第0个button 269 | if (i == 0) { 270 | [self buttonAction:_image_title_btn]; 271 | } 272 | 273 | // 存入所有的title_btn 274 | [self.titleBtn_mArr addObject:_image_title_btn]; 275 | [self addSubview:_image_title_btn]; 276 | } 277 | 278 | // 计算scrollView的宽度 279 | CGFloat scrollViewWidth = CGRectGetMaxX(self.subviews.lastObject.frame); 280 | self.contentSize = CGSizeMake(scrollViewWidth, self.frame.size.height); 281 | 282 | // 取出第一个子控件 283 | UIButton *firstButton = self.subviews.firstObject; 284 | 285 | // 添加指示器 286 | self.indicatorView = [[UIView alloc] init]; 287 | _indicatorView.backgroundColor = indicatorViewColorDefualt; 288 | _indicatorView.SG_height = indicatorViewHeight; 289 | _indicatorView.SG_y = self.frame.size.height - indicatorViewHeight; 290 | [self addSubview:_indicatorView]; 291 | 292 | 293 | // 指示器默认在第一个选中位置 294 | // 计算TitleLabel内容的Size 295 | CGSize buttonSize = [self sizeWithText:firstButton.titleLabel.text font:[UIFont systemFontOfSize:btn_fondOfSize] maxSize:CGSizeMake(MAXFLOAT, self.frame.size.height)]; 296 | _indicatorView.SG_width = buttonSize.width; 297 | _indicatorView.SG_centerX = firstButton.SG_centerX; 298 | 299 | } else { 300 | 301 | // 计算scrollView的宽度 302 | CGFloat scrollViewWidth = self.frame.size.width; 303 | CGFloat button_X = 0; 304 | CGFloat button_Y = 0; 305 | CGFloat button_W = scrollViewWidth / _title_Arr.count; 306 | CGFloat button_H = self.frame.size.height; 307 | 308 | for (NSInteger i = 0; i < _title_Arr.count; i++) { 309 | // 创建静止时的标题Label 310 | self.image_title_btn = [[SGImageButton alloc] init]; 311 | 312 | _image_title_btn.titleLabel.font = [UIFont systemFontOfSize:btn_fondOfSize]; 313 | _image_title_btn.tag = i; 314 | 315 | // 计算title_btn的x值 316 | button_X = i * button_W; 317 | _image_title_btn.frame = CGRectMake(button_X, button_Y, button_W, button_H); 318 | 319 | [_image_title_btn setTitle:_title_Arr[i] forState:(UIControlStateNormal)]; 320 | [_image_title_btn setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)]; 321 | [_image_title_btn setTitleColor:[UIColor redColor] forState:(UIControlStateSelected)]; 322 | [_image_title_btn setImage:[UIImage imageNamed:_nomal_image_Arr[i]] forState:(UIControlStateNormal)]; 323 | [_image_title_btn setImage:[UIImage imageNamed:_selected_image_Arr[i]] forState:(UIControlStateSelected)]; 324 | 325 | // 点击事件 326 | [_image_title_btn addTarget:self action:@selector(buttonAction:) forControlEvents:(UIControlEventTouchUpInside)]; 327 | 328 | // 默认选中第0个button 329 | if (i == 0) { 330 | [self buttonAction:_image_title_btn]; 331 | } 332 | 333 | // 存入所有的title_btn 334 | [self.titleBtn_mArr addObject:_image_title_btn]; 335 | [self addSubview:_image_title_btn]; 336 | } 337 | 338 | // 取出第一个子控件 339 | UIButton *firstButton = self.subviews.firstObject; 340 | 341 | // 添加指示器 342 | self.indicatorView = [[UIView alloc] init]; 343 | _indicatorView.backgroundColor = indicatorViewColorDefualt; 344 | _indicatorView.SG_height = indicatorViewHeight; 345 | _indicatorView.SG_y = self.frame.size.height - indicatorViewHeight; 346 | [self addSubview:_indicatorView]; 347 | 348 | // 指示器默认在第一个选中位置 349 | // 计算TitleLabel内容的Size 350 | CGSize buttonSize = [self sizeWithText:firstButton.titleLabel.text font:[UIFont systemFontOfSize:btn_fondOfSize] maxSize:CGSizeMake(MAXFLOAT, self.frame.size.height)]; 351 | _indicatorView.SG_width = buttonSize.width; 352 | _indicatorView.SG_centerX = firstButton.SG_centerX; 353 | } 354 | } 355 | 356 | #pragma mark - - - 按钮的点击事件 357 | - (void)buttonAction:(UIButton *)sender { 358 | 359 | // 1、让选中的标题居中 360 | if (self.segmentedControlType == SGSegmentedControlTypeScroll) { 361 | [self titleBtnSelectededCenter:sender]; 362 | } 363 | 364 | // 2、代理方法实现 365 | NSInteger index = sender.tag; 366 | if ([self.delegate_SG respondsToSelector:@selector(SGSegmentedControl:didSelectBtnAtIndex:)]) { 367 | [self.delegate_SG SGSegmentedControl:self didSelectBtnAtIndex:index]; 368 | } 369 | 370 | // 3、改变指示器位置 371 | [self titleBtnSelected:sender]; 372 | } 373 | 374 | /** 标题选中颜色改变以及指示器位置变化 */ 375 | - (void)titleBtnSelected:(UIButton *)button { 376 | 377 | dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(indicatorViewTimeOfAnimation * 0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ 378 | if (_temp_btn == nil) { 379 | button.selected = YES; 380 | _temp_btn = button; 381 | }else if (_temp_btn != nil && _temp_btn == button){ 382 | button.selected = YES; 383 | }else if (_temp_btn != button && _temp_btn != nil){ 384 | _temp_btn.selected = NO; 385 | button.selected = YES; _temp_btn = button; 386 | } 387 | }); 388 | 389 | if (self.segmentedControlType == SGSegmentedControlTypeScroll) { 390 | // 改变指示器位置 391 | if (self.segmentedControlIndicatorType == SGSegmentedControlIndicatorTypeCenter) { 392 | [UIView animateWithDuration:0.20 animations:^{ 393 | self.indicatorView.SG_width = button.SG_width - btn_Margin; 394 | self.indicatorView.SG_centerX = button.SG_centerX; 395 | }]; 396 | } else if (self.segmentedControlIndicatorType == SGSegmentedControlIndicatorTypeBankground) { 397 | [UIView animateWithDuration:0.20 animations:^{ 398 | self.indicatorView.SG_width = button.SG_width; 399 | self.bgIndicatorView.SG_width = button.SG_width; 400 | self.indicatorView.SG_centerX = button.SG_centerX; 401 | }]; 402 | } else { 403 | [UIView animateWithDuration:0.20 animations:^{ 404 | self.indicatorView.SG_width = button.SG_width - 2 * btn_Margin; 405 | self.indicatorView.SG_centerX = button.SG_centerX; 406 | }]; 407 | } 408 | 409 | // 2、让选中的标题居中 410 | [self titleBtnSelectededCenter:button]; 411 | 412 | } else { 413 | 414 | // 改变指示器位置 415 | if (self.segmentedControlIndicatorType == SGSegmentedControlIndicatorTypeCenter) { 416 | // 改变指示器位置 417 | [UIView animateWithDuration:indicatorViewTimeOfAnimation animations:^{ 418 | // 计算内容的Size 419 | CGSize buttonSize = [self sizeWithText:button.titleLabel.text font:[UIFont systemFontOfSize:btn_fondOfSize] maxSize:CGSizeMake(MAXFLOAT, self.frame.size.height - indicatorViewHeight)]; 420 | self.indicatorView.SG_width = buttonSize.width + btn_Margin; 421 | self.indicatorView.SG_centerX = button.SG_centerX; 422 | }]; 423 | 424 | } else if (self.segmentedControlIndicatorType == SGSegmentedControlIndicatorTypeBankground){ 425 | 426 | // 改变指示器位置 427 | [UIView animateWithDuration:indicatorViewTimeOfAnimation animations:^{ 428 | // 计算内容的Size 429 | // CGSize buttonSize = [self sizeWithText:sender.titleLabel.text font:[UIFont systemFontOfSize:btn_fondOfSize] maxSize:CGSizeMake(MAXFLOAT, self.frame.size.height - indicatorViewHeight)]; 430 | self.indicatorView.SG_width = self.SG_width / _title_Arr.count; 431 | self.indicatorView.SG_centerX = button.SG_centerX; 432 | }]; 433 | 434 | } else { 435 | 436 | // 改变指示器位置 437 | [UIView animateWithDuration:indicatorViewTimeOfAnimation animations:^{ 438 | // 计算内容的Size 439 | CGSize buttonSize = [self sizeWithText:button.titleLabel.text font:[UIFont systemFontOfSize:btn_fondOfSize] maxSize:CGSizeMake(MAXFLOAT, self.frame.size.height - indicatorViewHeight)]; 440 | self.indicatorView.SG_width = buttonSize.width+IndicatorWidth; 441 | self.indicatorView.SG_centerX = button.SG_centerX; 442 | }]; 443 | } 444 | } 445 | } 446 | 447 | /** 标题选中颜色改变以及指示器位置变化 */ 448 | - (void)titleBtnSelectedWithScrollView:(UIScrollView *)scrollView { 449 | // 1、计算滚动到哪一页 450 | NSInteger index = scrollView.contentOffset.x / scrollView.frame.size.width; 451 | 452 | // 2、把对应的标题选中 453 | UIButton *selectedBtn = self.titleBtn_mArr[index]; 454 | 455 | // 3、滚动时,改变标题选中 456 | [self titleBtnSelected:selectedBtn]; 457 | } 458 | 459 | /** 给外界scrollViewDidScroll方法提供文字渐显效果 */ 460 | - (void)titleBtnColorGradualChangeScrollViewDidScroll:(UIScrollView *)scrollView { 461 | 462 | CGFloat curPage = scrollView.contentOffset.x / scrollView.bounds.size.width; 463 | 464 | // 左边label角标 465 | NSInteger leftIndex = curPage; 466 | // 右边的label角标 467 | NSInteger rightIndex = leftIndex + 1; 468 | 469 | // 获取左边的label 470 | UIButton *left_btn = self.titleBtn_mArr[leftIndex]; 471 | 472 | // 获取右边的label 473 | UIButton *right_btn; 474 | if (rightIndex < self.titleBtn_mArr.count - 1) { 475 | right_btn = self.titleBtn_mArr[rightIndex]; 476 | } 477 | 478 | // 计算下右边缩放比例 479 | CGFloat rightScale = curPage - leftIndex; 480 | 481 | // 计算下左边缩放比例 482 | CGFloat leftScale = 1 - rightScale; 483 | 484 | if (self.titleFondGradualChange == YES) { 485 | // 左边缩放 486 | left_btn.transform = CGAffineTransformMakeScale(leftScale * 0.1 + 1, leftScale * 0.1 + 1); 487 | // 右边缩放 488 | right_btn.transform = CGAffineTransformMakeScale(rightScale * 0.1 + 1, rightScale * 0.1 + 1); 489 | } 490 | 491 | if (self.titleColorGradualChange == YES) { 492 | // 设置文字颜色渐变 493 | left_btn.titleLabel.textColor = [UIColor colorWithRed:leftScale green:0 blue:0 alpha:1]; 494 | right_btn.titleLabel.textColor = [UIColor colorWithRed:rightScale green:0 blue:0 alpha:1]; 495 | } 496 | } 497 | 498 | /** 滚动标题选中居中 */ 499 | - (void)titleBtnSelectededCenter:(UIButton *)centerBtn { 500 | // 计算偏移量 501 | CGFloat offsetX = centerBtn.center.x - SG_screenWidth * 0.5; 502 | 503 | if (offsetX < 0) offsetX = 0; 504 | 505 | // 获取最大滚动范围 506 | CGFloat maxOffsetX = self.contentSize.width - SG_screenWidth; 507 | 508 | if (offsetX > maxOffsetX) offsetX = maxOffsetX; 509 | 510 | // 滚动标题滚动条 511 | [self setContentOffset:CGPointMake(offsetX, 0) animated:YES]; 512 | } 513 | 514 | 515 | #pragma mark - - - setter 方法设置属性 516 | - (void)setTitleColorStateNormal:(UIColor *)titleColorStateNormal { 517 | _titleColorStateNormal = titleColorStateNormal; 518 | for (UIView *subViews in self.titleBtn_mArr) { 519 | UIButton *button = (UIButton *)subViews; 520 | [button setTitleColor:titleColorStateNormal forState:(UIControlStateNormal)]; 521 | } 522 | } 523 | 524 | - (void)setTitleColorStateSelected:(UIColor *)titleColorStateSelected { 525 | _titleColorStateSelected = titleColorStateSelected; 526 | for (UIView *subViews in self.titleBtn_mArr) { 527 | UIButton *button = (UIButton *)subViews; 528 | [button setTitleColor:titleColorStateSelected forState:(UIControlStateSelected)]; 529 | } 530 | } 531 | 532 | - (void)setIndicatorColor:(UIColor *)indicatorColor { 533 | _indicatorColor = indicatorColor; 534 | _indicatorView.backgroundColor = indicatorColor; 535 | } 536 | 537 | - (void)setShowsBottomScrollIndicator:(BOOL)showsBottomScrollIndicator { 538 | if (self.showsBottomScrollIndicator == YES) { 539 | 540 | } else { 541 | [self.indicatorView removeFromSuperview]; 542 | } 543 | } 544 | 545 | - (void)setSegmentedControlIndicatorType:(SGSegmentedControlIndicatorType)segmentedControlIndicatorType { 546 | _segmentedControlIndicatorType = segmentedControlIndicatorType; 547 | if (self.segmentedControlIndicatorType == SGSegmentedControlIndicatorTypeBottom) { 548 | 549 | } else if (self.segmentedControlIndicatorType == SGSegmentedControlIndicatorTypeCenter){ 550 | 551 | // 取出第一个子控件 552 | UIButton *firstButton = self.subviews.firstObject; 553 | 554 | // 指示器默认在第一个选中位置 555 | // 计算TitleLabel内容的Size 556 | CGSize buttonSize = [self sizeWithText:firstButton.titleLabel.text font:[UIFont systemFontOfSize:btn_fondOfSize] maxSize:CGSizeMake(MAXFLOAT, self.frame.size.height)]; 557 | _indicatorView.SG_width = buttonSize.width+IndicatorWidth; 558 | _indicatorView.SG_centerX = firstButton.SG_centerX; 559 | 560 | CGFloat indicatorViewHeight = 25; 561 | self.indicatorView.SG_height = indicatorViewHeight; 562 | self.indicatorView.SG_y = (self.frame.size.height - indicatorViewHeight) * 0.5; 563 | 564 | self.indicatorView.alpha = 0.3; 565 | self.indicatorView.layer.cornerRadius = 7; 566 | self.indicatorView.layer.masksToBounds = YES; 567 | 568 | } else if (self.segmentedControlIndicatorType == SGSegmentedControlIndicatorTypeBankground){ 569 | 570 | // 取出第一个子控件 571 | UIButton *firstButton = self.subviews.firstObject; 572 | 573 | // 指示器默认在第一个选中位置 574 | // 计算TitleLabel内容的Size 575 | CGSize buttonSize = [self sizeWithText:firstButton.titleLabel.text font:[UIFont systemFontOfSize:btn_fondOfSize] maxSize:CGSizeMake(MAXFLOAT, self.frame.size.height)]; 576 | if (self.segmentedControlType == SGSegmentedControlTypeScroll) { 577 | _indicatorView.SG_x = firstButton.SG_x; 578 | _indicatorView.SG_width = buttonSize.width + 2 * btn_Margin; 579 | } else { 580 | _indicatorView.SG_x = 0; 581 | _indicatorView.SG_width = self.SG_width / _title_Arr.count; 582 | } 583 | 584 | CGFloat indicatorViewHeight = self.frame.size.height; 585 | self.indicatorView.SG_height = indicatorViewHeight; 586 | self.indicatorView.SG_y = 0; 587 | 588 | self.indicatorView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.3]; 589 | 590 | // 指示器底部的小指示器 591 | self.bgIndicatorView = [[UIView alloc] init]; 592 | _bgIndicatorView.backgroundColor = [UIColor redColor]; 593 | 594 | if (self.segmentedControlType == SGSegmentedControlTypeScroll) { 595 | _bgIndicatorView.SG_x = firstButton.SG_x; 596 | _bgIndicatorView.SG_width = buttonSize.width + 2 * btn_Margin; 597 | } else { 598 | _bgIndicatorView.SG_x = _indicatorView.SG_x; 599 | _bgIndicatorView.SG_width = self.SG_width / _title_Arr.count; 600 | } 601 | 602 | CGFloat bgIndicatorViewHeight = 3; 603 | _bgIndicatorView.SG_height = bgIndicatorViewHeight; 604 | _bgIndicatorView.SG_y = _indicatorView.SG_height - bgIndicatorViewHeight; 605 | [self.indicatorView addSubview:_bgIndicatorView]; 606 | } 607 | } 608 | 609 | 610 | @end 611 | 612 | --------------------------------------------------------------------------------