美文网首页
iOS - 写滚动界面 并且导航上有滚动条

iOS - 写滚动界面 并且导航上有滚动条

作者: HanZhiZzzzz | 来源:发表于2017-06-22 15:51 被阅读0次
#import "PublishSubjectVC.h"
#import "WaitExamineV.h"
#import "DidExamineV.h"
#import "HomeNavV.h"

#define lightVW   38


@interface PublishSubjectVC ()
<
UIScrollViewDelegate,
HomeNavVDelegate
>
{
    NSInteger _tabCount ;
}
/** 待审核界面*/
@property (nonatomic,strong) WaitExamineV *waitExamineV;
/** 已审核界面*/
@property (nonatomic,strong) DidExamineV *didExamineV;
/** 导航栏名称*/
@property (nonatomic,strong) NSArray *navNames;
/** 导航栏*/
@property (nonatomic,strong) HomeNavV *homeHavV;
/** 整个视图的大小*/
@property (assign) CGRect mViewFrame;
/** 下方的ScrollView*/
@property (strong, nonatomic) UIScrollView *scrollView;
/** 下方的表格数组*/
@property (strong, nonatomic) NSMutableArray *scrollTableViews;
/** 当前选中页数*/
@property (assign) NSInteger currentPage;
/** 上面滑动的View*/
@property (strong, nonatomic) UIView *slideView;

@end

@implementation PublishSubjectVC
-- (void)viewDidLoad {

    [super viewDidLoad];
    // 界面
    [self initUserInterface];
}
#pragma mark -- Action
// 返回按钮点击事件
-(void)navBackClick{
   
 [self.navigationController popViewControllerAnimated:YES];

}
#pragma mark -- SelectedTutorsVDelegate
#pragma mark -- SelectedTopicVDelegate
#pragma mark -- HomeNavVDelegate
-(void)tabButton:(UIButton *)btn{

    UIButton *button = btn;
    [_scrollView setContentOffset:CGPointMake((button.tag - HomeNavBtnTag) * _mViewFrame.size.width, 0) animated:YES];

}
#pragma mark -- scrollView的代理方法
-(void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView{

    [self scrollViewDidEndDecelerating:scrollView];

}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{

    _currentPage = _scrollView.contentOffset.x/_mViewFrame.size.width;
}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{

    if ([_scrollView isEqual:scrollView]) {
        CGRect frame = _slideView.frame;
        frame.origin.x = scrollView.contentOffset.x/_tabCount;
        CGFloat minX = (S_W / 2 - 58 - 38 );
        if (frame.origin.x <= minX ) {
            frame.origin.x = minX;
            [self.homeHavV paperBtnClick];
        }else if (frame.origin.x >= (S_W / 2)){
            [self.homeHavV noticeBtnClick];
            NSString *secondTitle = self.navNames[1];
            frame.origin.x += (WidthOfLabel(18, secondTitle) / 2 - lightVW / 2);
        }
        _slideView.frame = frame;
    }

}

#pragma mark -- 界面
-(void)initUserInterface{

    self.navV.hidden = YES;
    // 基础信息
    _mViewFrame = CGRM(0, 0, S_W, S_H);
    _tabCount = self.navNames.count;
    _scrollTableViews = [[NSMutableArray alloc] init];
    
    // 实例化ScrollView
    _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, _mViewFrame.origin.y, _mViewFrame.size.width, _mViewFrame.size.height)];
    _scrollView.contentSize = CGSizeMake(_mViewFrame.size.width * _tabCount, _mViewFrame.size.height - 60);
    _scrollView.backgroundColor = DefaultBackGroundColor;
    _scrollView.pagingEnabled = YES;
    _scrollView.showsHorizontalScrollIndicator = NO;
    _scrollView.bounces = NO;
    _scrollView.delegate = self;
    [self.view addSubview:_scrollView];
    
    // 添加导航栏
    HomeNavV *homeHavV = [[HomeNavV alloc]initWithFrame:CGRM(0, 0, S_W, 64)];
    homeHavV.nameSArr = self.navNames;
    homeHavV.delegate = self;
    [self.view addSubview:homeHavV];
    self.homeHavV = homeHavV;
    // 返回按钮
    UIButton *backBtn = MakeButton(kClearColor);
    [backBtn addTarget:self action:@selector(navBackClick) forControlEvents:UIControlEventTouchUpInside];
    [homeHavV addSubview:backBtn];
    // 返回图片
    UIImageView *backImage = MakeImageViewWith(@"detial_back");
    [backBtn addSubview:backImage];
    // 返回按钮
    backBtn.frame = CGRM(0, 20, 50, 44);
    // 返回图片
    CGFloat backImageWH = 17;
    backImage.frame = CGRM(15, (backBtn.height - backImageWH) / 2, backImageWH, backImageWH);
    // 初始化下方的两个V
    for (int i = 0; i < _tabCount; i ++) {
        if (i == 0) {
            WaitExamineV *waitExamineV = [[WaitExamineV alloc]initWithFrame:CGRM(0, 64, S_W, S_H - 64)];
            //waitExamineV.delegate = self;
            [_scrollTableViews addObject:waitExamineV];
            [_scrollView addSubview:waitExamineV];
            self.waitExamineV = waitExamineV;
        }else if (i == 1){
            DidExamineV *didExamineV = [[DidExamineV alloc]initWithFrame:CGRM(S_W, 64, S_W, S_H - 64)];
            //didExamineV.delegate  = self;
            [_scrollTableViews addObject:didExamineV];
            [_scrollView insertSubview:didExamineV belowSubview:self.waitExamineV];
            self.didExamineV = didExamineV;
        }
    }
    // 设置阴影
    self.waitExamineV.layer.shadowColor = [UIColor grayColor].CGColor;
    self.waitExamineV.layer.shadowOffset = CGSizeMake(3, 0);
    self.waitExamineV.layer.shadowOpacity = 0.1;
    // 设置阴影
    self.didExamineV.layer.shadowColor = [UIColor grayColor].CGColor;
    self.didExamineV.layer.shadowOffset = CGSizeMake(3, 0);
    self.didExamineV.layer.shadowOpacity = 0.1;
    
    // 初始化滑动的指示View
    _slideView = [[UIView alloc] initWithFrame:CGRectMake((S_W / 2 - 58 - lightVW), 64 - 3, 58 + lightVW, 3)];
    [_slideView setBackgroundColor:kClearColor];
    [self.view addSubview:_slideView];
    
    UIView *lightV = MakeLineView(_slideView, DefaultBackGroundColor);
    lightV.frame = CGRM(29 , 0, lightVW, 2);
}

#pragma mark -- getter
// 导航栏名称
-(NSArray *)navNames{

    if (!_navNames) {
        _navNames = @[@"待审核",@"已审核"];
    }
    return _navNames;
}




@end

HomeNavV:https://pan.baidu.com/s/1geYJy3D

相关文章

网友评论

      本文标题:iOS - 写滚动界面 并且导航上有滚动条

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