頁(yè)面滾動(dòng)按鈕

我們今天來(lái)寫一個(gè)頁(yè)面滾動(dòng)按鈕
首先我們?cè)贏ppDelegate.m里創(chuàng)建個(gè)導(dǎo)航條
ViewController *vc = [[ViewController alloc]init];

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

self.window.rootViewController = nav;

我們創(chuàng)建幾個(gè)viewcontroller作為滾動(dòng)的視圖,我們就先創(chuàng)建六個(gè)吧,


屏幕快照 2018-12-02 下午6.56.23.png

然后我們?cè)谥饕晥D添加它們
導(dǎo)入頭文件

import "oneViewController.h"

import "twoViewController.h"

import "threeViewController.h"

import "fourViewController.h"

import "fiveViewController.h"

import "sixViewController.h"

import "tiaoViewController.h"

遵守協(xié)議
UIScrollViewDelegate

設(shè)置全局按鈕

{
UIScrollView *scr;
UIButton *bun;
UIScrollView *center;
}
@property (nonatomic,strong)UIButton *selbun;

然后

  • (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc]initWithTitle:@"問(wèn)問(wèn)" style:UIBarButtonItemStylePlain target:self action:@selector(qq1)];

    [self setUpVc];
    //添加頂部滾動(dòng)式圖
    [self setTopVc];
    //添加點(diǎn)擊按鈕
    [self setUpBun];
    //設(shè)置中間滾動(dòng)
    [self setCenterVc];

}

-(void)setUpVc{
oneViewController *one=[[oneViewController alloc]init];
one.title=@"XX";
[self addChildViewController:one];

twoViewController *two=[[twoViewController alloc]init];
two.title=@"XX";
[self addChildViewController:two];

threeViewController *three=[[threeViewController alloc]init];
three.title=@"XX";
[self addChildViewController:three];

fourViewController *four=[[fourViewController alloc]init];
four.title=@"XX";
[self addChildViewController:four];

fiveViewController *five=[[fiveViewController alloc]init];
five.title=@"XX";
[self addChildViewController:five];

sixViewController *six=[[sixViewController alloc]init];
six.title=@"XX";
[self addChildViewController:six];

}

-(void)setTopVc{
scr=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 70, self.view.frame.size.width,40)];
scr.bounces=NO ;
self.navigationItem.titleView =scr;
}

-(void)setUpBun{
NSInteger count=self.childViewControllers.count;
for (NSInteger i=0; i<count; i++) {
bun=[[UIButton alloc]initWithFrame:CGRectMake(i*80, 6, 100, 26)];

    bun.tag=i;
    NSLog(@"tag :%ld",bun.tag);
    if (i==0) {
        [self ppp:bun];
    }
    UIViewController *vc=self.childViewControllers[i];
    [bun setTitle:vc.title forState:UIControlStateNormal];
    [bun setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    
    [bun setTitleColor:[UIColor redColor] forState:UIControlStateSelected];
    
    [bun addTarget:self action:@selector(ppp:) forControlEvents:UIControlEventTouchUpInside];
    [scr addSubview:bun];
    scr.contentSize=CGSizeMake(count *100, 0);
 
}

}

-(void)setCenterVc{
center=[[UIScrollView alloc]initWithFrame:CGRectMake(0,0, self.view.frame.size.width,self.view.frame.size.height)];
//center.backgroundColor=[UIColor greenColor];
[self.view addSubview:center];
center.pagingEnabled=YES;
center.bounces=NO;
center.delegate=self;
center.contentSize=CGSizeMake([UIScreen mainScreen].bounds.size.width*self.childViewControllers.count, 0);
for (int i=0; i<self.childViewControllers.count; i++) {
UIViewController *vc=self.childViewControllers[i];
vc.view.frame=CGRectMake(self.view.frame.size.width, 0, self.view.frame.size.width, self.view.frame.size.height);
[center addSubview:vc.view];
}
}

-(void)ppp:(UIButton* )sender{
[self selbun:sender];
//滾動(dòng)到對(duì)應(yīng)頁(yè)面的偏移量
CGFloat ofsetX = sender.tag*self.view.frame.size.width;
NSLog(@"%ld",sender.tag);
//設(shè)置中間滾動(dòng)式圖的偏移量
center.contentOffset=CGPointMake(ofsetX, 0);
//添加對(duì)應(yīng)子控制器view到對(duì)應(yīng)的位置
UIViewController *vc=self.childViewControllers[sender.tag];
//設(shè)置子控制器view的位置
vc.view.frame=CGRectMake(ofsetX, 0, self.view.frame.size.width, self.view.frame.size.height);
[center addSubview:vc.view];
}

-(void)selbun:(UIButton *)sender{
_selbun.selected=NO;
sender.selected=YES;
_selbun=sender;
}
-(void)qq1{
tiaoViewController *tiao=[tiaoViewController new];
[self.navigationController pushViewController:tiao animated:YES];
}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容