iOS 封裝頁數控制,點選NavigationTabBar切換頁面
import <UIKit/UIKit.h>
typedef void(^TabBarDidClickAtIndex)(NSInteger buttonIndex);
@interface WNavigationTabBar : UIView
@property(nonatomic,copy)TabBarDidClickAtIndex didClickAtIndex;
-(instancetype)initWithTitles:(NSArray *)titles;
-(void)scrollToIndex:(NSInteger)index;
@property(nonatomic,strong)UIColor *sliderBackgroundColor;
@property(nonatomic,strong)UIColor *buttonNormalTitleColor;
@property(nonatomic,strong)UIColor *buttonSelectedTileColor;
@end
import "WNavigationTabBar.h"
@interface WNavigationTabBar ()
@property (nonatomic, strong) UIView *sliderView;
@property(nonatomic,strong)NSMutableArray<UIButton *> *buttonArray;
@property(nonatomic,assign)CGFloat width;
@property(nonatomic,strong)UIButton *selectedButton;
@end
@implementation WNavigationTabBar
-(instancetype)initWithTitles:(NSArray *)titles
{
if (self = [super initWithFrame:CGRectMake(0, 0, 150, 44)]) {
self.buttonNormalTitleColor = RGBA(255, 255, 255, 0.7);
self.buttonSelectedTileColor = [UIColor whiteColor];
[self setSubViewWithTitles:titles];
}
return self;
}
-(void)setSubViewWithTitles:(NSArray *)titles
{
self.buttonArray = [[NSMutableArray alloc] init];
for (int buttonIndex = 0 ; buttonIndex < titles.count; buttonIndex++) {
NSString *titleString = titles[buttonIndex];
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn setTitleColor:self.buttonNormalTitleColor forState:UIControlStateNormal];
[btn setTitleColor:self.buttonSelectedTileColor forState:UIControlStateSelected];
[btn setTitleColor:self.buttonSelectedTileColor forState:UIControlStateHighlighted | UIControlStateSelected];
[btn setTitle:titleString forState:UIControlStateNormal];
btn.titleLabel.font = [UIFont systemFontOfSize:16];
if(buttonIndex == 0) {btn.selected = YES; self.selectedButton = btn;};
[btn addTarget:self action:@selector(subButtonSelected:) forControlEvents:UIControlEventTouchUpInside];
btn.tag = 100 + buttonIndex;
[self addSubview:btn];
[self.buttonArray addObject:btn];
}
self.sliderView = [[UIView alloc] init];
self.sliderView.backgroundColor = self.buttonSelectedTileColor;
[self addSubview:self.sliderView];
}
-(void)subButtonSelected:(UIButton *)button
{
self.selectedButton.selected = NO;
button.selected = YES;
self.selectedButton = button;
[self sliderViewAnimationWithButtonIndex:button.tag - 100];
if (self.didClickAtIndex) {
self.didClickAtIndex(button.tag - 100);
}
}
-(void)scrollToIndex:(NSInteger)index
{
self.selectedButton.selected = NO;
self.buttonArray[index].selected = YES;
self.selectedButton = self.buttonArray[index];
[self sliderViewAnimationWithButtonIndex:index];
}
-(void)sliderViewAnimationWithButtonIndex:(NSInteger)buttonIndex
{
[UIView animateWithDuration:0.25 animations:^{
CGFloat buttonX = self.buttonArray[buttonIndex].center.x - (self.width /2);
self.sliderView.frame = CGRectMake(buttonX, self.frame.size.height - 2.0f, self.width - 4, 2);
}];
}
-(void)layoutSubviews
{
[super layoutSubviews];
self.width = self.frame.size.width / (self.buttonArray.count * 2);
CGFloat buttonWidth = self.frame.size.width / self.buttonArray.count;
for (int buttonIndex = 0; buttonIndex < self.buttonArray.count; buttonIndex ++) {
self.buttonArray[buttonIndex].frame = CGRectMake(buttonIndex * buttonWidth, 0, buttonWidth, 44);
}
CGFloat buttonX = self.buttonArray[0].center.x - self.width / 2;
self.sliderView.frame = CGRectMake(buttonX, self.frame.size.height - 2.0f, self.width - 4, 2);
}
@end
呼叫 建立UIPageViewController
遵循 UIPageViewController的代理 UIPageViewControllerDelegate,UIPageViewControllerDataSource
-(WNavigationTabBar *)navigationTabBar
{
if (!_navigationTabBar) {
self.navigationTabBar = [[WNavigationTabBar alloc] initWithTitles:@[@"職場",@"發現"]];
__weak typeof(self) weakSelf = self;
[self.navigationTabBar setDidClickAtIndex:^(NSInteger index){
[weakSelf navigationDidSelectedControllerIndex:index];
}];
}
return _navigationTabBar;
}
設定viewController陣列 傳入需要切換的介面
-(NSArray *)subViewControllers
{
if (!_subViewControllers) {
DiscoverViewController *controllerOne = [[DiscoverViewController alloc] init];
controllerOne.view.backgroundColor = RGB(239, 239, 239);
BusinessController *controllerTwo = [[BusinessController alloc] init];
controllerTwo.view.backgroundColor = RGB(239, 239, 239);
self.subViewControllers = @[controllerOne,controllerTwo];
}
return _subViewControllers;
}
切記切記 遵循代理 self.delegate = self;
self.dataSource = self;
實現代理方法
[self setViewControllers:@[self.subViewControllers.firstObject]
direction:UIPageViewControllerNavigationDirectionForward
animated:YES
completion:nil];
-
(nullable UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController
{
NSInteger index = [self.subViewControllers indexOfObject:viewController];
if(index == 0 || index == NSNotFound) {
return nil;
}return [self.subViewControllers objectAtIndex:index - 1];
} (nullable UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController
{
NSInteger index = [self.subViewControllers indexOfObject:viewController];
if(index == NSNotFound || index == self.subViewControllers.count - 1) {
return nil;
}
return [self.subViewControllers objectAtIndex:index + 1];
}(void)pageViewController:(UIPageViewController *)pageViewController
didFinishAnimating:(BOOL)finished
previousViewControllers:(NSArray *)previousViewControllers
transitionCompleted:(BOOL)completed {
UIViewController *viewController = self.viewControllers[0];
NSUInteger index = [self.subViewControllers indexOfObject:viewController];
[self.navigationTabBar scrollToIndex:index];
}
pragma mark - PrivateMethod
- (void)navigationDidSelectedControllerIndex:(NSInteger)index {
[self setViewControllers:@[[self.subViewControllers objectAtIndex:index]] direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];
switch (index) {
case 0:
NSLog(@"第一個頁面");
break;
case 1:
NSLog(@"第二個頁面");
break;
default:
break;
}
}
相關文章
- [提問交流]切換頁面,左側選單消失
- tpextbuilder- HasBuilder(封裝頁面)UI封裝
- WPF手動實現切換頁面
- 針對IOS頁面縮放,需要點選兩次才能選中iOS
- 切換電腦頁面、視窗快捷鍵
- iOS 面向協議方式封裝空白頁功能iOS協議封裝
- 點選頁面愛心效果
- 元件化頁面:封裝el-table元件化封裝
- 微信小程式頁面功能-----標籤切換微信小程式
- 防止頁面按鈕多次點選
- 複雜type頁面封裝庫,支援多種狀態切換和下拉重新整理上拉載入封裝
- 切換頁面主題樣式研究及less教程
- flutter實戰6:TAB頁面切換免重繪Flutter
- CSS實現頁面切換時的滑動效果CSS
- 快速構建H5單頁面切換應用H5
- H5單頁面手勢滑屏切換原理H5
- 解決ionic2/ionic3輪播圖切換頁面或者點選過後不自動輪圖
- 直播系統中網頁類似app頁面切換動畫的實現方式網頁APP動畫
- 模擬在頁面點選匯入csv
- 頁面怎麼做點選表頭排序排序
- 小程式標籤頁切換效果
- 使用Bootstrap tab頁切換的使用boot
- vue 獲取頁面詳情後 切換頁面時 如何監聽使用者是否修改過資訊Vue
- 用CSS實現Tab頁切換效果CSS
- router-view子頁面呼叫父頁面方法更新父頁面引數View
- HTML頁面轉換為Sharepoint母版頁(實戰)HTML
- 直播平臺搭建,切換頁面 捲軸預設最頂端
- 關於微信 H5 頁面切換 webview 的問題H5WebView
- vue頁面表格元件高度控制Vue元件
- 那些年踩過的坑——h5頁面在ios端點選高亮閃爍H5iOS
- Flutter 特定頁面切換螢幕方向/iOS強制橫屏/SystemChrome.setPreferredOrientations不起作用 看這裡!FlutteriOSChrome
- 成品直播原始碼,html頁面點選按鈕實現頁面跳轉的兩種方法原始碼HTML
- 我的頁面切圖[美工專用]
- jQuery點選平滑跳轉到頁面指定位置jQuery
- Laya頁面過後fgui介面不能點選問題GUI
- JavaScript 點選頁面其他地方關閉視窗JavaScript
- 點選連結跳轉到應用指定頁面
- js禁用頁面所有輸入框以及點選事件JS事件