美文网首页
iOS 封装页数控制,点击NavigationTabBar切换页

iOS 封装页数控制,点击NavigationTabBar切换页

作者: 龍飝 | 来源:发表于2018-04-18 11:10 被阅读127次

    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 封装页数控制,点击NavigationTabBar切换页

          本文链接:https://www.haomeiwen.com/subject/lckvkftx.html