美文网首页
UIscrollView 与 UIpageControl使用

UIscrollView 与 UIpageControl使用

作者: leonardni | 来源:发表于2017-03-28 10:08 被阅读116次

    1.初始化

    @property(nonatomic,strong)UIView *subContentView;//父视图
    @property(nonatomic,strong)UIScrollView *funcScrollerView;//功能scrollerview
    @property(nonatomic,strong)UIView  *funcContent;//功能Content
    @property(nonatomic,strong)UIPageControl *pageControl;//page控制
    
    -(UIView *)subContentView{
    
        if(_subContentView == nil){
            _subContentView = [[UIView alloc]initWithFrame:CGRectMake(0, kcycleViewH, kScreenWidth, ksubContentH)];
            _subContentView.backgroundColor = [UIColor whiteColor];
        }
        return _subContentView;
    }
    
    -(UIScrollView *)funcScrollerView{
        if(_funcScrollerView == nil){
            _funcScrollerView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kfuncScrollerH)];
            _funcScrollerView.showsVerticalScrollIndicator = NO;
            _funcScrollerView.showsHorizontalScrollIndicator = NO;
            _funcScrollerView.bounces = NO;
            _funcScrollerView.contentSize = CGSizeMake(kScreenWidth * 2, kfuncScrollerH);
            _funcScrollerView.indicatorStyle = UIScrollViewIndicatorStyleDefault;
            _funcScrollerView.pagingEnabled = YES;
            _funcScrollerView.delegate = self;
        }
        return _funcScrollerView;
    }
    
    -(UIView *)funcContent{
        if(_funcContent == nil){
            _funcContent = [UIView new];
            _funcContent.backgroundColor = [UIColor whiteColor];
            _funcContent.frame = CGRectMake(0, 0, kScreenWidth*2, kfuncScrollerH);
            //添加按钮
           [_ItemsArray enumerateObjectsUsingBlock:^(MMHomeBtnItemModel*  _Nonnull model, NSUInteger idx, BOOL * _Nonnull stop) {
               CGFloat BtnItemW = kScreenWidth / 4;
               CGFloat BtnItemH = kfuncScrollerH;
               MMHomeFuncBtnItem *btnItem = [[MMHomeFuncBtnItem alloc]initWithFrame:CGRectMake(idx+idx*BtnItemW, 0, BtnItemW, BtnItemH)];
               [btnItem setImage:[UIImage imageNamed:model.icon] forState:UIControlStateNormal];
               [btnItem setTitle:model.title forState:UIControlStateNormal];
               [btnItem setTitleColor:HEXCOLOR(0x333333) forState:UIControlStateNormal];
               [btnItem.titleLabel setFont:[UIFont systemFontOfSize:14.0f]];
               [btnItem.titleLabel setTextAlignment:NSTextAlignmentCenter];
               btnItem.tag = kTagOffset + idx;
               [btnItem addTarget:self action:@selector(HHFuncBtnClick:) forControlEvents:UIControlEventTouchUpInside];
               [_funcContent addSubview:btnItem];
           }];
        }
        return _funcContent;
    }
    
    -(UIPageControl *)pageControl{
        if(_pageControl  == nil){
            _pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, ksubContentH - kpageControlH, kScreenWidth, kpageControlH)];
            _pageControl.currentPage = 0;
            _pageControl.numberOfPages = 2.0f;
            _pageControl.pageIndicatorTintColor = HEXCOLOR(0xcccccc);
            _pageControl.currentPageIndicatorTintColor = HEXCOLOR(kBlueColor);
            [_pageControl addTarget:self action:@selector(pageChange:)
             
                  forControlEvents:UIControlEventValueChanged];
        }
        return _pageControl;
        
    }
    
    

    2.视图层级

    [self addSubview:self.subContentView];
        [self.subContentView addSubview:self.funcScrollerView];
        [self.funcScrollerView addSubview:self.funcContent];
        [self.subContentView addSubview:self.pageControl];
    

    3.关联事件

    #pragma mark - 功能按钮点击
    -(void)HHFuncBtnClick:(UIButton *)sender{
        NSUInteger tag = sender.tag - kTagOffset;
        NSLog(@"%lu",tag);
        _FuncBtnClickBlock?_FuncBtnClickBlock(tag):nil;
    }
    
    #pragma mark - scrollViewDidScroll
    - (void)scrollViewDidScroll:(UIScrollView *)scrollView{
        if(scrollView == _funcScrollerView){
        int current = scrollView.contentOffset.x/kScreenWidth;
        _pageControl.currentPage = current;
        }
    }
    
    #pragma mark - 分页符点击
    -(void)pageChange:(UIPageControl *)control{
        CGFloat x = control.currentPage * kScreenWidth;
        [_funcScrollerView setContentOffset:CGPointMake(x,0) animated:YES];
    }
    

    最终效果:

    相关文章

      网友评论

          本文标题:UIscrollView 与 UIpageControl使用

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