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;
}
}
相關文章
- iOS實現超酷頁面切換動畫特效iOS動畫特效
- 點選按鈕實現切換頁面背景顏色效果
- mui 子頁面切換父頁面底部導航UI
- [提問交流]切換頁面,左側選單消失
- tpextbuilder- HasBuilder(封裝頁面)UI封裝
- WPF手動實現切換頁面
- 頁面無重新整理切換實現
- 微信小程式頁面功能-----標籤切換微信小程式
- 元件化頁面:封裝el-table元件化封裝
- history API 和錨點連結在單頁應用頁面切換中的應用API
- Xamarin iOS教程之頁面控制元件iOS控制元件
- 針對IOS頁面縮放,需要點選兩次才能選中iOS
- js通過contentWindow控制iframe子頁面元素點選事件,並把值傳給父頁面JS事件
- 防止頁面按鈕多次點選
- 如何解決setInterval 與頁面切換問題
- webbrowser 控制 頁面Web
- GAT新Bug:點選分頁按鈕頁面沒變
- 切換頁面主題樣式研究及less教程
- CSS實現頁面切換時的滑動效果CSS
- flutter實戰6:TAB頁面切換免重繪Flutter
- Android使用Fragment打造萬能頁面切換框架AndroidFragment框架
- 複雜type頁面封裝庫,支援多種狀態切換和下拉重新整理上拉載入封裝
- iOS 面向協議方式封裝空白頁功能iOS協議封裝
- 直播系統中網頁類似app頁面切換動畫的實現方式網頁APP動畫
- iOS頁面翻轉iOS
- H5單頁面手勢滑屏切換原理H5
- 快速構建H5單頁面切換應用H5
- 頁面怎麼做點選表頭排序排序
- 模擬在頁面點選匯入csv
- 解決ionic2/ionic3輪播圖切換頁面或者點選過後不自動輪圖
- vue 獲取頁面詳情後 切換頁面時 如何監聽使用者是否修改過資訊Vue
- 關於微信 H5 頁面切換 webview 的問題H5WebView
- router-view子頁面呼叫父頁面方法更新父頁面引數View
- iOS多重巢狀頁面iOS巢狀
- iOS 頁面效能優化iOS優化
- 高仿網易新聞客戶端首頁滑動切換頁面框架--LXScrollContentView客戶端框架View
- 封裝頭部切換檢視封裝
- 使用Bootstrap tab頁切換的使用boot