diff --git a/XXPageController.podspec b/XXPageController.podspec index 2d4a905..5a9a81a 100644 --- a/XXPageController.podspec +++ b/XXPageController.podspec @@ -16,7 +16,7 @@ Pod::Spec.new do |spec| # spec.name = "XXPageController" - spec.version = "0.0.4" + spec.version = "0.0.7" spec.summary = "分页加载控制器." spec.description = <<-DESC diff --git a/XXPageController.xcodeproj/project.pbxproj b/XXPageController.xcodeproj/project.pbxproj index ffb9adc..ff56fcd 100755 --- a/XXPageController.xcodeproj/project.pbxproj +++ b/XXPageController.xcodeproj/project.pbxproj @@ -167,7 +167,7 @@ C4F8F6E91C5F41270030181A /* Project object */ = { isa = PBXProject; attributes = { - LastUpgradeCheck = 0830; + LastUpgradeCheck = 1120; ORGANIZATIONNAME = pogo.inxx; TargetAttributes = { C4F8F6F01C5F41270030181A = { @@ -178,10 +178,9 @@ }; buildConfigurationList = C4F8F6EC1C5F41270030181A /* Build configuration list for PBXProject "XXPageController" */; compatibilityVersion = "Xcode 3.2"; - developmentRegion = English; + developmentRegion = en; hasScannedForEncodings = 0; knownRegions = ( - English, en, Base, ); @@ -255,18 +254,27 @@ isa = XCBuildConfiguration; buildSettings = { ALWAYS_SEARCH_USER_PATHS = NO; + CLANG_ANALYZER_LOCALIZABILITY_NONLOCALIZED = YES; CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; CLANG_CXX_LIBRARY = "libc++"; CLANG_ENABLE_MODULES = YES; CLANG_ENABLE_OBJC_ARC = YES; + CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; CLANG_WARN_BOOL_CONVERSION = YES; + CLANG_WARN_COMMA = YES; CLANG_WARN_CONSTANT_CONVERSION = YES; + CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; CLANG_WARN_EMPTY_BODY = YES; CLANG_WARN_ENUM_CONVERSION = YES; CLANG_WARN_INFINITE_RECURSION = YES; CLANG_WARN_INT_CONVERSION = YES; + CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; + CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; + CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; + CLANG_WARN_STRICT_PROTOTYPES = YES; CLANG_WARN_SUSPICIOUS_MOVE = YES; CLANG_WARN_UNREACHABLE_CODE = YES; CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; @@ -289,7 +297,7 @@ GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; GCC_WARN_UNUSED_FUNCTION = YES; GCC_WARN_UNUSED_VARIABLE = YES; - IPHONEOS_DEPLOYMENT_TARGET = 9.1; + IPHONEOS_DEPLOYMENT_TARGET = 9.0; MTL_ENABLE_DEBUG_INFO = YES; ONLY_ACTIVE_ARCH = YES; SDKROOT = iphoneos; @@ -300,18 +308,27 @@ isa = XCBuildConfiguration; buildSettings = { ALWAYS_SEARCH_USER_PATHS = NO; + CLANG_ANALYZER_LOCALIZABILITY_NONLOCALIZED = YES; CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; CLANG_CXX_LIBRARY = "libc++"; CLANG_ENABLE_MODULES = YES; CLANG_ENABLE_OBJC_ARC = YES; + CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; CLANG_WARN_BOOL_CONVERSION = YES; + CLANG_WARN_COMMA = YES; CLANG_WARN_CONSTANT_CONVERSION = YES; + CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; CLANG_WARN_EMPTY_BODY = YES; CLANG_WARN_ENUM_CONVERSION = YES; CLANG_WARN_INFINITE_RECURSION = YES; CLANG_WARN_INT_CONVERSION = YES; + CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; + CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; + CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; + CLANG_WARN_STRICT_PROTOTYPES = YES; CLANG_WARN_SUSPICIOUS_MOVE = YES; CLANG_WARN_UNREACHABLE_CODE = YES; CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; @@ -328,7 +345,7 @@ GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; GCC_WARN_UNUSED_FUNCTION = YES; GCC_WARN_UNUSED_VARIABLE = YES; - IPHONEOS_DEPLOYMENT_TARGET = 9.1; + IPHONEOS_DEPLOYMENT_TARGET = 9.0; MTL_ENABLE_DEBUG_INFO = NO; SDKROOT = iphoneos; VALIDATE_PRODUCT = YES; diff --git a/XXPageController.xcodeproj/xcshareddata/xcschemes/XXPageController.xcscheme b/XXPageController.xcodeproj/xcshareddata/xcschemes/XXPageController.xcscheme new file mode 100644 index 0000000..527f362 --- /dev/null +++ b/XXPageController.xcodeproj/xcshareddata/xcschemes/XXPageController.xcscheme @@ -0,0 +1,85 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/XXPageController/ExampleController/PageCell1Controller.m b/XXPageController/ExampleController/PageCell1Controller.m index b34a354..621a2ef 100755 --- a/XXPageController/ExampleController/PageCell1Controller.m +++ b/XXPageController/ExampleController/PageCell1Controller.m @@ -15,44 +15,62 @@ @interface PageCell1Controller () @property (nonatomic, strong) UIActivityIndicatorView *indicatorView; @end -#define kNavAndStatus_Height ([[UIApplication sharedApplication] statusBarFrame].size.height + 44) - +//#define kNavAndStatus_Height ([[UIApplication sharedApplication] statusBarFrame].size.height + 44) +#define isIPhoneX (CGSizeEqualToSize(CGSizeMake(375.f, 812.f), [UIScreen mainScreen].bounds.size) || CGSizeEqualToSize(CGSizeMake(812.f, 375.f), [UIScreen mainScreen].bounds.size)) //是否是iphoneX +//底部安全高度 +#define kBottom_Safe_Height (isIPhoneX ? 34 : 0) @implementation PageCell1Controller -(void)viewDidLoad{ [super viewDidLoad]; self.view.backgroundColor = [UIColor yellowColor]; - [self addTableView]; } --(UIView *)addHeaderView{ - UIImageView *header = [[UIImageView alloc] initWithFrame:(CGRect){{0, 0}, {self.view.bounds.size.width, 200}}]; - [header setImage:[UIImage imageNamed:@"bg"]]; - header.contentMode = UIViewContentModeScaleAspectFill; - header.clipsToBounds = YES; - - return header; +- (void)viewWillAppear:(BOOL)animated { + [super viewWillAppear:animated]; + //当调用_childController.view.frame时,触发ChildViewController的-(void)viewDidLoad方法执行,但是这个时候_childController.view还没被添加在父视图上呢,所以坐标是被设置了,但是还没完成设置,也就是没有最终落地到实处。所以tableView去self.view.bounds还是取得默认的试图控制器的宽和高,默认的宽和高是屏幕的宽和高,所以导致了上述问题。 + //在 viewWillAppear/viewWillLayoutSubviews/viewDidLayoutSubviews 中 subview 的 frame 是准确的 +// if (!_tableView) { +// [self addTableView]; +// } + +} + +- (void)viewWillLayoutSubviews { + [super viewWillLayoutSubviews]; +// _tableView.frame = self.view.bounds; +// _indicatorView.center = _tableView.center; } +- (void)viewDidLayoutSubviews { + [super viewDidLayoutSubviews]; + _tableView.frame = self.view.frame; + _indicatorView.center = _tableView.center; +} + + -(void)addTableView{ CGRect frame = self.view.bounds; - frame.size.height = frame.size.height - kNavAndStatus_Height; + //frame.size.height = frame.size.height - kNavAndStatus_Height; _tableView = [[UITableView alloc] initWithFrame:frame]; + _tableView.delegate = self; _tableView.dataSource = self; - _tableView.tableHeaderView = [self addHeaderView]; _tableView.tableFooterView = [[UIView alloc] init]; + [self.view addSubview:_tableView]; + _tableView.hidden = YES; _indicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray]; _indicatorView.hidesWhenStopped = YES; + _indicatorView.color = [UIColor redColor]; _indicatorView.center = _tableView.center; [self.view addSubview:_indicatorView]; [_indicatorView startAnimating]; dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ - [_indicatorView stopAnimating]; - [self.view addSubview:_tableView]; + [self->_indicatorView stopAnimating]; + self->_tableView.hidden = NO; }); } @@ -63,7 +81,7 @@ -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ } -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ - return 10; + return 12; } -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ diff --git a/XXPageController/ExampleController/PageCell2Controller.m b/XXPageController/ExampleController/PageCell2Controller.m index ea7e829..ff2be5d 100755 --- a/XXPageController/ExampleController/PageCell2Controller.m +++ b/XXPageController/ExampleController/PageCell2Controller.m @@ -14,17 +14,22 @@ @interface PageCell2Controller () @property (nonatomic, strong) UIActivityIndicatorView *indicatorView; @end -#define kNavAndStatus_Height ([[UIApplication sharedApplication] statusBarFrame].size.height + 44) +//#define kNavAndStatus_Height ([[UIApplication sharedApplication] statusBarFrame].size.height + 44) @implementation PageCell2Controller -(void)viewDidLoad{ [super viewDidLoad]; - self.view.backgroundColor = [UIColor whiteColor]; + self.view.backgroundColor = [UIColor brownColor]; - [self addTableView]; +// [self addTableView]; +} +- (void)viewWillAppear:(BOOL)animated { + [super viewWillAppear:animated]; + if (!_tableView) { + [self addTableView]; + } } - -(UIView *)addHeaderView{ UIImageView *header = [[UIImageView alloc] initWithFrame:(CGRect){{0, 0}, {self.view.bounds.size.width, 200}}]; [header setImage:[UIImage imageNamed:@"bg"]]; @@ -36,7 +41,7 @@ -(UIView *)addHeaderView{ -(void)addTableView{ CGRect frame = self.view.bounds; - frame.size.height = frame.size.height - kNavAndStatus_Height; +// frame.size.height = frame.size.height - kNavAndStatus_Height - 44; _tableView = [[UITableView alloc] initWithFrame:frame]; _tableView.delegate = self; _tableView.dataSource = self; @@ -51,8 +56,8 @@ -(void)addTableView{ [_indicatorView startAnimating]; dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ - [_indicatorView stopAnimating]; - [self.view addSubview:_tableView]; + [self->_indicatorView stopAnimating]; + [self.view addSubview:self->_tableView]; }); } @@ -63,7 +68,7 @@ -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ } -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ - return 10; + return 20; } -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ diff --git a/XXPageController/ExampleController/Parent3Controller.m b/XXPageController/ExampleController/Parent3Controller.m index ad52203..1a947de 100644 --- a/XXPageController/ExampleController/Parent3Controller.m +++ b/XXPageController/ExampleController/Parent3Controller.m @@ -26,9 +26,8 @@ - (void)viewDidLoad { [titles enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { [controllers addObject:[PageCell1Controller new]]; }]; - XXPageMenuController *pageMenuController = [[XXPageMenuController alloc] initWithTitles:titles iconNames:iconNames controllers:controllers onNavigationBar:YES]; + XXPageMenuController *pageMenuController = [[XXPageMenuController alloc] initWithTitles:titles iconNames:iconNames controllers:controllers onNavigationBar:NO]; pageMenuController.lineColor = [UIColor orangeColor]; - pageMenuController.titleFont = [UIFont systemFontOfSize:14 weight:UIFontWeightLight]; pageMenuController.titleColor = [UIColor colorWithWhite:0.2 alpha:1]; pageMenuController.titleSelectedColor = [UIColor blackColor]; pageMenuController.pageBarBgColor = [UIColor whiteColor]; diff --git a/XXPageController/ExampleController/Parent4Controller.m b/XXPageController/ExampleController/Parent4Controller.m index 1682500..019057c 100644 --- a/XXPageController/ExampleController/Parent4Controller.m +++ b/XXPageController/ExampleController/Parent4Controller.m @@ -13,13 +13,23 @@ @interface Parent4Controller () @end +#define kScreenWidth [[UIScreen mainScreen] bounds].size.width +#define kScreenHeight [[UIScreen mainScreen] bounds].size.height +#define kNavAndStatus_Height ([[UIApplication sharedApplication] statusBarFrame].size.height + 44) @implementation Parent4Controller - (void)viewDidLoad { [super viewDidLoad]; - self.view.backgroundColor = [UIColor whiteColor]; + self.view.backgroundColor = [UIColor redColor]; self.title = NSStringFromClass(self.class); + + UIImageView *header = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 150)]; + [header setImage:[UIImage imageNamed:@"bg"]]; + header.contentMode = UIViewContentModeScaleAspectFill; + header.clipsToBounds = YES; + [self.view addSubview:header]; + NSArray *titles = @[@"新闻",@"体育",@"汽车",@"房产"]; NSMutableArray *controllers = [NSMutableArray array]; @@ -28,18 +38,35 @@ - (void)viewDidLoad { }]; XXPageMenuController *pageMenuController = [[XXPageMenuController alloc] initWithTitles:titles controllers:controllers onNavigationBar:NO]; pageMenuController.lineColor = [UIColor orangeColor]; - pageMenuController.titleFont = [UIFont systemFontOfSize:14 weight:UIFontWeightLight]; + pageMenuController.titleFont = [UIFont systemFontOfSize:13]; pageMenuController.titleColor = [UIColor colorWithWhite:0.2 alpha:1]; pageMenuController.titleSelectedColor = [UIColor blackColor]; - pageMenuController.pageBarBgColor = [UIColor whiteColor]; + pageMenuController.pageBarBgColor = [UIColor greenColor]; pageMenuController.pageBarHeight = 44; pageMenuController.lineWidthType = LineWidthTypeStaticShort; - pageMenuController.lineScrollType = arc4random() % 2 + 1; //1,2随机 LineScrollTypeFinishedAnimation 或者 LineScrollTypeFinishedLinear; - + //pageMenuController.lineScrollType = arc4random() % 2 + 1; //1,2随机 LineScrollTypeFinishedAnimation 或者 LineScrollTypeFinishedLinear; + pageMenuController.defaultIndex = 1; + pageMenuController.originY = 50; //因为这里是将pageMenuController添加到ParentController(self类)上的,所以要为pageMenuController设置父视图控制器 - [pageMenuController setSuperViewController:self]; +// [pageMenuController setSuperViewController:self]; + [self addChildViewController:pageMenuController]; + [self.view addSubview:pageMenuController.view]; + +// dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(6 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ +// +// CGFloat tableViewH = kScreenHeight - kNavAndStatus_Height - pageMenuController.pageBarHeight - 150; +// //pageMenuController.view.frame = CGRectMake(0, pageMenuController.pageBarHeight + 150, kScreenWidth, tableViewH); +// for (PageCell1Controller *vc in controllers) { +// vc.tableViewH = tableViewH; +// [vc reloadDataAndUI]; +// } +// }); + +} + +- (void)viewDidAppear:(BOOL)animated { + [super viewDidAppear:animated]; - pageMenuController.defaultIndex = 1; } - (void)didReceiveMemoryWarning { diff --git a/XXPageController/ViewController.m b/XXPageController/ViewController.m index 5986c0f..9f29e88 100755 --- a/XXPageController/ViewController.m +++ b/XXPageController/ViewController.m @@ -21,7 +21,7 @@ @interface ViewController () @property(nonatomic,strong)UIColor *color ; @end -#define SCREEN_Width ([[UIScreen mainScreen] bounds].size.width) +#define kScreenWidth [[UIScreen mainScreen] bounds].size.width @implementation ViewController @@ -38,7 +38,7 @@ - (void)viewDidLoad { } -(UIView *)addHeaderView{ - UIImageView *header = [[UIImageView alloc] initWithFrame:CGRectMake(0, -64, SCREEN_Width, 200)]; + UIImageView *header = [[UIImageView alloc] initWithFrame:CGRectMake(0, -64, kScreenWidth, 200)]; [header setImage:[UIImage imageNamed:@"bg"]]; header.contentMode = UIViewContentModeScaleAspectFill; header.clipsToBounds = YES; @@ -56,7 +56,7 @@ -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger) } - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{ - UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, SCREEN_Width, 30)]; + UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 30)]; label.backgroundColor = [UIColor colorWithWhite:0.8 alpha:0.5]; label.font = [UIFont systemFontOfSize:12]; label.textAlignment = NSTextAlignmentLeft; @@ -141,7 +141,7 @@ -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath * { NSMutableArray *controllersClass = [NSMutableArray array]; [titles enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { - [controllersClass addObject:[PageCell2Controller class]]; + [controllersClass addObject:[PageCell1Controller class]]; }]; pageMenuController = [[XXPageMenuController alloc] initWithTitles:titles controllersClass:controllersClass onNavigationBar:YES]; @@ -152,7 +152,7 @@ -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath * { NSMutableArray *controllers = [NSMutableArray array]; [titles enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { - [controllers addObject:[PageCell1Controller new]]; + [controllers addObject:[PageCell2Controller new]]; }]; pageMenuController = [[XXPageMenuController alloc] initWithTitles:titles controllers:controllers onNavigationBar:NO]; pageMenuController.titleColor = [UIColor grayColor]; @@ -171,12 +171,12 @@ -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath * break; case 2: { - pageMenuController = [[XXPageMenuController alloc] initWithTitles:titles2 controllersClass:@[[PageCell2Controller class],[PageCell2Controller class],[PageCell2Controller class],[PageCell2Controller class]] onNavigationBar:YES]; + pageMenuController = [[XXPageMenuController alloc] initWithTitles:titles2 controllersClass:@[[PageCell1Controller class],[PageCell1Controller class],[PageCell1Controller class],[PageCell1Controller class]] onNavigationBar:YES]; } break; default: { - pageMenuController = [[XXPageMenuController alloc] initWithTitles:titles2 controllersClass:@[[PageCell1Controller class],[PageCell1Controller class],[PageCell1Controller class],[PageCell2Controller class]] onNavigationBar:NO]; + pageMenuController = [[XXPageMenuController alloc] initWithTitles:titles2 controllersClass:@[[PageCell2Controller class],[PageCell2Controller class],[PageCell2Controller class],[PageCell2Controller class]] onNavigationBar:NO]; } break; } diff --git a/XXPageController/XXPageMenuController/XXPageMenuController.h b/XXPageController/XXPageMenuController/XXPageMenuController.h index b1f747a..93a1c91 100755 --- a/XXPageController/XXPageMenuController/XXPageMenuController.h +++ b/XXPageController/XXPageMenuController/XXPageMenuController.h @@ -109,6 +109,9 @@ typedef NS_ENUM(NSInteger, PageTitleColorChangeType) { /** 默认选择的 index 位置 ,默认值为0*/ @property (nonatomic,assign) NSInteger defaultIndex; +/** 分页控制器View视图的 Y 轴方向 设置初始位置(适用于分页条不在导航条上的情况) , 动态位置由于UICollectionViewCell 的复用存在不可知 bug, 暂不实现*/ +@property (nonatomic, assign) CGFloat originY; + /** 创建分页控制器 : 自动创建全部控制器(方式一) @@ -148,4 +151,7 @@ typedef NS_ENUM(NSInteger, PageTitleColorChangeType) { */ - (void)setSuperViewController:(UIViewController *)superVc; +/// 【自定义页面的frame】在界面viewDidLayoutSubviews/组件layoutSubviews后调用,让外界有机会修改frame等 +@property (nonatomic, copy) void (^didLayoutSubviewsBlock)(UIView *pageMenuControllerView, UIScrollView *scrollViewPage, UICollectionView *collectionMain); + @end diff --git a/XXPageController/XXPageMenuController/XXPageMenuController.m b/XXPageController/XXPageMenuController/XXPageMenuController.m index bfa1283..a100e3b 100755 --- a/XXPageController/XXPageMenuController/XXPageMenuController.m +++ b/XXPageController/XXPageMenuController/XXPageMenuController.m @@ -11,15 +11,12 @@ #define kScreenWidth [[UIScreen mainScreen] bounds].size.width #define kScreenHeight [[UIScreen mainScreen] bounds].size.height #define kNavAndStatus_Height ([[UIApplication sharedApplication] statusBarFrame].size.height + 44) -#define isIPhoneX (CGSizeEqualToSize(CGSizeMake(375.f, 812.f), [UIScreen mainScreen].bounds.size) || CGSizeEqualToSize(CGSizeMake(812.f, 375.f), [UIScreen mainScreen].bounds.size)) //是否是iphoneX -//底部安全高度 -#define kBottom_Safe_Height (isIPhoneX ? 34 : 0) @interface PopEnabeldCollectionView : UICollectionView @end @implementation PopEnabeldCollectionView - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer { - //解决横向滚动的scrollView和系统pop手势返回冲突 + //处理横向滚动的scrollView和系统pop手势返回冲突 if ([otherGestureRecognizer.view isKindOfClass:NSClassFromString(@"UILayoutContainerView")] && otherGestureRecognizer.state == UIGestureRecognizerStateBegan && self.contentOffset.x == 0) return YES; return NO; } @@ -44,7 +41,6 @@ - (instancetype)initWithFrame:(CGRect)frame { _titleLabel = [[UILabel alloc] initWithFrame:self.bounds]; _titleLabel.textAlignment = NSTextAlignmentCenter; _titleLabel.lineBreakMode = NSLineBreakByCharWrapping; -// _titleLabel.backgroundColor = [UIColor greenColor]; _titleLabel.userInteractionEnabled = YES; UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(titleClick)]; [_titleLabel addGestureRecognizer:tap]; @@ -72,38 +68,7 @@ - (void)titleClick { @end -//@interface ItemCell : UICollectionViewCell -//@property (nonatomic, strong) UILabel *titleLabel; -//@property (nonatomic, strong) UIButton *titleBtn; -//@end -// -//@implementation ItemCell -//- (instancetype)initWithFrame:(CGRect)frame { -// self = [super initWithFrame:frame]; -// if (self) { -// self.backgroundColor = [UIColor clearColor]; -// _titleLabel = [[UILabel alloc] initWithFrame:self.bounds]; -// _titleLabel.textAlignment = NSTextAlignmentCenter; -// _titleLabel.lineBreakMode = NSLineBreakByCharWrapping; -// _titleLabel.backgroundColor = [UIColor greenColor]; -// [self.contentView addSubview:_titleLabel]; -// -// _titleBtn = [[UIButton alloc] initWithFrame:self.bounds]; -// _titleBtn.contentMode = UIViewContentModeCenter; -// _titleBtn.titleEdgeInsets = UIEdgeInsetsMake(0, 3, 0, 0); -// _titleBtn.imageEdgeInsets = UIEdgeInsetsMake(1, -3, 0, 0); -// CGSize size = CGSizeMake(15, 15); -// _titleBtn.imageView.frame = (CGRect){{0,0},size}; -// _titleBtn.titleLabel.lineBreakMode = NSLineBreakByCharWrapping; -// _titleBtn.userInteractionEnabled = NO; -// [self.contentView addSubview:_titleBtn]; -// } -// return self; -//} -//@end - @interface XXPageMenuController () - /** 分页条滑动的下滑线 */ @property (readonly, nonatomic, strong) UIView *line; /** 下滑线宽度 */ @@ -111,11 +76,7 @@ @interface XXPageMenuController ()*titles; @property (nonatomic, strong) NSArray *controllersClass; @property (nonatomic, strong) NSArray *controllers; @property (nonatomic, strong) NSArray *icons; - /** 当前选中的 index 位置 */ @property (nonatomic, assign) NSInteger selectedIndex; /** 点击didSelectItemAtIndexPath:方法的标记 */ @property (nonatomic,assign) BOOL didSelectCollectionPageItem; - -//@property (nonatomic, strong) ItemCell *lastTimeCell; - @end #define kAnimateDuration 0.3 -static NSString *pageBarCell = @"inxx_pageBarCell"; static NSString *mainCell = @"inxx_mainCell"; @implementation XXPageMenuController @@ -195,22 +151,21 @@ - (void)setSuperViewController:(UIViewController *)superVc { [superVc.view addSubview:self.view]; } +- (void)viewDidLoad { + [super viewDidLoad]; + self.view.backgroundColor = [UIColor whiteColor]; + [self configProperties]; + [self addScrollViewPage]; + [self addCollectionMain]; + [self addPageBottomLine]; + [self addObserver:self forKeyPath:@"selectedIndex" options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld context:nil]; +} + - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; - //将XXPageMenuController对象的view被添加到另一个控制器的view上时就会发生if内的情况 - if (self.nextResponder && - self.view.superview && - self.view.superview == self.nextResponder && - [self.view.superview.nextResponder isKindOfClass:[UIViewController class]] - ) { - UIViewController *controller = (UIViewController *)self.view.superview.nextResponder; - UIViewController *controller2 = self.parentViewController; - //解决一个view上面放两个不同的collectionview的显示冲突 - controller.automaticallyAdjustsScrollViewInsets = NO; - } else { - self.automaticallyAdjustsScrollViewInsets = NO; + if (self.parentViewController ) { + self.parentViewController.edgesForExtendedLayout = UIRectEdgeNone; } - //1. 更新下划线的frame [self updateLineFrameWithIndex:self.selectedIndex]; //2. 更新collection page的显示index 位置 @@ -223,90 +178,70 @@ - (void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; //FIXME: iOS 11前后在_onNavigationBar上面时,子视图出现的顺序是不一样的! iOS11前需要在UINavigationController的视图显示以后,子视图才会显示! iOS 11之后可能修复了这个bug if (_onNavigationBar && [UIDevice currentDevice].systemVersion.doubleValue<=11.0 && self.selectedIndex >= _maxPagesCountInPageShowArea) { -// [self.collectionPage scrollToItemAtIndexPath:[NSIndexPath indexPathForRow:self.selectedIndex inSection:0] atScrollPosition:UICollectionViewScrollPositionLeft animated:NO]; [self updateCurrentScrollViewPageContentOffsetByIndex:self.selectedIndex]; } } -- (void)viewDidLoad { - [super viewDidLoad]; - self.view.backgroundColor = [UIColor clearColor]; - [self configProperties]; -// [self addCollectionPage]; - [self addScrollViewPage]; - [self addCollectionMain]; - [self addPageBottomLine]; - - [self addObserver:self forKeyPath:@"selectedIndex" options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld context:nil]; -} - - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { -// if ( object == self && [keyPath isEqualToString:@"selectedIndex"]) { -// NSInteger oldIndex = [[change objectForKey:NSKeyValueChangeOldKey] integerValue]; -// NSInteger newIndex = [[change objectForKey:NSKeyValueChangeNewKey] integerValue]; -// NSIndexPath *oldIndexPath = [NSIndexPath indexPathForRow:oldIndex inSection:0]; -// NSIndexPath *newIndexPath = [NSIndexPath indexPathForRow:newIndex inSection:0]; -// ItemCell *oldCell = (ItemCell *)[self.collectionPage cellForItemAtIndexPath:oldIndexPath]; -// ItemCell *newCell = (ItemCell *)[self.collectionPage cellForItemAtIndexPath:newIndexPath]; -// if (newIndex != oldIndex) { -// //FIXME: 此处由于 cell 的重用/缓存池机制, 当两个 cell 切换位置过远, 超过一屏时, oldCell不显示在当前屏幕, 会进入缓存池, 取到的值是 nil, 需要特殊处理来让我们准确取到这个 cell. 而在-scrollViewDidScroll:中的此取 cell 方法不会发生此种情况,因为都是相邻的两个 cell 间的滑动 -// if (oldCell == nil) { -// oldCell = _lastTimeCell; -// } -// [self reloadCollectionPageCell:oldCell index:oldIndex selected:NO]; -// [self reloadCollectionPageCell:newCell index:newIndex selected:YES]; -// } -// _lastTimeCell = newCell; //赋值 -// } - if ([keyPath isEqualToString:@"selectedIndex"]) { NSInteger oldIndex = [[change objectForKey:NSKeyValueChangeOldKey] integerValue]; NSInteger newIndex = [[change objectForKey:NSKeyValueChangeNewKey] integerValue]; - XXPageItemCell *oldCell = _pageCells[oldIndex]; - XXPageItemCell *newCell = _pageCells[newIndex]; - if (newIndex != oldIndex) { - [self reloadCollectionPageCell:oldCell index:oldIndex selected:NO]; - [self reloadCollectionPageCell:newCell index:newIndex selected:YES]; + [self reloadCollectionPageCell:_pageCells[oldIndex] index:oldIndex selected:NO]; + [self reloadCollectionPageCell:_pageCells[newIndex] index:newIndex selected:YES]; } } - - } /// 刷新 CollectionPage的Cell 数据 /// @param selected 待刷新cell是否被选中 - (void)reloadCollectionPageCell:(XXPageItemCell *)cell index:(NSInteger)index selected:(BOOL)selected { + + UILabel *titleLabel = nil; if (self.icons) { cell.titleLabel.hidden = YES; cell.titleBtn.hidden = NO; - [cell.titleBtn setTitle:self.titles[index] forState:UIControlStateNormal]; + titleLabel = cell.titleBtn.titleLabel; [cell.titleBtn setImage:[UIImage imageNamed:self.icons[index]] forState:UIControlStateNormal]; - [cell.titleBtn setTitleColor:(selected ? _titleSelectedColor : _titleColor) forState:UIControlStateNormal]; [cell.titleBtn.titleLabel setFont:(selected ? _titleSelectedFont : _titleFont)]; } else { cell.titleLabel.hidden = NO; cell.titleBtn.hidden = YES; - [cell.titleLabel setText:self.titles[index]]; - [cell.titleLabel setTextColor:(selected ? _titleSelectedColor : _titleColor)]; + titleLabel = cell.titleLabel; //title font 的改变方式: 滑动结束动画改变 || 点击didSelectItemAtIndexPath:方法 if (_pageTitleFontChangeType == PageTitleFontChangeTypeScrollEndAnimation || _didSelectCollectionPageItem) { CGFloat scale = selected ? _titleSelectedFont.pointSize/_titleFont.pointSize : _titleFont.pointSize/_titleSelectedFont.pointSize; [UIView animateWithDuration:kAnimateDuration animations:^{ - cell.titleLabel.transform = CGAffineTransformMakeScale(scale, scale); + titleLabel.transform = CGAffineTransformMakeScale(scale, scale); } completion:^(BOOL finished) { //缩放结束后重置transform, 并设置当前对应的字体大小. 这样就能避免transform从小到大时字体模糊 - cell.titleLabel.transform = CGAffineTransformIdentity; - [cell.titleLabel setFont:(selected ? _titleSelectedFont : _titleFont)]; + titleLabel.transform = CGAffineTransformIdentity; + [titleLabel setFont:(selected ? self->_titleSelectedFont : self->_titleFont)]; }]; } else { - [cell.titleLabel setFont:(selected ? _titleSelectedFont : _titleFont)]; + [titleLabel setFont:(selected ? _titleSelectedFont : _titleFont)]; } } + + [titleLabel setText:self.titles[index]]; + [titleLabel setTextColor:(selected ? _titleSelectedColor : _titleColor)]; } - (void)configProperties { + //edgesForExtendedLayout 边缘延伸属性,默认为UIRectEdgeAll. + //UIRectEdgeAll 当前视图控制器里各种UI控件会忽略导航栏和标签的存在,布局时若设置其原点设置为(0,0),视图会延伸显示到导航栏的下面被覆盖; + //UIRectEdgeNone: 意味着子控件本身会自动躲避导航栏和标签栏,以免被遮挡。 + self.edgesForExtendedLayout = UIRectEdgeNone; + if (self.parentViewController ) { + self.parentViewController.edgesForExtendedLayout = UIRectEdgeNone; + } + if (_originY != 0) { + CGRect frame = self.view.frame; + frame.origin.y += _originY; + frame.size.height -= _originY; + self.view.frame = frame; + } + //init default value - _cellidDic = [NSMutableDictionary dictionary]; _pageBarHeight = _onNavigationBar ? 44 : (_pageBarHeight ?: 44); _pageBarBgColor = _pageBarBgColor ? : [UIColor whiteColor]; _lineColor = _lineColor ? : [UIColor blueColor]; @@ -319,7 +254,6 @@ - (void)configProperties { _titleColor = _titleColor ? : [UIColor colorWithWhite:0.1 alpha:1]; _titleSelectedColor = _titleSelectedColor ?: [UIColor blackColor]; _pageMenuW = _onNavigationBar ? (kScreenWidth - 120) : kScreenWidth; //120为预估的左右navigationItem的总宽度 - if (_pageCellWidthType == PageCellWidthTypeSplitScreen) { //根据titles.count平分宽度 _pageCellW = (_titles.count <= _maxPagesCountInPageShowArea) ? _pageMenuW / _titles.count : _pageMenuW / _maxPagesCountInPageShowArea; @@ -335,6 +269,9 @@ - (void)configProperties { CGFloat width1 = title.length * _titleFont.pointSize + 25; CGFloat width2 = title.length * _titleSelectedFont.pointSize; //防止有少数过长标题时滑动异常 CGFloat width = MAX(width1, width2); + if (self.icons) { + width += 24; + } sumWidth += width; if (sumWidth < _pageMenuW) { showCount++; @@ -347,52 +284,14 @@ - (void)configProperties { self.selectedIndex = _defaultIndex; } -//- (void)addCollectionPage { -// -// UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init]; -// layout.minimumLineSpacing = 0; -// layout.minimumInteritemSpacing = 0; -// layout.scrollDirection = UICollectionViewScrollDirectionHorizontal; -// if (_pageCellWidthType != PageCellWidthTypeByTitleLength) { -// layout.itemSize = CGSizeMake(_pageCellW, _pageBarHeight); -// } -// CGFloat pageMenuY = _onNavigationBar ? 0 : kNavAndStatus_Height; -// CGRect frame = CGRectMake(0, pageMenuY, _pageMenuW, _pageBarHeight); -// UICollectionView *collection = [[UICollectionView alloc] initWithFrame:frame collectionViewLayout:layout]; -// collection.dataSource = self; -// collection.delegate = self; -// collection.pagingEnabled = YES; -// collection.scrollEnabled = YES; -// collection.bounces = YES; //禁止左右弹簧拉伸 -// collection.showsHorizontalScrollIndicator = NO; -// //[collection registerClass:[ItemCell class] forCellWithReuseIdentifier:pageBarCell]; -// self.collectionPage = collection; -// -// if (_onNavigationBar) { -// collection.backgroundColor = [UIColor clearColor]; //位于导航条时背景色处理为透明色,不公开属性 -// if (self.parentViewController && ![self.parentViewController isKindOfClass:[UINavigationController class]]) { -// self.parentViewController.navigationItem.titleView = self.collectionPage; -// } else { -// self.navigationItem.titleView = self.collectionPage; -// } -// } else { -// collection.backgroundColor = _pageBarBgColor; -// [self.view addSubview:self.collectionPage]; -// -// UIView *borderline = [[UIView alloc] initWithFrame:CGRectMake(0, collection.bounds.size.height - 0.5, _pageCellW*_titles.count, 0.5)]; -// borderline.backgroundColor = [UIColor colorWithWhite:0.75 alpha:0.5]; -// //[collection addSubview:borderline]; -// } -// -//} - - (void)addScrollViewPage { - CGFloat pageMenuY = _onNavigationBar ? 0 : kNavAndStatus_Height; - CGRect frame = CGRectMake(0, pageMenuY, _pageMenuW, _pageBarHeight); + + CGRect frame = CGRectMake(0, 0, _pageMenuW, _pageBarHeight); UIScrollView *scrollViewPage = [[UIScrollView alloc] initWithFrame:frame]; scrollViewPage.showsVerticalScrollIndicator = NO; scrollViewPage.showsHorizontalScrollIndicator = NO; scrollViewPage.delegate = self; + if (_onNavigationBar) { scrollViewPage.backgroundColor = [UIColor clearColor]; //位于导航条时背景色处理为透明色,不公开属性 if (self.parentViewController && ![self.parentViewController isKindOfClass:[UINavigationController class]]) { @@ -416,8 +315,11 @@ - (void)addScrollViewPage { _pageCells = [NSMutableArray array]; __block CGFloat currentX = 0; - [_pageCellWs enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { - CGFloat pageCellW = [obj floatValue]; + [self.titles enumerateObjectsUsingBlock:^(NSString * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { + CGFloat pageCellW = _pageCellW; + if (_pageCellWidthType == PageCellWidthTypeByTitleLength) { + pageCellW = [self.pageCellWs[idx] floatValue]; + } CGRect frame = CGRectMake(currentX, 0, pageCellW, _pageBarHeight); currentX += pageCellW; XXPageItemCell *pageItemCell = [[XXPageItemCell alloc] initWithFrame:frame]; @@ -446,41 +348,24 @@ - (void)addScrollViewPage { self.scrollViewPage = scrollViewPage; } - - - (void)addPageBottomLine { _line = [UIView new]; _line.backgroundColor = _lineColor; _line.clipsToBounds = YES; _line.layer.cornerRadius = _lineHeight/2; -// [self.collectionPage addSubview:_line]; -// [self.collectionPage bringSubviewToFront:_line]; - [self.scrollViewPage addSubview:_line]; [self.scrollViewPage bringSubviewToFront:_line]; } +CGRect childFrame; - (void)addCollectionMain { - UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init]; layout.minimumLineSpacing = 0; layout.minimumInteritemSpacing = 0; layout.scrollDirection = UICollectionViewScrollDirectionHorizontal; - - CGRect frame; - if (_onNavigationBar) { - //分页条 onNavigationBar && isIPhoneX && parentViewController && parentViewController不是NavigationController类型 - if (isIPhoneX && self.parentViewController && ![self.parentViewController isKindOfClass:[UINavigationController class]]) { - layout.itemSize = CGSizeMake(kScreenWidth, kScreenHeight - kNavAndStatus_Height - kBottom_Safe_Height); - } else { - layout.itemSize = CGSizeMake(kScreenWidth, kScreenHeight - kNavAndStatus_Height); - } - frame = CGRectMake(0, kNavAndStatus_Height, layout.itemSize.width, layout.itemSize.height); - } else { - layout.itemSize = CGSizeMake(kScreenWidth, kScreenHeight - kNavAndStatus_Height- _pageBarHeight);//kBottom_Safe_Height - frame = CGRectMake(0, CGRectGetMaxY(self.scrollViewPage.frame), layout.itemSize.width, layout.itemSize.height); - } - + CGFloat y = _onNavigationBar ? 0 : _pageBarHeight; + layout.itemSize = CGSizeMake(self.view.bounds.size.width, kScreenHeight - kNavAndStatus_Height - y - _originY); + CGRect frame = CGRectMake(0, y, layout.itemSize.width, layout.itemSize.height); PopEnabeldCollectionView *collection = [[PopEnabeldCollectionView alloc] initWithFrame:frame collectionViewLayout:layout]; collection.backgroundColor = [UIColor whiteColor]; collection.dataSource = self; @@ -493,116 +378,31 @@ - (void)addCollectionMain { self.collectionMain = collection; [self.view addSubview:collection]; [self.view bringSubviewToFront:self.collectionMain]; -} - -#pragma mark - UICollectionViewDelegateFlowLayout -- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath { -// if (collectionView == self.collectionPage) { -// if (_pageCellWidthType == PageCellWidthTypeByTitleLength) { -// CGFloat width = [_pageCellWs[indexPath.item] floatValue]; -// return CGSizeMake(width, _pageBarHeight); -// } -// return CGSizeMake(_pageCellW, _pageBarHeight); -// } else { - if (_onNavigationBar) { - return CGSizeMake(kScreenWidth, kScreenHeight - kNavAndStatus_Height - kBottom_Safe_Height); - } - return CGSizeMake(kScreenWidth, kScreenHeight - kNavAndStatus_Height- _pageBarHeight);//kBottom_Safe_Height -// } + + childFrame = frame; + for (UIViewController *childVc in self.controllers) { + //子控制器的frame.origin.y肯定要从其自己的0开始 + childFrame.origin.y = 0; + childVc.view.frame = childFrame; + } } -#pragma mark - UICollectionViewDataSource && UICollectionViewDelegate +- (void)viewDidLayoutSubviews { + [super viewDidLayoutSubviews]; + if (self.didLayoutSubviewsBlock) { + self.didLayoutSubviewsBlock(self.view, self.scrollViewPage, self.collectionMain); + } +} - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { return self.titles.count; } - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { - -// if (collectionView == self.collectionPage) { -// // 每次先从字典中根据IndexPath取出唯一标识符 -// NSString *identifier = [_cellidDic objectForKey:[NSString stringWithFormat:@"%@", indexPath]]; -// // 如果取出的唯一标示符不存在,则初始化唯一标示符,并将其存入字典中,对应唯一标示符注册Cell -// if (identifier == nil) { -// identifier = [NSString stringWithFormat:@"%@%@", pageBarCell, [NSString stringWithFormat:@"%@", indexPath]]; -// [_cellidDic setValue:identifier forKey:[NSString stringWithFormat:@"%@", indexPath]]; -// // 注册Cell -// [collectionView registerClass:[ItemCell class] forCellWithReuseIdentifier:identifier]; -// } -// ItemCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath]; -// -// BOOL selected = (indexPath.row == self.selectedIndex); //是否被选中 -// if (selected) { -// _lastTimeCell = cell; //赋值 -// } -// -// if (self.icons) { -// cell.titleLabel.hidden = YES; cell.titleBtn.hidden = NO; -// [cell.titleBtn setTitle:self.titles[indexPath.row] forState:UIControlStateNormal]; -// [cell.titleBtn setImage:[UIImage imageNamed:self.icons[indexPath.row]] forState:UIControlStateNormal]; -// [cell.titleBtn setTitleColor:(selected ? _titleSelectedColor : _titleColor) forState:UIControlStateNormal]; -// [cell.titleBtn.titleLabel setFont:(selected ? _titleSelectedFont : _titleFont)]; -// } else { -// cell.titleLabel.hidden = NO; cell.titleBtn.hidden = YES; -// [cell.titleLabel setText:self.titles[indexPath.row]]; -// [cell.titleLabel setTextColor:(selected ? _titleSelectedColor : _titleColor)]; -// [cell.titleLabel setFont:(selected ? _titleSelectedFont : _titleFont)]; -// } -// -// return cell; -// } -// else -// { - UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:mainCell forIndexPath:indexPath]; - [cell.contentView addSubview:((UIViewController *)self.controllers[indexPath.row]).view]; - return cell; -// } -} - -- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath { - -// if (collectionView == self.collectionPage) { //self.collectionMain的点击不用判断,会被具体视图所遮挡 -// -// _didSelectCollectionPageItem = YES; -// -// BOOL needReset = NO; -// LineScrollType lineScrollType = _lineScrollType; -// PageTitleFontChangeType titleFontChangeType = _pageTitleFontChangeType; -// PageTitleColorChangeType titleColorChangeType = _pageTitleColorChangeType; -// if ((_lineScrollType != LineScrollTypeScrollEndLinear)) { -// _lineScrollType = LineScrollTypeScrollEndLinear; -// needReset = YES; -// } -// if (_pageTitleFontChangeType != PageTitleFontChangeTypeScrollEnd) { -// _pageTitleFontChangeType = PageTitleFontChangeTypeScrollEnd; -// needReset = YES; -// } -// if (_pageTitleColorChangeType != PageTitleColorChangeTypeScrollEnd) { -// _pageTitleColorChangeType = PageTitleColorChangeTypeScrollEnd; -// needReset = YES; -// } -// -// [UIView animateWithDuration:kAnimateDuration animations:^{ -// //ABS(self.selectedIndex - indexPath.item)>1 表示跨越至少 2 个 index 移动 -// [self updateLineFrameWithIndex:indexPath.row] ; -// } completion:^(BOOL finished) { -// if (needReset) { //复位 -// _lineScrollType = lineScrollType; -// _pageTitleFontChangeType = titleFontChangeType; -// _pageTitleColorChangeType = titleColorChangeType; -// } -// _didSelectCollectionPageItem = NO; -// }]; -// -// NSInteger index = _maxPagesCountInPageShowArea - 2; -// if (indexPath.row >= index) { -// [self.collectionPage scrollToItemAtIndexPath:[NSIndexPath indexPathForRow:indexPath.row - index inSection:0] atScrollPosition:UICollectionViewScrollPositionLeft animated:YES]; -// } -// -// [self.collectionMain scrollToItemAtIndexPath:[NSIndexPath indexPathForRow:indexPath.row inSection:0] atScrollPosition:UICollectionViewScrollPositionLeft animated:YES]; -// } - + UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:mainCell forIndexPath:indexPath]; + [cell.contentView addSubview:((UIViewController *)self.controllers[indexPath.row]).view]; + return cell; } #pragma mark - XXPageItemCellDelegate @@ -632,11 +432,11 @@ - (void)pageItemCell:(XXPageItemCell *)pageItemCell didSelectItemAtIndex:(NSInte [self updateLineFrameWithIndex:index] ; } completion:^(BOOL finished) { if (needReset) { //复位 - _lineScrollType = lineScrollType; - _pageTitleFontChangeType = titleFontChangeType; - _pageTitleColorChangeType = titleColorChangeType; + self->_lineScrollType = lineScrollType; + self->_pageTitleFontChangeType = titleFontChangeType; + self->_pageTitleColorChangeType = titleColorChangeType; } - _didSelectCollectionPageItem = NO; + self->_didSelectCollectionPageItem = NO; }]; @@ -675,7 +475,7 @@ - (void)changePageLinePropertyiesWithSrollX:(CGFloat)x { CGFloat changedW; ///< line动态改变的宽度 CGFloat centerX; ///< 线的实时滑动中心 - NSLog(@"scrollRatio - %.1f",scrollRatio); + //NSLog(@"scrollRatio - %.1f",scrollRatio); BOOL toLeft = (x > oldOffsetX); oldOffsetX = x; if (toLeft) { @@ -753,13 +553,6 @@ - (void)changePageTitlePropertyiesWithSrollX:(CGFloat)x { //创建通用变量 int pageIndex = ABS(x) / kScreenWidth; CGFloat xInScreen = x-kScreenWidth*pageIndex; //一屏内的位移距离 -// NSIndexPath *oldIndexPath = [NSIndexPath indexPathForRow:pageIndex inSection:0]; -// NSIndexPath *newIndexPath = [NSIndexPath indexPathForRow:pageIndex+1 inSection:0]; -// ItemCell *oldCell = (ItemCell *)[self.collectionPage cellForItemAtIndexPath:oldIndexPath]; -// ItemCell *newCell = (ItemCell *)[self.collectionPage cellForItemAtIndexPath:newIndexPath]; -// if (newCell == nil) { -// newCell = _lastTimeCell; -// } XXPageItemCell *oldCell = _pageCells[pageIndex]; XXPageItemCell *newCell = nil; @@ -776,8 +569,7 @@ - (void)changePageTitlePropertyiesWithSrollX:(CGFloat)x { //oldCell.titleLabel.font = [UIFont fontWithDescriptor:oldCell.titleLabel.font.fontDescriptor size:_titleSelectedFont.pointSize-fontRuntimeDifferenceSize]; //newCell.titleLabel.font = [UIFont fontWithDescriptor:newCell.titleLabel.font.fontDescriptor size:_titleFont.pointSize+fontRuntimeDifferenceSize]; - //working中的 2 个分页标题实时属性在滑动时的稳定防抖动效果 - //transform形变防抖动处理 + //transform形变稳定防抖动处理 //避免transform从小到大时字体模糊: 先把font设置为缩放的最大值,再缩小到最小值,最后根据当前的titleLabelZoomScale值,进行缩放更新 newCell.titleLabel.font = _titleSelectedFont; oldCell.titleLabel.font = _titleSelectedFont; @@ -787,16 +579,6 @@ - (void)changePageTitlePropertyiesWithSrollX:(CGFloat)x { newCell.titleLabel.transform = CGAffineTransformMakeScale(baseScale*upRatio, baseScale*upRatio); oldCell.titleLabel.transform = CGAffineTransformMakeScale(baseScale*downRatio, baseScale*downRatio); - //FIXME: 防止 cell 在复用时取到未复位的 cell -// for (ItemCell *cell in self.collectionPage.visibleCells) { -// NSInteger row = [self.collectionPage indexPathForCell:cell].row; -// if (row != pageIndex && row != pageIndex+1) { -// cell.titleLabel.transform = CGAffineTransformIdentity; -// cell.titleLabel.font = _titleFont; -// } -// } - - [_pageCells enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull view, NSUInteger idx, BOOL * _Nonnull stop) { if ([view isKindOfClass:[XXPageItemCell class]]) { XXPageItemCell *cell = view; @@ -821,7 +603,6 @@ - (void)changePageTitlePropertyiesWithSrollX:(CGFloat)x { CGFloat runingred1 = [rgba1[0] floatValue] - diffR; CGFloat runinGreen1 = [rgba1[1] floatValue] - diffG; CGFloat runingBlue1 = [rgba1[2] floatValue] - diffB; - //colorWithRed:red/255.0f green:green/255.0f blue:blue/255.0f 是错误写法,原色值里面已经包含了 x/255.0 的处理 UIColor *runtimeColor0 = [UIColor colorWithRed:runingRed0 green:runingGreen0 blue:runingBlue0 alpha:1.0]; UIColor *runtimeColor1 = [UIColor colorWithRed:runingred1 green:runinGreen1 blue:runingBlue1 alpha:1.0]; oldCell.titleLabel.textColor = runtimeColor1; @@ -831,10 +612,7 @@ - (void)changePageTitlePropertyiesWithSrollX:(CGFloat)x { /** 系统API获取UIColor的RGBA值 */ - (NSArray *)getRGBValueFromColor:(UIColor *)color { - CGFloat red = 0.0; - CGFloat green = 0.0; - CGFloat blue = 0.0; - CGFloat alpha = 0.0; + CGFloat red = 0.0, green = 0.0, blue = 0.0, alpha = 0.0; [color getRed:&red green:&green blue:&blue alpha:&alpha]; return @[@(red), @(green), @(blue), @(alpha)]; } @@ -844,80 +622,52 @@ - (NSArray *)getRGBValueFromColor:(UIColor *)color { - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView { if (_didSelectCollectionPageItem) return; - - CGFloat x = scrollView.contentOffset.x ; - int index = (x + kScreenWidth*0.5) / kScreenWidth; //滑动切换基准选择: kScreenWidth*0.5(半屏) - - if (scrollView == self.collectionMain && _lineScrollType == LineScrollTypeScrollEndLinear) { - [UIView animateWithDuration:kAnimateDuration animations:^{ - [self updateLineFrameWithIndex:index]; - }]; - } - if (scrollView != self.scrollViewPage) { + if (scrollView == _collectionMain) { + + CGFloat x = scrollView.contentOffset.x ; + int index = (x + kScreenWidth*0.5) / kScreenWidth; //滑动切换基准选择: kScreenWidth*0.5(半屏) + + // ScrollViewPage 处理偏移:setContentOffset [self updateCurrentScrollViewPageContentOffsetByIndex:index]; - } - - if (_pageTitleFontChangeType == PageTitleFontChangeTypeScrolling && scrollView == self.collectionMain) { - //FIXME: 防止 cell 在复用时取到未复位的 cell - int pageIndex = ABS(x) / kScreenWidth; -// for (ItemCell *cell in self.collectionPage.visibleCells) { -// NSInteger row = [self.collectionPage indexPathForCell:cell].row; -// if (row != pageIndex) { -// cell.titleLabel.transform = CGAffineTransformIdentity; -// cell.titleLabel.font = _titleFont; -// } -// } - [_pageCells enumerateObjectsUsingBlock:^(__kindof XXPageItemCell * _Nonnull cell, NSUInteger idx, BOOL * _Nonnull stop) { - if (idx != pageIndex) { - cell.titleLabel.transform = CGAffineTransformIdentity; - cell.titleLabel.font = _titleFont; - } - }]; + // 下划线线性动画 + if ( _lineScrollType == LineScrollTypeScrollEndLinear) { + [UIView animateWithDuration:kAnimateDuration animations:^{ + [self updateLineFrameWithIndex:index]; + }]; + } + + if (_pageTitleFontChangeType == PageTitleFontChangeTypeScrolling) { + //FIXME: 防止 cell 在复用时取到未复位的 cell + int pageIndex = ABS(x) / kScreenWidth; + [_pageCells enumerateObjectsUsingBlock:^(__kindof XXPageItemCell * _Nonnull cell, NSUInteger idx, BOOL * _Nonnull stop) { + if (idx != pageIndex) { + cell.titleLabel.transform = CGAffineTransformIdentity; + cell.titleLabel.font = _titleFont; + } + }]; + } } } -/// 更新当前使用的分页ScrollView偏移量 +/// 更新当前使用的分页ScrollView偏移量,复现UICollectionView滑动到view边缘处理的大致流程 /// @param index 当前位置 - (void)updateCurrentScrollViewPageContentOffsetByIndex:(NSInteger)index { - //用collectionPage时的操作 -// if (self.collectionPage) { -// NSInteger needScrollIndex = _maxPagesCountInPageShowArea - 2; -// if (index >= needScrollIndex) { -// [self.collectionPage scrollToItemAtIndexPath:[NSIndexPath indexPathForRow:index - needScrollIndex inSection:0] atScrollPosition:UICollectionViewScrollPositionLeft animated:YES]; -// } -// } else { - ///< 复现UICollectionView滑动边缘处理的大致流程 - NSArray *currentArray = [_pageCellWs subarrayWithRange:NSMakeRange(0, index)]; - CGFloat currentOffX = [[currentArray valueForKeyPath:@"@sum.floatValue"] floatValue]; //↑ - CGFloat allOffX = [[_pageCellWs valueForKeyPath:@"@sum.floatValue"] floatValue]; // - -// if (currentOffX > kScreenWidth*0.5) { -// currentOffX = currentOffX - kScreenWidth*0.5; //防止滑动到最左边分页条继续左滑的异常 -// if (currentOffX <= allOffX-kScreenWidth) { -// [self.scrollViewPage setContentOffset:CGPointMake(currentOffX, 0) animated:YES]; -// } else { -// [self.scrollViewPage setContentOffset:CGPointMake(allOffX-kScreenWidth, 0) animated:YES]; -// } -// } else { -// [self.scrollViewPage setContentOffset:CGPointMake(0, 0) animated:YES]; -// } - - CGFloat likeScreenWidth = _pageMenuW; //考虑进去咋 navbar 上的情况 - - if (currentOffX < likeScreenWidth*0.5) { + NSArray *currentArray = [_pageCellWs subarrayWithRange:NSMakeRange(0, index)]; + CGFloat currentOffX = [[currentArray valueForKeyPath:@"@sum.floatValue"] floatValue]; //↑ + CGFloat contentWidth = [[_pageCellWs valueForKeyPath:@"@sum.floatValue"] floatValue]; // - + if (contentWidth <= _pageMenuW) return; //contentSize宽度小于_pageMenuW时,不偏移 + if (currentOffX < _pageMenuW*0.5) { [self.scrollViewPage setContentOffset:CGPointMake(0, 0) animated:YES]; + } else if (currentOffX >= contentWidth-_pageMenuW*0.6) { + [self.scrollViewPage setContentOffset:CGPointMake(contentWidth-_pageMenuW, 0) animated:YES]; } else { - if (currentOffX >= allOffX-likeScreenWidth*0.5) { - [self.scrollViewPage setContentOffset:CGPointMake(allOffX-likeScreenWidth, 0) animated:YES]; - } else { - [self.scrollViewPage setContentOffset:CGPointMake(currentOffX-likeScreenWidth*0.5, 0) animated:YES]; - } + [self.scrollViewPage setContentOffset:CGPointMake(currentOffX-_pageMenuW*0.4, 0) animated:YES]; } -// } } /** 更新/设置 下划线的 frame */