標(biāo)題與View的聯(lián)動(dòng)(通過(guò)ScrollView實(shí)現(xiàn))

首先我們要?jiǎng)?chuàng)建幾個(gè)View用來(lái)滾動(dòng)下面是我示例的


接下來(lái)就是ScrollView的實(shí)現(xiàn)了


<UIScrollViewDelegate,UIPopoverPresentationControllerDelegate>{

NSMutableArray *arr;

UIButton *btn;

}

下面如上圖所示



@property (nonatomic, strong) UISegmentedControl *segmentedControl;

@property (nonatomic, strong) UIScrollView *scrollView;

@property (nonatomic, strong) OneViewController *oneVC;

@property (nonatomic, strong) TwoViewController *twoVC;

@property (nonatomic, strong) ThreeViewController *threeVC;

@property (nonatomic, strong) FourViewController *fourVC;

@property (nonatomic, strong) FiveViewController *fiveVC;

@property (nonatomic, strong) SixViewController *sixVC;

然后是一些具體的實(shí)現(xiàn)代碼部分(viewDidLoad)


//? ? 創(chuàng)建一個(gè)view,放分段控制器

UIView *vv = [[UIView alloc]initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, 30)];

//? ? vv.backgroundColor = [UIColor redColor];

[self.view addSubview:vv];

//? ? 存儲(chǔ)分段控制器標(biāo)題

arr = [NSMutableArray arrayWithObjects:@"移動(dòng)通訊",@"傳媒",@"軟工",@"網(wǎng)工",@"云計(jì)算",@"建筑",nil];

// 適應(yīng)scrollView

self.automaticallyAdjustsScrollViewInsets = NO;

//? ? 創(chuàng)建分段控制器

self.segmentedControl = [[UISegmentedControl alloc]initWithItems:arr];

self.segmentedControl.frame = CGRectMake(0, 0, 386, 30);

//? ? 加載到vv上

[vv addSubview:self.segmentedControl];

//? ? self.navigationItem.titleView = self.segmentedControl;

[self.segmentedControl addTarget:self action:@selector(segmentedControlAction:) forControlEvents:UIControlEventValueChanged];

//? ? 默認(rèn)第一個(gè)視圖

self.segmentedControl.selectedSegmentIndex = 0;

// 創(chuàng)建scrollView

self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 94, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height - 64)];

[self.view addSubview:self.scrollView];

// 設(shè)置scrollView的內(nèi)容

self.scrollView.contentSize = CGSizeMake([UIScreen mainScreen].bounds.size.width * 6, [UIScreen mainScreen].bounds.size.height - 64);

self.scrollView.pagingEnabled = YES;

self.scrollView.bounces = YES;

// 創(chuàng)建控制器

self.oneVC = [OneViewController new];

self.twoVC = [TwoViewController new];

self.threeVC = [ThreeViewController new];

self.fourVC = [FourViewController new];

self.fiveVC = [FiveViewController new];

self.sixVC = [SixViewController new];

// 添加為self的子控制器

[self addChildViewController:self.oneVC];

[self addChildViewController:self.twoVC];

[self addChildViewController:self.threeVC];

[self addChildViewController:self.fourVC];

[self addChildViewController:self.fiveVC];

[self addChildViewController:self.sixVC];

//? ? 每個(gè)視圖

self.oneVC.view.frame = CGRectMake(0, 0, self.scrollView.frame.size.width, CGRectGetHeight(self.scrollView.frame));

self.twoVC.view.frame = CGRectMake([UIScreen mainScreen].bounds.size.width, 0, self.scrollView.frame.size.width, CGRectGetHeight(self.scrollView.frame));

self.threeVC.view.frame = CGRectMake([UIScreen mainScreen].bounds.size.width*2, 0, self.scrollView.frame.size.width, CGRectGetHeight(self.scrollView.frame));

self.fourVC.view.frame = CGRectMake([UIScreen mainScreen].bounds.size.width*3, 0, self.scrollView.frame.size.width, CGRectGetHeight(self.scrollView.frame));

self.fiveVC.view.frame = CGRectMake([UIScreen mainScreen].bounds.size.width*4, 0, self.scrollView.frame.size.width, CGRectGetHeight(self.scrollView.frame));

self.sixVC.view.frame = CGRectMake([UIScreen mainScreen].bounds.size.width*5, 0, self.scrollView.frame.size.width, CGRectGetHeight(self.scrollView.frame));

//加載到滾動(dòng)視圖上

[self.scrollView addSubview:self.oneVC.view];

[self.scrollView addSubview:self.twoVC.view];

[self.scrollView addSubview:self.fourVC.view];

[self.scrollView addSubview:self.fiveVC.view];

[self.scrollView addSubview:self.threeVC.view];

[self.scrollView addSubview:self.sixVC.view];

// 設(shè)置scrollView的代理

self.scrollView.delegate = self;

//? ? 按鈕

btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];

//按鈕位置

btn.frame = CGRectMake(385, 0, 30, 30);

//? ? 按鈕背景顏色

btn.backgroundColor = [UIColor whiteColor];

[btn setTitle:@"+" forState:0];

btn.layer.borderWidth = 1;

btn.layer.borderColor = [UIColor blueColor].CGColor;

//? ? 按鈕點(diǎn)擊事件

[btn addTarget:self action:@selector(btnclick) forControlEvents:UIControlEventTouchUpInside];

//? ? 加載

[vv addSubview:btn];

代理方法

//分段控制器方法

- (void)segmentedControlAction:(UISegmentedControl *)sender

{

[self.scrollView setContentOffset:CGPointMake(sender.selectedSegmentIndex * self.scrollView.frame.size.width, 0) animated:YES];

}

//滾動(dòng)視圖代理方法

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView

{

NSInteger n = scrollView.contentOffset.x / scrollView.frame.size.width;

self.segmentedControl.selectedSegmentIndex = n;

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容