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

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/walkerwqp/article/details/79986179

#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.widthself.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


#import "SquarePageViewController.h"

#import "WNavigationTabBar.h"

#import "DiscoverViewController.h"

#import "BusinessController.h"

#import "DiscoverTopicReleaseVC.h"

#import "YQNavigationController.h"

#import "YQGroupTableItem.h"


@interface SquarePageViewController ()<UIPageViewControllerDelegate,UIPageViewControllerDataSource>


@property (nonatomic, strong) WNavigationTabBar *navigationTabBar;

@property (nonatomic, strong) NSArray<UIViewController *> *subViewControllers;

@property (nonatomic, strong) UIButton  *rightBut;


@end


@implementation SquarePageViewController


-(WNavigationTabBar *)navigationTabBar

{

    if (!_navigationTabBar) {

        self.navigationTabBar = [[WNavigationTabBar alloc] initWithTitles:@[@"职场",@"发现"]];

        __weak typeof(self) weakSelf = self;

        [self.navigationTabBar setDidClickAtIndex:^(NSInteger index){

            [weakSelf navigationDidSelectedControllerIndex:index];

        }];

    }

    return _navigationTabBar;

}


-(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;

}



- (void)viewDidLoad {

    [super viewDidLoad];

    self.view.backgroundColor = [UIColor whiteColor];

    self.navigationItem.titleView = self.navigationTabBar;

    self.delegate = self;

    self.dataSource = self;

    

    [self setViewControllers:@[self.subViewControllers.firstObject]

                   direction:UIPageViewControllerNavigationDirectionForward

                    animated:YES

                  completion:nil];

    

    self.rightBut = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 15, 15)];

    [self.rightBut setBackgroundImage:[UIImage imageNamed:@"discover_release"] forState:UIControlStateNormal];

    [self.rightBut addTarget:self action:@selector(rightBtnSelector:) forControlEvents:UIControlEventTouchUpInside];

    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:self.rightBut];

    

    

}


#pragma mark - 点击事件

- (void)rightBtnSelector : (UIButton *)sender {

    NSLog(@"点击发布");

    

    NSInteger flag = 0;

    if ([UserEntity getIsCompany]) {

        NSInteger auth = [[UserEntity getRealAuth] integerValue];

        if (auth == 1) {

            flag = 1;

        }else if (auth == 2 || auth == 0){

            //去认证

            [self goAuth];

        }else if (auth == 3){

            // 等待审核

            [self waitExamine];

        }

    }else{

        NSInteger auth = [[UserEntity getRealAuth] integerValue];

        if (auth == 1) {

            flag = 1;

        }else if (auth == 2 || auth == 0){

            //去认证

            [self goAuth];

        }else if (auth == 3){

            // 等待审核

            [self waitExamine];

        }

    }

    

    

    if (flag == 1) {

        // 发布新话题

        DiscoverTopicReleaseVC *vc = [[DiscoverTopicReleaseVC alloc] init];

//        if (curTableIndex == 0) {

            vc.isdefault = @"2";// 职场

            vc.navigationItem.title = @"发职场动态";

            vc.placeholder = @"分享有价值的职场内容,为你的职业竞争力加分";

//        }else{

//            vc.navigationItem.title = @"发合伙人动态";

//            vc.placeholder = @"分享有价值的职场内容,为你的职业竞争力加分";

//            vc.isdefault = @"1";// 合伙人

//        }

        

        YQWeakSelf;

        vc.releaseSuccessBlock = ^(NSString *text) {

//            [weakSelf headerRereshing];;

        };

        YQNavigationController *nav = [[YQNavigationController alloc] initWithRootViewController:vc];

        [self presentViewController:nav animated:YES completion:nil];

    }

    

}


//- (void)headerRereshing

//{

//    YQGroupTableItem *item = [self.tableGroups objectAtIndex:curTableIndex];

//    item.pageCount = 1;

//    NSString *isdefault = @"1";// 合伙人

//    if (curTableIndex == 0) {

//        isdefault = @"2";// 职场

//    }

//    [self reqWorkplaceList:[NSString stringWithFormat:@"%li",item.pageCount] isdefault:isdefault];

//}


- (void)goAuth

{

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"友情提示" message:@"根据国家互联网政策规定要求请先进行实名认证,方能在职场社交中发布相关话题" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"去认证", nil];

    alert.tag = 1000;

    [alert show];

}


- (void)waitExamine

{

    [YQToast yq_AlertText:@"您的资料正在审核中,请耐心等待"];

}



#pragma mark - UIPageViewControllerDelegate

- (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(@"第一个页面");

            self.rightBut.hidden = NO;

            break;

            case 1:

            NSLog(@"第二个页面, 不显示发布按钮");

            self.rightBut.hidden = YES;

            break;

            

        default:

            break;

    }

    

    

}


- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}




@end






猜你喜欢

转载自blog.csdn.net/walkerwqp/article/details/79986179